How to merge/join/append/concatenate PDF files with WebSuperGoo ABCpdf
Approach 1:
Public Sub DoPDFAppend()
If SummaryFileName = "" Then Exit Sub
On Error GoTo ReportErrorHandler
'-------------------------------------------------------
' PDF $ Append OneFile_Bookmark_ByAccount_ByReport
'-------------------------------------------------------
Dim ii As Integer
Dim totalFileCount As Integer
totalFileCount = UBound(PDFFileArray) + 1
For ii = 0 To UBound(PDFFileArray)
Dim thisFileNumber As Integer
thisFileNumber = ii + 1
Status_update "Concatenating " & CStr(thisFileNumber) & " of " & CStr(totalFileCount), ""
'-------------------------------------------------------
' fetch currentSegmentFileName from array
'-------------------------------------------------------
Dim currentSegmentFileName As String
currentSegmentFileName = PDFFileArray(ii)
Dim myReportName As String
myReportName = ReportNameArray(ii)
'-------------------------------------------------------
' PDF Append OneFile_Bookmark_ByAccount_ByReport
'-------------------------------------------------------
Dim Account_num8 As String
Account_num8 = Left(PDFAcctArray(ii), 8)
Dim docSegment As New ABCpdf6.Doc
Dim docSummary As New ABCpdf6.Doc
'-------------------------------------------------------
' 1st segment?
'-------------------------------------------------------
Dim bookmarkPage As Integer
If ii = 0 Then
'-------------------------------------------------------
'yes, read first segment
'-------------------------------------------------------
docSegment.Read currentSegmentFileName
'-------------------------------------------------------
'append current segment to empty docSummary
'-------------------------------------------------------
docSummary.Append docSegment
bookmarkPage = 1
Else
'-------------------------------------------------------
'nth segment
'read current segment
'into docSegment
'-------------------------------------------------------
docSegment.Read currentSegmentFileName
'-------------------------------------------------------
'bookmarkPage = docSummary.pageCount
'e.g.
'if docSummary.pageCount = 10
' bookmarkPage = 11
' docSummary.page range = 1 - 10
'page 11 does not exist yet, but will exist after next append
'-------------------------------------------------------
bookmarkPage = docSummary.PageCount + 1
'-------------------------------------------------------
'append current segment to previous segment(s)
'-------------------------------------------------------
docSummary.Append docSegment
End If
'-------------------------------------------------------
'add bookmark to first page of current segment
'-------------------------------------------------------
docSummary.PageNumber = bookmarkPage
If prevAccountNumber <> Account_num8 Then
docSummary.AddBookmark Account_num8, True
Debug.Print ("-----" + Account_num8)
prevAccountNumber = Account_num8
docSummary.AddBookmark Account_num8 + "\" + myReportName, False
Debug.Print ("-----" + Account_num8 + "\" + myReportName)
Else
docSummary.AddBookmark Account_num8 + "\" + myReportName, False
Debug.Print ("- - -" + Account_num8 + "\" + myReportName)
End If
'-------------------------------------------------------
'set docSegment = nothing so file can be re-written from docSum
'then docSum save
'-------------------------------------------------------
Set docSegment = Nothing
'-------------------------------------------------------
'delete 1st or nth pdf Segment file
'-------------------------------------------------------
Kill (currentSegmentFileName)
Next 'currentSegmentFileName
'-------------------------------------------------------
'End PDF Append FileByReport_BookmarkByAccount
'-------------------------------------------------------
docSummary.Save (SummaryFileName)
Set docSummary = Nothing
Status_update "Done Concatenation to file: " + SummaryFileName, ""
Exit Sub
ReportErrorHandler:
MsgBox (Err.Description)
End Sub
Approach 2:
'-------------------------------------------------------
'PDF Append
'-------------------------------------------------------
Dim currentSegmentFileName As String
currentSegmentFileName = crxReport.ExportOptions.DiskFileName
Debug.Print ("***currentSegmentFileName =" + currentSegmentFileName)
Set crxReport = Nothing
'-------------------------------------------------------
' PDF Append OneFile_Bookmark_ByAccount_ByReport
'$ Append
'-------------------------------------------------------
Dim Account_num8 As String
Account_num8 = Left(Account_num, 8)
DoEvents
If NumOfRecs > 0 Then
'-------------------------------------------------------
'build SummaryFileName
'-------------------------------------------------------
'SummaryFileName = Output_Folder + Prev_Business_Date_Str + "_PDF_Summary_OneFile_Bookmark_ByAccount_ByReport_" + appendDate + "_" + appendTime + FileExt
'SummaryFileName = Output_Folder + Prev_Business_Date_Str + "_PDF_Summary_OneFile_Bookmark_ByAccount_ByReport" + FileExt
SummaryFileName = Output_Folder + Prev_Business_Date_Str + "_Reports_Summary_" + appendDate + FileExt
Debug.Print ("***SummaryFileName =" + SummaryFileName)
If prevAccountNumber = "" Then
On Error Resume Next
Kill SummaryFileName
On Error GoTo ReportErrorHandler
End If
Dim doc12 As New ABCpdf5.Doc
Dim doc22 As New ABCpdf5.Doc
Dim docSum2 As New ABCpdf5.Doc
'-------------------------------------------------------
'read previous segment(s)
'into doc 12
'(if not opened previously, error will occur)
'-------------------------------------------------------
On Error GoTo FileNeverCreatedInFirstPlace2
doc12.Read SummaryFileName
On Error GoTo ReportErrorHandler
Debug.Print ("doc12.PageCount: " + CStr(doc12.PageCount))
'-------------------------------------------------------
'append previous segment(s)
'into docSum2 where bookmarking happens)
'(since read was done from doc12, save done from docSum)
'-------------------------------------------------------
Debug.Print ("docSum2.PageCount before append: " + CStr(docSum2.PageCount))
DoEvents
docSum2.Append doc12
DoEvents
Debug.Print ("docSum2.PageCount after append: " + CStr(docSum2.PageCount))
Debug.Print ("doc12.PageCount: " + CStr(doc12.PageCount))
Debug.Print ("doc22.PageCount: " + CStr(doc22.PageCount))
'-------------------------------------------------------
'read current segment
'into doc 22
'-------------------------------------------------------
DoEvents
Debug.Print ("doc22.PageNumber: " + CStr(doc22.PageNumber))
doc22.Read currentSegmentFileName
Debug.Print ("doc22.PageNumber: " + CStr(doc22.PageNumber))
DoEvents
'-------------------------------------------------------
'bookmarkPage = docSum2.pageCount
'e.g.
'if docSum2.pageCount = 10
' bookmarkPage = 11
' docSum2.page range = 1 - 10
'page 11 does not exist yet, but will exist after next append
'-------------------------------------------------------
Dim bookmarkPage As Integer
bookmarkPage = docSum2.PageCount + 1
'-------------------------------------------------------
'append current segment to previous segment(s)
'-------------------------------------------------------
DoEvents
docSum2.Append doc22
DoEvents
'-------------------------------------------------------
'point to first page of current segment
'-------------------------------------------------------
docSum2.PageNumber = bookmarkPage
'-------------------------------------------------------
'add bookmark to first page of current segment
'-------------------------------------------------------
If prevAccountNumber <> Account_num8 Then
docSum2.AddBookmark Account_num8, True
Debug.Print ("-----" + Account_num8)
prevAccountNumber = Account_num8
docSum2.AddBookmark Account_num8 + "\" + Report_Output, False
Debug.Print ("-----" + Account_num8 + "\" + Report_Output)
Else
docSum2.AddBookmark Account_num8 + "\" + Report_Output, False
Debug.Print ("- - -" + Account_num8 + "\" + Report_Output)
End If
'-------------------------------------------------------
'set doc12 = nothing so file can be re-written from docSum
'then docSum save
'-------------------------------------------------------
Set doc12 = Nothing
Set doc22 = Nothing
docSum2.Save SummaryFileName
'-------------------------------------------------------
'done nth segment
'-------------------------------------------------------
Set docSum2 = Nothing
GoTo DoneAppend2
FileNeverCreatedInFirstPlace2:
'-------------------------------------------------------
'read first segment
'into doc 12
'then AddBookmark
'then save
'-------------------------------------------------------
On Error GoTo ReportErrorHandler
doc12.Read currentSegmentFileName
If prevAccountNumber <> Account_num8 Then
doc12.AddBookmark Account_num8, True
Debug.Print ("-----" + Account_num8)
prevAccountNumber = Account_num8
doc12.AddBookmark Account_num8 + "\" + Report_Output, True
Debug.Print ("-----" + Account_num8 + "\" + Report_Output)
Else
doc12.AddBookmark Account_num8 + "\" + Report_Output, True
Debug.Print ("- - -" + Account_num8 + "\" + Report_Output)
End If
doc12.Save SummaryFileName
'-------------------------------------------------------
'done 1st segment
'-------------------------------------------------------
Set doc12 = Nothing
Set doc22 = Nothing
Set docSum2 = Nothing
DoneAppend2:
End If
'-------------------------------------------------------
'End PDF Append FileByReport_BookmarkByAccount
'-------------------------------------------------------
'-------------------------------------------------------
'delete pdf Segment file
'-------------------------------------------------------
Kill currentSegmentFileName