see here the VBA code tested; many thanks
Sub Protectpdf()
Dim PDFtkPath As String
Dim InputFilePath As String
Dim OutputFilePath As String
Dim Password As String
Dim ShellCmd As String
' Path to the PDFtk executable
PDFtkPath = "C:\Program Files (x86)\PDFtk\bin\Pdftkxp.exe"
' Path to the input PDF file
InputFilePath = "D:\Test\input.pdf"
' Path to the output PDF file (password protected)
OutputFilePath = "D:\Test\output.pdf"
' Password for the PDF file
Password = "090046"
' Assemble the command to password protect the PDF using PDFtk
ShellCmd = PDFtkPath & " " & InputFilePath & " output " & OutputFilePath & " user_pw " & Password
'alternative syntax not working:
'ShellCmd = Chr(34) & PDFtkPath & Chr(34) & " " & Chr(34) & InputFilePath & Chr(34) & " output " & Chr(34) & OutputFilePath & Chr(34) & " user_pw " & Password
'or, another one not working:
'ShellCmd = PDFtkPath & " """ & InputFilePath & """ output """ & OutputFilePath & """ user_pw " & Password
' Use the Shell function to execute the PDFtk command
Shell ShellCmd, vbHide
MsgBox "PDF file has been password protected and saved to " & OutputFilePath
End Sub
Bookmarks