需要为从MS Access导出PDF文件设置密码

问题描述 投票:0回答:1

[请找到我在MS Access中使用的以下代码我需要添加用于提取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"
vba forms ms-access pdf ms-access-2016
1个回答
0
投票

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

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
© www.soinside.com 2019 - 2024. All rights reserved.