Results 1 to 6 of 6

Thread: Decrypt PDF

  1. #1
    Junior Member
    Join Date
    May 12
    Posts
    19

    Decrypt PDF

    I am using this code to Encrypt a PDF , works fine.

    But how do i Decrypt this pdf ???

    Code:
    Imports System.IO
    Imports iTextSharp.text.pdf
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Using input As Stream = New FileStream("C:\NS\test.pdf", FileMode.Open, FileAccess.Read, FileShare.Read)
                Using output As Stream = New FileStream("C:\NS\test_encrypted.pdf", FileMode.Create, FileAccess.Write, FileShare.None)
                    Dim reader As New PdfReader(input)
                    PdfEncryptor.Encrypt(reader, output, True, "secret", "secret", PdfWriter.ALLOW_PRINTING)
                End Using
            End Using
        End Sub
    End Class

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 06
    Location
    Providence, RI - USA
    Posts
    9,167

    Re: Decrypt PDF

    You don't really decrypt an encrypted pdf. The way you "decrypt" it is to open it and supply the password that it was encrypted with. For example:
    Code:
    Dim password As String = "secret"
    Dim reader as New PdfReader(pdfFilePath, PdfWriter.GetISOBytes(password))
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Junior Member
    Join Date
    May 12
    Posts
    19

    Re: Decrypt PDF

    Ok , looks alright.

    I am trying to open it , but where do i put the code ??

    Want to open the file with a button

    "C:\NS\test_encrypted.pdf"

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 06
    Location
    Providence, RI - USA
    Posts
    9,167

    Re: Decrypt PDF

    If you want to open the pdf file using the default application, just start a process. Put this code in a button click event handler
    Code:
    Dim pdfFile As String = "C:\NS\test_encrypted.pdf"
    If IO.File.Exists(pdfFile) Then
        Process.Start(pdfFile)
    End If
    The default pdf reader application (i.e. Acrobat Reader) will prompt you for the password, and then open the file.

    If you want to open it programmatically (nothing shown on screen) so that you can do other stuff with it (also programmatically) then do as I posted in the previous post.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5
    Junior Member
    Join Date
    May 12
    Posts
    19

    Re: Decrypt PDF

    stanav ,thanks

    But i can't get it to work so it opens with the programm (nothing shown on screen) .I think i do something wrong with process start ?????

    The other code works great.



    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim password As String = "secret"
    Dim pdfTemplate As String = (My.Application.Info.DirectoryPath & "\test_encrypted.pdf")
    Dim pdfReader As PdfReader = New PdfReader(pdfTemplate, PdfWriter.GetISOBytes(password))

    Process.Start(My.Application.Info.DirectoryPath & "\test_encrypted.pdf")


    End Sub

  6. #6
    Junior Member
    Join Date
    May 12
    Posts
    19

    Re: Decrypt PDF

    stanav,

    I think i found something , i converted it from # to vb

    But it is not working , what am i missing here ????

    Imports System.IO
    Imports iTextSharp.text.pdf
    Public Class Form1
    Dim WorkingFolder As String = ("C:\NSG\")
    Dim InputFile As String = Path.Combine(WorkingFolder, "BM1.pdf")
    Dim OutputFile As String = Path.Combine(WorkingFolder, "BM_dec.pdf")
    'will be created automatically //You must provide owner password but not the user password.


    Private Sub DecryptFile(ByVal inputFile As String, ByVal outputFile As String)

    Dim password As String = "1234"
    ' Your Key Here
    Try
    Dim reader As New PdfReader(inputFile, New System.Text.ASCIIEncoding().GetBytes(password))

    Using memoryStream As New MemoryStream()
    Dim stamper As New PdfStamper(reader, memoryStream)
    stamper.Close()
    reader.Close()
    File.WriteAllBytes(outputFile, memoryStream.ToArray())

    End Using
    Catch err As Exception
    '' Console.WriteLine(err.Message)
    MsgBox("")
    End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    DecryptFile(WorkingFolder, "BM1.pdf")
    End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •