如何使用 VBA 连接 .pdf 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9488690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:23:01  来源:igfitidea点击:

How to concatenate .pdf files with VBA?

vbapdf

提问by vasek1

I am trying to concatenate .pdf files with VBA. Nothing fancy, literally sticking the pages one after each other. I did numerous web searches but was unable to find any solutions that are even close to working. Has anyone done this before? Thanks!

我正在尝试使用 VBA 连接 .pdf 文件。没什么特别的,字面上一页一页地贴着。我做了大量的网络搜索,但无法找到任何接近工作的解决方案。以前有人这样做过吗?谢谢!

回答by yms

If a GPL library is a valid option for you, you could use ghostscript as proposed in this SO question. You can do this by calling the ShellExecutefunction from Windows API or by using the class WScript.Shellif you are creating a vbscript file.

如果 GPL 库对你来说是一个有效的选择,你可以使用在这个 SO question 中建议的 ghostscript 。您可以通过从 Windows API调用ShellExecute函数或WScript.Shell在创建 vbscript 文件时使用该类来完成此操作。

If a commercial library is an option, I recommend Amyuni PDF Creator ActiveXor Amyuni PDF Converter, both have an Appendfunction that will do the work. The code for Amyuni PDF Converter for example would look like this:

如果可以选择商业库,我推荐Amyuni PDF Creator ActiveXAmyuni PDF Converter,两者都有一个Append功能可以完成这项工作。例如,Amyuni PDF Converter 的代码如下所示:

Set PDFDoc = CreateObject("CDintfEx.Document.4.5")
PDFdoc.SetLicenseKey "your company", "your license code"
PDFDoc.Open "test_append1.pdf"
PDFDoc.Append "test_append2.pdf"
PDFDoc.Save "result_append.pdf"
Set PDFdoc = Nothing

Usual disclaimer applies for the latest suggestion

通常的免责声明适用于最新的建议

回答by Nick Oetjen

I found this topic while having the same task, with a customer using amyuni. Thanks to yms for a good approach. I found Acces crashing on "Set PDFdoc = Nothing". This one works fine for me:

我在执行相同任务时发现了这个主题,客户使用 amyuni。感谢 yms 提供了一个很好的方法。我发现 Access 在“Set PDFdoc = Nothing”上崩溃。这个对我来说很好用:

Public Sub fctPDO_Concatenate_pdf_with_Amyuni_Document_6_0()

' PDO: Usage of .append: Crashes on destruction of pdfdoc-Object. pdf-file is created properly. But usind .append , MS Access crashes - without it's okay.

'       Solution: Build second pdfdoc2 - object , and concatenate using  .AppendEx(Object)   .


On Error Resume Next
Dim PDFdoc As Object
Dim PDFdoc2 As Object

Const strLibraryVersion As String = "CDintfEx.Document.6.0"
' PDO: Examples
'Set PDFdoc = CreateObject("CDintfEx.Document.6.0")     ' PDO: See Object catalog
'Set PDFdoc = CreateObject("CDintfEx.Document")         ' PDO: Not sufficient w/o version
'Set PDFdoc = CreateObject("CDintfEx.Document.4.5")     ' PDO: Older version

Set PDFdoc = CreateObject(strLibraryVersion)
Set PDFdoc2 = CreateObject(strLibraryVersion)

'PDO: Open first file
PDFdoc.Open "D:\PDO_test\Beratungsprotokoll_2018.pdf"


'PDFdoc.Append "D:\PDO_test\GV_0093Z0_Einzelantrag.pdf"   ' PDO: Crashes on   set PDFdoc = nothing

' PDO: Open and append second file (as Object, not as file)
PDFdoc2.Open "D:\PDO_test\GV_0093Z0_Einzelantrag.pdf"
PDFdoc.AppendEx PDFdoc2

' PDO: Open and append third file (as Object, not as file). Re-use of second Object possible
PDFdoc2.Open "D:\PDO_test\result_append_sammel.pdf"
PDFdoc.AppendEx PDFdoc2


'PDO: Save with a new name
PDFdoc.Save "D:\PDO_test\result_append_sammelsammel.pdf"

'PDFdoc.Close => Not existing.

Set PDFdoc = Nothing  '=> Access crashed, with PDFdoc.Append 
Set PDFdoc2 = Nothing

Debug.Print "Done: " & Now() & " Error: " & Err.Number

End Sub

If you prefer Ghostscript you can use a single line:

如果您更喜欢 Ghostscript,则可以使用一行:

C:\PROGRA~2\gs\gs9.19\bin\gswin32c.exe -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOwnerPassword=pass2 -sUserPassword=pass1 -dCompatibilityLevel=2.0 -sOutputFile="D:\PDO_test\Beratungsprotokoll_2018_Sammel.pdf" "D:\PDO_test\Beratungsprotokoll_2018.pdf" "D:\PDO_test\GV_0093Z0_Einzelantrag.pdf" 

This concatenates the two latter files into the (new) first one and applies a password (see security details before applying). The short path can be obtained with a FileScripting Object "fso" using

这会将后两个文件连接到(新的)第一个文件中并应用密码(在应用之前请参阅安全详细信息)。可以使用 FileScripting 对象“fso”获得短路径

fso.GetFolder(strFolder).ShortPath

回答by Klaas Hoppe

I run sedja-console and add my pdf's as parameters. Quite easy to implement. Do not forget to check before starting Sedja-console if the readonly flag of the possible previous created destination pdf isn't set to yes, as there is no feedback of this external process.

我运行 sedja-console 并添加我的 pdf 作为参数。很容易实现。不要忘记在启动 Sedja-console 之前检查可能之前创建的目标 pdf 的只读标志是否未设置为 yes,因为没有此外部过程的反馈。