Using PDFCreator with security options set

| |

Macro Purpose:
To use PDFCreator to make a PDF that leverages some of their security features. Specifically, we'll create a file that uses:

  1. A "File open" password
  2. 128 bit encryption
  3. Preventing content copying
  4. Preventing modification
  5. 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:

  1. Excel 2003
  2. 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
        .cClearCache
    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
        DoEvents
    Loop
    pdfjob.cPrinterStop = False

    'Wait until PDF creator is finished then release the objects
    Do Until pdfjob.cCountOfPrintjobs = 0
        DoEvents
    Loop
    pdfjob.cClose
    Set pdfjob = Nothing
End Sub