Last post Apr 18, 2017 01:30 AM by jagomezp1972
Member
15 Points
193 Posts
Aug 03, 2008 08:09 AM|fizzystutter|LINK
Hi,
I'm just starting out with the iTextSharp PDF dll. I'm wanting to add a password to the pdf filethat I produce, but I can't find anywhere on the net how I add the password.
I've found the following, but I can't convert it:
writer.setEncryption(PdfWriter.STRENGTH128BITS, "userpass", "ownerpass", PdfWriter.AllowCopy | PdfWriter.AllowPrinting);
My page so far is:
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Partial Class PDF
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ProposalID As String = Request.QueryString("ProposalID")
Dim LevelsToGoBack As String = "Secure/"
Dim WebsiteAddress As String = ConfigurationManager.AppSettings.Get("WebsiteAddress")
Dim FileName As String = DateTime.UtcNow.Ticks & ".pdf"
Dim myDocument As iTextSharp.text.Document
myDocument = New Document(PageSize.A4)
PdfWriter.GetInstance(myDocument, New FileStream(HttpContext.Current.Server.MapPath(LevelsToGoBack & "/Files/" & FileName), FileMode.Create))
myDocument.AddTitle("Proposal")
myDocument.AddAuthor("Me")
myDocument.AddCreator("My Company")
myDocument.AddSubject("Proposal ID " & ProposalID)
myDocument.AddHeader("Proposal ID " & ProposalID, "Check Proposal Details")
myDocument.Open()
myDocument.Add(New Paragraph("hey"))
Dim Logo As Image = Image.GetInstance(WebsiteAddress & "/MediaFiles/PDFLogo.png")
myDocument.Add(Logo)
Dim aTable As New Table(2, 2)
aTable.AddCell("0.0")
aTable.AddCell("0.1")
aTable.AddCell("1.0")
aTable.AddCell("1.1")
myDocument.Add(aTable)
myDocument.Close()
End Sub
End Class
Any help would be appreciated Thanks James
Aug 03, 2008 11:05 AM|fizzystutter|LINK
Dim
document.Open()
document.Add(
document.Close() Thanks James
None
0 Points
4 Posts
Aug 14, 2010 07:00 AM|exoticankit|LINK
This code is amazing... now i won't be Crystal Report Supporter anymore.
using your lines now i can put password in secs...
love the way you described.
Cheers
1 Post
Feb 03, 2011 12:44 PM|Code Jockey|LINK
Very useful - much thanks!
In case anyone visiting here is wondering, you can also use the same member function on a pdfStamper [e.g. pdfStamperInstance.SetEncryption(...)]
Apr 18, 2017 01:30 AM|jagomezp1972|LINK
Thanks fizzystutter, you make my day.......
Member
15 Points
193 Posts
iTextSharp - Add password to PDF (vb)
Aug 03, 2008 08:09 AM|fizzystutter|LINK
Hi,
I'm just starting out with the iTextSharp PDF dll. I'm wanting to add a password to the pdf filethat I produce, but I can't find anywhere on the net how I add the password.
I've found the following, but I can't convert it:
writer.setEncryption(PdfWriter.STRENGTH128BITS, "userpass", "ownerpass", PdfWriter.AllowCopy | PdfWriter.AllowPrinting);
My page so far is:
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Partial Class PDF
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ProposalID As String = Request.QueryString("ProposalID")
Dim LevelsToGoBack As String = "Secure/"
Dim WebsiteAddress As String = ConfigurationManager.AppSettings.Get("WebsiteAddress")
Dim FileName As String = DateTime.UtcNow.Ticks & ".pdf"
Dim myDocument As iTextSharp.text.Document
myDocument = New Document(PageSize.A4)
PdfWriter.GetInstance(myDocument, New FileStream(HttpContext.Current.Server.MapPath(LevelsToGoBack & "/Files/" & FileName), FileMode.Create))
myDocument.AddTitle("Proposal")
myDocument.AddAuthor("Me")
myDocument.AddCreator("My Company")
myDocument.AddSubject("Proposal ID " & ProposalID)
myDocument.AddHeader("Proposal ID " & ProposalID, "Check Proposal Details")
myDocument.Open()
myDocument.Add(New Paragraph("hey"))
Dim Logo As Image = Image.GetInstance(WebsiteAddress & "/MediaFiles/PDFLogo.png")
myDocument.Add(Logo)
Dim aTable As New Table(2, 2)
aTable.AddCell("0.0")
aTable.AddCell("0.1")
aTable.AddCell("1.0")
aTable.AddCell("1.1")
myDocument.Add(aTable)
myDocument.Close()
End Sub
End Class
Any help would be appreciated
Thanks
James
Member
15 Points
193 Posts
Re: iTextSharp - Add password to PDF (vb)
Aug 03, 2008 11:05 AM|fizzystutter|LINK
Hi,
I've managed to sort it with the following code:Dim
document As Document = New Document(PageSize.A4, 50, 50, 50, 50) Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(HttpContext.Current.Server.MapPath(LevelsToGoBack & "/Files/" & FileName), FileMode.Create))writer.SetEncryption(PdfWriter.STRENGTH128BITS, "mypass", Nothing, PdfWriter.AllowCopy)document.Open()
document.Add(
New Paragraph("This document is Top Secret!"))document.Close()
Thanks James
None
0 Points
4 Posts
Re: iTextSharp - Add password to PDF (vb)
Aug 14, 2010 07:00 AM|exoticankit|LINK
This code is amazing... now i won't be Crystal Report Supporter anymore.
using your lines now i can put password in secs...
love the way you described.
Cheers
None
0 Points
1 Post
Re: iTextSharp - Add password to PDF (vb)
Feb 03, 2011 12:44 PM|Code Jockey|LINK
Very useful - much thanks!
In case anyone visiting here is wondering, you can also use the same member function on a pdfStamper [e.g. pdfStamperInstance.SetEncryption(...)]
None
0 Points
1 Post
Re: iTextSharp - Add password to PDF (vb)
Apr 18, 2017 01:30 AM|jagomezp1972|LINK
Thanks fizzystutter, you make my day.......