我正在寻求密码保护来自 MS Access(2013 版)的 PDF 报告。
我在 Stack 上找到了这个问题和答案。
创建报告后,它包含所有必要的信息。调用该函数后,对 PDF 进行密码保护,名称相同,PDF 受密码保护,但为空。
传递给 Ghostscript 的指令:
-q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOwnerPassword=170284 -sUserPassword=170284 -dCompatibilityLevel=2.0 -sOutputFile=D:\Data\ Report.pdf
原始的公共函数(略有修改):
Public Function fctPDO_Print_pdf_GhostScript(strSourceFolder As String, strTargetFolder As String, strFile_for_pdf As String, Optional strUserPassword As String = "", Optional strOwnerPassword As String = "") As String
' http://www.herber.de/forum/archiv/1164to1168/1165503_Zusammenfuehren_von_PDF_Files.html#1165503
' https://stackoverflow.com/questions/49953421/ghostscript-with-aes-256-password-protection-for-pdf-2-0-documents
' PDO: Prints a pdf (originally multi-pdf). Requires Ghostscript, and read/write rights.
' Existing files are overwritten without asking.
' Provide both passwords to lock. Ghostscript does rc4 , being comparatively unsafe.
'
On Error Resume Next
Dim fso As Object, WshShell As Object
Dim strCommand As String
Dim strGhostScript As String
Set fso = CreateObject("Scripting.FileSystemObject")
'Path to gswin32c.exe
strGhostScript = "C:\Program Files (x86)\gs9.54.0\bin\gswin64c.exe"
'Shell-command prepare
strTargetFolder = fso.GetFolder(strTargetFolder).ShortPath
strGhostScript = fso.GetFile(strGhostScript).ShortPath
'PDO: Password-Phrase, with Ghostscript only RC4 possible...
strCommand = strGhostScript & " -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOwnerPassword=" & strOwnerPassword
strCommand = strCommand & " -sUserPassword=" & strUserPassword & " -dCompatibilityLevel=2.0"
strCommand = strCommand & " -sOutputFile=" & strTargetFolder & "\" & strFile_for_pdf
'Execute
Debug.Print strCommand
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run strCommand, 0, True
Set WshShell = Nothing
fctPDO_Print_pdf_GhostScript = strTargetFolder & strFile_for_pdf
' Cleanup:
Err_Handler:
Set fso = Nothing
End Function
感谢@Kens 的提示。更新后的代码现在可以工作:
Public Function fctPDO_Print_pdf_GhostScript(strSourceFolder As String, strTargetFolder As String, strOriginalFile As String, strTargetFile As String, Optional strUserPassword As String = "", Optional strOwnerPassword As String = "") As String
' PDO: Prints a pdf (originally multi-pdf). Requires Ghostscript, and read/write rights.
' Existing files are overwritten without asking.
' Provide both passwords to lock. Ghostscript does rc4 , being comparatively unsafe.
'
On Error Resume Next
Dim fso As Object, WshShell As Object
Dim strCommand As String
Dim strGhostScript As String
Set fso = CreateObject("Scripting.FileSystemObject")
'Path to gswin32c.exe
strGhostScript = "C:\Program Files (x86)\gs9.54.0\bin\gswin64c.exe"
'Shell-command prepare
strTargetFolder = fso.GetFolder(strTargetFolder).ShortPath
strGhostScript = fso.GetFile(strGhostScript).ShortPath
'PDO: Password-Phrase, with Ghostscript only RC4 possible...
strCommand = strGhostScript & " -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOwnerPassword=" & strOwnerPassword
strCommand = strCommand & " -sUserPassword=" & strUserPassword & " -dCompatibilityLevel=2.0"
strCommand = strCommand & " -sOutputFile=" & strTargetFolder & "\" & strTargetFile
strCommand = strCommand & " " & strSourceFolder & "\" & strOriginalFile
'Execute
Debug.Print strCommand
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run strCommand, 0, True
Set WshShell = Nothing
fctPDO_Print_pdf_GhostScript = strTargetFolder & strTargetFile
' Cleanup:
Err_Handler:
Set fso = Nothing
End Function