I use the following code for years and worked well. Since 6.0.0 there's some problems. Please see the example code, because it's easier to understand what I want to report.
- ConvertTo(path) will duplicate pages (or append the file itself).
- Calling ConvertTo() with a non-existing path the new file won't be created.
Public Function EncryptPdf(ByVal pdfFullPath As String, ByVal pwd As String, Optional ByVal titleStr As String, Optional ByVal subjectStr As String) As String
Dim pdfCreatorQueue As Object
Set pdfCreatorQueue = CreateObject("PDFCreator.JobQueue")
Dim creator As Object
Set creator = CreateObject("PDFCreator.PDFCreatorObj")
Dim printJob As Object
If Not creator.IsInstanceRunning() Then
pdfCreatorQueue.Initialize
End If
creator.AddFileToQueue pdfFullPath
Set printJob = pdfCreatorQueue.NextJob
With printJob
.SetProfileSetting "TitleTemplate", titleStr
.SetProfileSetting "SubjectTemplate", subjectStr
.SetProfileSetting "PdfSettings.Security.Enabled", "true"
.SetProfileSetting "PdfSettings.Security.EncryptionLevel", "Aes256Bit"
.SetProfileSetting "PdfSettings.Security.RequireUserPassword", "true"
.SetProfileSetting "PdfSettings.Security.UserPassword", pwd
.SetProfileSetting "PdfSettings.Security.OwnerPassword", "verysecurepassword123456"
.SetProfileSetting "PdfSettings.Security.AllowToEditTheDocument", "False"
'This will duplicate the pages. For example, a 3-page PDF will have 6 pages.
.ConvertTo pdfFullPath
'Giving a new path will be ignored, the file won't be created. Instead the pdfFullPath will be modified.
'.ConvertTo "c:\newpdf.pdf"
EncryptPdf = .GetOutputFiles.GetFileName(0)
End With
Cleanup:
pdfCreatorQueue.ReleaseCom
Set printJob = Nothing
Set pdfCreatorQueue = Nothing
Set creator = Nothing
End Function