locked
iTextSharp - Add password to PDF (vb) RRS feed

  • Question

  • User333333839 posted

    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

    Sunday, August 3, 2008 8:09 AM

All replies

  • User333333839 posted

    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

    Sunday, August 3, 2008 11:05 AM
  • User-1584104985 posted

    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 Wink

    Saturday, August 14, 2010 7:00 AM
  • User-535478063 posted

    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(...)]

    Thursday, February 3, 2011 12:44 PM
  • User1466948442 posted

    Thanks fizzystutter, you make my day.......

    Tuesday, April 18, 2017 1:30 AM