Click here to Skip to main content
15,311,151 members
Articles / Programming Languages / Visual Basic
Posted 17 Oct 2009

Tagged as


21 bookmarked

Encrypt PDF

Rate me:
Please Sign up or sign in to vote.
4.43/5 (13 votes)
17 Oct 2009CPOL
This Windows application lets you password protect PDF files in a given folder.

Image 1


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.

Comments and Discussions

Questionencrypt pdf file Pin
Member 143566515-May-19 21:05
MemberMember 143566515-May-19 21:05 
Questionthanks Pin
ahmadlubiss26-Apr-19 17:59
Memberahmadlubiss26-Apr-19 17:59 
QuestionPDF Encryptor Pin
Member 1364935730-Jan-18 19:43
MemberMember 1364935730-Jan-18 19:43 
Question[My vote of 1] please complete your job Pin
Rayig28-Jan-13 20:23
MemberRayig28-Jan-13 20:23 
QuestionDecrypt Pdf file Pin
programadorvb814-Aug-11 5:12
Memberprogramadorvb814-Aug-11 5:12 
GeneralMy vote of 5 Pin
Gaurav Lal8-Jul-11 21:35
MemberGaurav Lal8-Jul-11 21:35 
QuestionQuestion Pin
Menon Santosh18-Jan-11 19:49
professionalMenon Santosh18-Jan-11 19:49 
GeneralMy vote of 5 Pin
Eduardo_David11-Jul-10 4:22
MemberEduardo_David11-Jul-10 4:22 
GeneralExcessive Code with no explanation Pin
Abhishek Sur12-Nov-09 23:45
professionalAbhishek Sur12-Nov-09 23:45 

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

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