• 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:
    Code:
    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 the file shows up before closing PDF Creator
        Do
            DoEvents
        Loop Until Dir(sPDFPath & sPDFName) = sPDFName
    
        Set pdfjob = Nothing
    
    End Sub
  • MVP Logo
  •  Donations

    If you like our website and would like to give something in return, you can make a donation. All donations are gratefully received and go to support the site.


    Select your preferred currency and donation amount, then click the donate button.

  • Recent Forum Posts

    Colo

    How to Rename a File from English to Other Language?

    Yeah, some massive HTML conversion is my line. In other words, most of difficult things can be done with Excel alone like this time. Well done, Excel!...

    Colo Today, 07:36 AM Go to last post
    Ken Puls

    Link a series name to a cell using Excel 2010 VBA

    Hi there,

    I recorded linking the title to a cell and it came back with the following. Does this help?

    Code:
        ActiveChart.SetElement
    ...

    Ken Puls Today, 04:06 AM Go to last post
    Ken Puls

    How to Rename a File from English to Other Language?

    Colo, that was way too easy... I was expecting some massive HTML conversion, or a huge engine to compare each character against a library of Chr codes!...

    Ken Puls Today, 04:02 AM Go to last post
    Ken Puls

    Shared file\macro & IP address

    Oh, and as for the max number of users who can access the file in the shared folder at once...

    • For reading, I believe it's unlimited. (The second and
    ...

    Ken Puls Today, 03:59 AM Go to last post
    Ken Puls

    Shared file\macro & IP address

    ibrahimaa,

    There is no one-line way to get your IP address the way you are getting the username. So you're going to need more code than...

    Ken Puls Today, 03:56 AM Go to last post