Is it possible to (re)print existing PDF to a new securised one with COM Lib in vba?
Please take a look to my issue history before answer me :
I tried to print MS ACCESS (2013) report to a securized PFD file. It works but some time I get some error on the PDF file (It look like the printing job was running before ACCESS finish formating the report) (PDF_withPassword)
I also tried to print the report with ACCESS without password using DoCmd.OutputTo. It always work (PDF_withoutPassword)
So I want to convert “PDF_withoutPassword” file to “PDF_withPassword” using your COM Lib in vba (ACCESS).
Do you have any idea how to do that ?
I need to do all the printing job by code (vba)
I already have the code to print a open document in Access (see the code below)
Sub PrintToPDFCreator(sPDFDirPath As String, _
sFileName As String, _
Optional sOwnerPassword As String, _
Optional sUserPassword As String, _
Optional bOpenViewer As String, _
Optional bAllowCopy As Boolean, _
Optional bAllowPrint As Boolean, _
Optional bAllowEdit As Boolean)
Dim sPDFFullPath As String
Dim PDF As New PdfCreatorObj
Dim PDFdevices As PDFCreator_COM.Printers
Dim DefaultPrinterName, Prt As Printer
Dim PDFprinterName As String
Dim PDFCreatorQueue As PDFCreator_COM.Queue
Dim pdfjob As PDFCreator_COM.PrintJob
On Error GoTo Err_Infos
'create dir if needed
CreateDirectory (sPDFDirPath)
sPDFFullPath = sPDFDirPath & sFileName
'Delete previous version of the file
DeleteFile sPDFFullPath
'Keep the current printer
DefaultPrinterName = Printer.DeviceName
'Get the name of the PDF printer
Set PDFdevices = PDF.GetPDFCreatorPrinters
PDFprinterName = PDFdevices.GetPrinterByIndex(0)
'Direct Printer to PDFcreator
For Each Prt In Printers
If Prt.DeviceName = PDFprinterName Then
Set Printer = Prt
Exit For
End If
Next
Set PDFCreatorQueue = New PDFCreator_COM.Queue
PDFCreatorQueue.Initialize
DoCmd.PrintOut 'Imprimer document actif
If Not PDFCreatorQueue.WaitForJob(10) Then
MsgBox "Impossible de joindre la file d'attente - 10sec. ", , TITLE_POPUP_ERROR
GoTo Err_Infos
End If
Set pdfjob = PDFCreatorQueue.NextJob
With pdfjob
.SetProfileByGuid ("DefaultGuid")
' Set up the pdf security using the SetProfileSetting method of the job object.
'-------------------------------------------------------------------------------
'Since we want to make our pdf more safe, we have to enable the security action first
.SetProfileSetting "PdfSettings.Security.Enabled", "true"
' We set up the encryption level to medium
.SetProfileSetting "PdfSettings.Security.EncryptionLevel", "Rc128Bit"
' Notice that in order to have a user password we have also to set the owner password
' and additionally enable the RequireUserPassword property
.SetProfileSetting "PdfSettings.Security.OwnerPassword", sOwnerPassword
' Require a user password to be able to view the PDF
.SetProfileSetting "PdfSettings.Security.RequireUserPassword", "true"
' Now everyone who wants to open the converted file has to know the security password "myPassword"
.SetProfileSetting "PdfSettings.Security.UserPassword", sUserPassword
' Set Security options
.SetProfileSetting "PdfSettings.Security.AllowToCopyContent", IIf(bAllowCopy, "True", "False")
.SetProfileSetting "PdfSettings.Security.AllowPrinting", IIf(bAllowPrint, "True", "False")
.SetProfileSetting "PdfSettings.Security.AllowToEditTheDocument", IIf(bAllowEdit, "True", "False")
.SetProfileSetting "DropboxSettings.EnsureUniqueFilenames", "False"
' Setup main option
'-------------------------------------------------------------------------------
.SetProfileSetting "OpenViewer", IIf(bOpenViewer, "True", "False")
.ConvertTo sPDFFullPath
End With
Do Until pdfjob.IsFinished
DoEvents
Loop
Set pdfjob = Nothing
PDFCreatorQueue.ReleaseCom
'Restore Printer to the old printer
For Each Prt In Printers
If Prt.DeviceName = DefaultPrinterName Then
Set Printer = Prt
Exit For
End If
Next
Exit_currentFunction:
Set PDFCreatorQueue = Nothing
Exit Sub
Err_Infos:
MsgBox "Erreur #" & Err.Number & " : " & Err.Description
Err.Raise Err.Number, IIf(Err.Source = Application.VBE.ActiveVBProject.Name, "Tools_Files.PrintToPDFCreator" & " " & Erl, Err.Source), Err.Description
GoTo Exit_currentFunction
End Sub
I tried to modify the previous code but I did not understand when and how to define the source file (unsecure) and the target file (secure) and when I have to use printFile().
Could you help me?
There is not exemple in your web site with this function (printfile) ?
you could more or less use the same code again for printing the unsecured PDF to the PDFCreator printer, just by initializing PDFCreator.PDFCreatorObj and replacing DoCmd.PrintOut with PDFCreator.PrintFile(“Path to the PDF”).
Or you could replace DoCmd.PrintOut with any command which will print the PDF, here is a description how to print any file with VBA https://www.mrexcel.com/forum/excel-questions/719507-print-any-file-using-vba-excel.html
All methods will require a PDF viewer capable of printing PDF to be installed on the machine.
I replace 'DoCmd.PrintOut by PDF.PrintFile sPDFFullPath_withoutPW and I finish with .ConvertTo sPDFFullPath_withPW
It's work but I have to close the PDF viewer to let the program printing the new PDF (with password). If I do not close it, the program freeze until I close the PDf viewer.
Do you have any Idea to avoid such issue ?
I want to populate the PDf without the help of the database user.