如何为PDF文件导出设置密码? [英] How to set a password for PDF file export?

查看:55
本文介绍了如何为PDF文件导出设置密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加密码才能提取 PDF.

I need to add a password for extracting 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"

推荐答案

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

Here's a solution using Ghostscript. First print your pdf like you already do, then call the function below. Ghostscript is free under license agreement, 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

这篇关于如何为PDF文件导出设置密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆