【问题标题】:How to set a password for PDF file export?如何设置PDF文件导出密码?
【发布时间】:2020-05-21 15:03:52
【问题描述】:

我需要添加一个密码来提取 PDF。

Dim FileName As String
Dim FilePath As String
FileName = Me.Full_Name & "_" & Me.ID
FilePath = "C:\Users\Desktop\" & FileName & ".Pdf"
DoCmd.OutputTo acOutputReport, "Report", acFormatPDF, FilePath
MsgBox "Exported Successfully"

【问题讨论】:

  • 我不认为这可以使用DoCmd.OutputTo 来完成,我认为您可能需要使用第三方 PDF 编写器来执行此操作。
  • 可以使用 VBA 操作 PDF 文件。有一个参考图书馆。但是,根据我的经验,还需要安装 Adob​​e Acrobat(不是 Reader)版本。
  • 嗨 Applecore - 此代码可以正常工作,以 PDF 格式导出报告。我需要设置导出 PDF 的密码,如果有任何帮助我的话!
  • 常规 PDF 文档查看器不支持密码 - 您需要更高付费版本。从 Access 输出的 PDF 不支持密码。您可以输出文件,然后用 PDF(adobe)打开,设置密码并关闭它。您可以使用 PDF + 在 VBA 代码中设置密码来执行此打开的第二步,但您需要付费版本的 adobe acrobat 来执行此操作。免费版不允许也没有密码选项 - 您需要某种付费版本才能使用此功能。所有版本的 PDF 都支持打开 PDF 并要求输入密码,但只有付费版本允许设置密码。
  • 就像 Albert 所说,您需要第三方解决方案。如何导出 pdf 并在之后使用密码对其进行压缩?您可以使用 7-zip 和命令行。

标签: vba ms-access pdf


【解决方案1】:

这是一个使用 Ghostscript 的解决方案。首先像你已经做的那样打印你的pdf,然后调用下面的函数。根据许可协议,Ghostscript 是免费的,afaik。

Public Function fctPDO_Print_pdf_GhostScript(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 strZielOrdner As String
          Dim strQuellOrdner As String
          Dim strCommand As String
          Dim strGhostScript As String

          Dim strFile_with_Path As String
          Dim strTargetFile_without_Path As String



          Set fso = CreateObject("Scripting.FileSystemObject")

         'Path to gswin32c.exe
          strGhostScript = "C:\Program Files (x86)\gs\gs9.19\bin\gswin32c.exe"

         ' Define folder

          strQuellOrdner = "D:\PDO_test"
          strZielOrdner = "D:\PDO_test"


         ' Shell-command prepare
          strZielOrdner = fso.GetFolder(strZielOrdner).ShortPath
          strGhostScript = fso.GetFile(strGhostScript).ShortPath
          strCommand = strGhostScript & " -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite"


            ' PDO: Passwort-Phrase, with Ghostscript only RC4 possible...
            If ((strUserPassword <> "") And (strOwnerPassword <> "")) Then
                strCommand = strCommand & " -sOwnerPassword=" & strOwnerPassword & " -sUserPassword=" & strUserPassword & " -dCompatibilityLevel=2.0"
            End If

          strCommand = strCommand & " -sOutputFile=" & Chr(34)

          strCommand = strCommand & strZielOrdner & "\"   'PDO: Danach kommt die Zieldatei und die einzelnen, anzubindenden Dateien.




          strTargetFile_without_Path = "Beratungsprotokoll_2018_Sammel.pdf"
          strFile_with_Path = strFile_for_pdf


               ' PDO: Gesamtcommand pt togehter ad executed
                strCommand = strCommand & strTargetFile_without_Path & Chr(34) & strFile_with_Path
                Debug.Print strCommand

                Set WshShell = CreateObject("WScript.Shell")
                WshShell.Run strCommand, 0, True
                Set WshShell = Nothing


            fctPDO_Print_pdf_GhostScript = strZielOrdner & "\" & strTargetFile_without_Path


        ' Cleanup:
Err_Handler:

          Set fso = Nothing
          MsgBox "Done"


        End Function

【讨论】:

  • 感谢您的回复,尼克它帮了我很多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 2015-03-18
相关资源
最近更新 更多