• Using PDFCreator with security options set

    This article shows how to use PDFCreator to make a PDF that leverages some of their security features. Specifically, we'll create a file that uses:
    • A "File open" password
    • 128 bit encryption
    • Preventing content copying
    • Preventing modification
    • Preventing printing

    These code examples are built for PDFCreator, an open source PDF writer utility. Unlike Adobe Acrobat and CutePDF, which both require pro versions to create PDF's via code, PDFCreator is completely free! Download PDF Creator from Sourceforge here. Please note that this code will NOT work with Adobe Acrobat.

    Versions Tested:
    These routines were tested successfully using PDFCreator 0.9.1, GPLGhostscript.exe download package, on Windows XP Pro (SP2). Excel versions tested include:
    • Excel 2003
    • Excel 2007

    NOTE: Before you "go it alone" with trying to adapt any of these routines, you may want to read this article, which shares some of the idiosyncrasies discovered in the development of the PDFCreator code samples.

    VBA Code Required:
    The following code all goes in a standard module:
    Option Explicit
    Sub PrintToPDF_WithSecurity()
    'Author       : Ken Puls (www.excelguru.ca)
    'Macro Purpose: Print to PDF file using PDFCreator WITH SECURITY
    '   (Download from http://sourceforge.net/projects/pdfcreator/)
    '   Designed for early bind, set reference to PDFCreator
        Dim pdfjob As PDFCreator.clsPDFCreator
        Dim sPDFName As String
        Dim sPDFPath As String
        Dim sMasterPass As String
        Dim sUserPass As String
        '/// Change the output file names and passwords (if security req'd)! ///
        sPDFName = "testPDF.pdf"
        sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
        sMasterPass = "master"
        sUserPass = "letmein"
        'Check if worksheet is empty and exit if so
        If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
        Set pdfjob = New PDFCreator.clsPDFCreator
        With pdfjob
            If .cStart("/NoProcessingAtStartup") = False Then
                MsgBox "Can't initialize PDFCreator.", vbCritical + _
                        vbOKOnly, "PrtPDFCreator"
                Exit Sub
            End If
            'Set details on where to save file to, and flag it automatic
            .cOption("UseAutosave") = 1
            .cOption("UseAutosaveDirectory") = 1
            .cOption("AutosaveDirectory") = sPDFPath
            .cOption("AutosaveFilename") = sPDFName
            .cOption("AutosaveFormat") = 0    ' 0 = PDF
            'The following are required to set security of any kind
            .cOption("PDFUseSecurity") = 1
            .cOption("PDFOwnerPass") = 1
            .cOption("PDFOwnerPasswordString") = sMasterPass
            'To set individual security options
            .cOption("PDFDisallowCopy") = 1
            .cOption("PDFDisallowModifyContents") = 1
            .cOption("PDFDisallowPrinting") = 1
            'To force a user to enter a password before opening
            .cOption("PDFUserPass") = 1
            .cOption("PDFUserPasswordString") = sUserPass
            'To change to High encryption
            .cOption("PDFHighEncryption") = 1
            'Get ready for the print job
        End With
        'Print the document to PDF
        ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
        'Wait until the print job has entered the print queue
        Do Until pdfjob.cCountOfPrintjobs = 1
        pdfjob.cPrinterStop = False
        'Wait until the file shows up before closing PDF Creator
        Loop Until Dir(sPDFPath & sPDFName) = sPDFName
        Set pdfjob = Nothing
    End Sub


    I'm afraid that you must be logged in to comment or leave a testimonial. I wish it could be otherwise, but I'm trying to keep my site spam free for everyone's benefit. If you don't yet have an account it's completely free to sign up, and a very quick process. Simply click here to Register. Not only can you post a comment here, but it gives you full access to posts questions in our forum as well!


    If you already have an account, and just haven't logged in yet, what are you waiting for? Login Now!

    Comments 3 Comments
    1. cricketstar's Avatar
      cricketstar -
      Ken, You're my God. Thank you.
    1. eric654's Avatar
      eric654 -
      I'm trying to use this with the latest version of pdfcreator but it's not working.
      also tried installing old version of pdfcreator but seems like it's not compatible with windows 7...
    1. vinodhan1987's Avatar
      vinodhan1987 -
      program completely successful!!! but report not generate I am using same configuration
      Quote Originally Posted by cricketstar View Post
      Ken, You're my God. Thank you.