Password protect pdf file

AMER SAID 396 Reputation points


I want to protect a PDF file with a password and re-cancel the password from reading the file in my program.
My entire project depends on a library

code encrypt with password work perfect

  Sub EncryptPdf(ByVal sInFilePath As String, ByVal sOutFilePath As String, ByVal sPassword As String)

        Dim oPdfReader As iTextSharp.text.pdf.PdfReader = New iTextSharp.text.pdf.PdfReader(sInFilePath)
        Dim oPdfDoc As New iTextSharp.text.Document()
        Dim oPdfWriter As PdfWriter = PdfWriter.GetInstance(oPdfDoc, New FileStream(sOutFilePath, FileMode.Create))
        oPdfWriter.SetEncryption(PdfWriter.STRENGTH40BITS, sPassword, sPassword, PdfWriter.AllowCopy)


        Dim oDirectContent As iTextSharp.text.pdf.PdfContentByte = oPdfWriter.DirectContent
        Dim iNumberOfPages As Integer = oPdfReader.NumberOfPages
        Dim iPage As Integer = 0

        Do While (iPage < iNumberOfPages)
            iPage += 1

            Dim oPdfImportedPage As iTextSharp.text.pdf.PdfImportedPage = oPdfWriter.GetImportedPage(oPdfReader, iPage)
            Dim iRotation As Integer = oPdfReader.GetPageRotation(iPage)
            If (iRotation = 90) Or (iRotation = 270) Then
                oDirectContent.AddTemplate(oPdfImportedPage, 0, -1.0F, 1.0F, 0, 0, oPdfReader.GetPageSizeWithRotation(iPage).Height)
                oDirectContent.AddTemplate(oPdfImportedPage, 1.0F, 0, 0, 1.0F, 0, 0)
            End If


    End Sub

code decrypt with password not work and give error Byte ??

 Dim rafa As RandomAccessFileOrArray = New RandomAccessFileOrArray(txtfilefrom.Text)
        If rafa IsNot Nothing Then
            Dim objms As MemoryStream = New MemoryStream()
            Dim pdfReader As PdfReader = New PdfReader(txtfileto.Text, txtPasswor.Text)//error Byte ??
            Dim noofpages As Integer = pdfReader.NumberOfPages
            Dim pdfDoc As Document = New Document()
            Dim pdfCopy As PdfCopy = New PdfCopy(pdfDoc, objms)
            Dim i As Integer = 0

            While i < noofpages
                pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfReader, i + 1))
                i += 1
            End While

        End If
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,408 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 22,021 Reputation points Microsoft Vendor

    Hi @AMER SAID ,
    Please see if the solution in the questions below is helpful to you.
    How to decrypt a pdf file by supplying password of the file as argument using c#?
    Since iTextSharp is a third-party library, we do not support it. You can post your question using iTextSharp in the above forum.
    Best Regards.
    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments