1

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!

vasek1
  • 13,541
  • 11
  • 32
  • 36
  • by the way, are you trying to put one page *after* the other or one page *on top of* the other? – yms Feb 28 '12 at 20:28
  • @yms: [concatenate](http://dictionary.reference.com/browse/concatenate), [superpose](http://dictionary.reference.com/browse/concatenate) – Jean-François Corbett Feb 28 '12 at 20:42
  • @jean he also said "sticking the pages on top of each other", that is why I asked for clarification.Also, as a developer of pdf-related products, I have seen all those terms being used for any scenario without distinction – yms Feb 28 '12 at 20:46
  • sorry, I mean "one after the other". I edited the question to make it more clear. – vasek1 Feb 28 '12 at 20:51

3 Answers3

2

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

Community
  • 1
  • 1
yms
  • 10,361
  • 3
  • 38
  • 68
1

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
Nick Oetjen
  • 129
  • 4
0

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.