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 .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