Click here to Skip to main content
11,806,350 members (61,867 online)
Click here to Skip to main content

Encrypt PDF

, 17 Oct 2009 CPOL 27.9K 1.7K 20
Rate this:
Please Sign up or sign in to vote.
This Windows application lets you password protect PDF files in a given folder.


This complete Windows application lets you password protect your PDF files in a given folder and its subfolders. It uses free iTextSharp library.

Using the code

To use this program, simply select the source and the destination folders. It the program experiences any problems, it will create a log file (Log.txt) in the source folder.

Here is the code:

Private Sub btnFrom_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnFrom.Click
    txtFrom.Text = fldFrom.SelectedPath
End Sub

Private Sub btnTo_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnTo.Click
    txtTo.Text = fldTo.SelectedPath
End Sub

Private Sub btnProcess_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnProcess.Click

    If Trim(txtPassword.Text) = "" Then
        MsgBox("Password is missing.")
        Exit Sub
    End If

    Dim sFromPath As String = txtFrom.Text
    Dim sToPath As String = txtTo.Text

    If Not Directory.Exists(sFromPath) Then
        MsgBox("From folder does not exist")
        Exit Sub
    End If

    ProccessFolder(sFromPath, sToPath)

End Sub

Sub ProccessFolder(ByVal sFromPath As String, ByVal sToPath As String)

    If Not Directory.Exists(sToPath) Then
        Catch ex As Exception
            MsgBox("To folder does not exist and could not be created.")
            Exit Sub
        End Try
    End If

    Dim oFiles As String() = Directory.GetFiles(sFromPath)
    ProgressBar1.Maximum = oFiles.Length

    btnProcess.Enabled = False
    For i As Integer = 0 To oFiles.Length - 1
        Dim sFromFilePath As String = oFiles(i)
        Dim oFileInfo As New FileInfo(sFromFilePath)
        Dim sToFileName As String = sToPath & "\" & oFileInfo.Name

        If UCase(oFileInfo.Extension) = ".PDF" Then
                EncryptPdf(sFromFilePath, sToFileName, txtPassword.Text)
            Catch ex As Exception
                Dim oStreamWriter As New StreamWriter(txtFrom.Text & "\Log.txt", True)
                oStreamWriter.WriteLine(sFromFilePath & " " & ex.Message)
            End Try
        End If

        ProgressBar1.Value = i

    btnProcess.Enabled = True
    ProgressBar1.Value = 0

    Dim oFolders As String() = Directory.GetDirectories(sFromPath)
    For i As Integer = 0 To oFolders.Length - 1
        Dim sChildFolder As String = oFolders(i)
        Dim iPos As Integer = sChildFolder.LastIndexOf("\")
        Dim sFolderName As String = sChildFolder.Substring(iPos + 1)
        ProccessFolder(sChildFolder, sToPath & "\" & sFolderName)

End Sub

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


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Igor Krupitsky
Web Developer
United States United States
Igor is a business intelligence consultant working in Tampa, Florida. He has a BS in Finance from University of South Carolina and Masters in Information Management System from University of South Florida. He also has following professional certifications: MCSD, MCDBA, MCAD.

You may also be interested in...

Comments and Discussions

QuestionDecrypt Pdf file Pin
programadorvb814-Aug-11 5:12
memberprogramadorvb814-Aug-11 5:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151002.1 | Last Updated 18 Oct 2009
Article Copyright 2009 by Igor Krupitsky
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid