Reputation: 14071
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!
Upvotes: 1
Views: 7120
Reputation: 149
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:
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:
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
fso.GetFolder(strFolder).ShortPath
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 10418
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 ShellExecute function from Windows API or by using the class WScript.Shell
if you are creating a vbscript file.
If a commercial library is an option, I recommend Amyuni PDF Creator ActiveX or Amyuni PDF Converter, both have an Append
function that will do the work. The code for Amyuni PDF Converter for example would look like this:
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
Upvotes: 2