Password protect pdf file

AMER SAID 396 Reputation points
2022-03-30T00:05:20.797+00:00

hi

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
iTextSharp

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)
        oPdfDoc.Open()

        oPdfDoc.SetPageSize(iTextSharp.text.PageSize.LEDGER.Rotate())

        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
            oPdfDoc.SetPageSize(oPdfReader.GetPageSizeWithRotation(iPage))
            oPdfDoc.NewPage()

            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)
            Else
                oDirectContent.AddTemplate(oPdfImportedPage, 1.0F, 0, 0, 1.0F, 0, 0)
            End If
        Loop

        oPdfDoc.Close()


    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)
            pdfDoc.Open()
            Dim i As Integer = 0

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

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

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 32,931 Reputation points Microsoft Vendor
    2022-03-30T06:07:07.553+00:00

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.