Merging program files with software while maintaining the structure of the "Merge Files ..." bookmark?

I originally asked this on the Adobe forums, but still got the answers.

I need to combine many (100+) PDF files into a single report on a weekly basis, and so far I have been doing the process manually, selecting files, right-clicking and selecting “Merge supported files into Acrobat.” What I would like to do is to repeat this exact process programmatically (preferably in Excel / VBA, but C # or command commands are acceptable alternatives.) I currently have code that will combine PDF files, but it does not preserve the bookmark structure in the same way how "Merge supported files in Acrobat" does.

In other words, let's say I have three files called "A.pdf", "B.pdf" and "C.pdf", and each file contains two bookmarks called "Bkmrk 1" and "Bkmrk 2". I want to programmatically combine these three files into a single file with 9 bookmarks that look like the structure below:

A
    Bkmrk 1
    Bkmrk 2
B
    Bkmrk 1
    Bkmrk 2
C
    Bkmrk 1
    Bkmrk 2

I first tried to automate the process using the Acrobat SDK, but from what I understand, the Acrobat SDK does not allow programs to interact with the dialog box that appears when the menu option "Combined Files" is executed, so that Work. I also tried the ability to programmatically insert pages from one PDF file into another, but this does not create the bookmark structure I am looking for and does not allow me to manipulate the bookmark hierarchy to create the bookmark structure I am looking for.

- , ? !

+5
5

, Aspose.Pdf.Kit, . .

30- , , , .

+1

, , , . , , , :

Private mlngBkmkCounter     As Long

Public Sub updfConcatenate(pvarFromPaths As Variant, _
                           pstrToPath As String)

    Dim origPdfDoc      As Acrobat.CAcroPDDoc
    Dim newPdfDoc       As Acrobat.CAcroPDDoc
    Dim lngNewPageCount As Long
    Dim lngInsertPage   As Long
    Dim i               As Long

    Set origPdfDoc = CreateObject("AcroExch.PDDoc")
    Set newPdfDoc = CreateObject("AcroExch.PDDoc")
    mlngBkmkCounter = 0

    'set the first file in the array as the "new"'
    If newPdfDoc.Open(pvarFromPaths(LBound(pvarFromPaths))) = True Then
        updfInsertBookmark "Test Start", lngInsertPage, , newPdfDoc
        mlngBkmkCounter = 1

        For i = LBound(pvarFromPaths) + 1 To UBound(pvarFromPaths)
            Application.StatusBar = "Merging " & pvarFromPaths(i) & "..."
            If origPdfDoc.Open(pvarFromPaths(i)) = True Then
                lngInsertPage = newPdfDoc.GetNumPages
                newPdfDoc.InsertPages lngInsertPage - 1, origPdfDoc, 0, origPdfDoc.GetNumPages, False
                updfInsertBookmark "Test " & i, lngInsertPage, , newPdfDoc
                origPdfDoc.Close
                mlngBkmkCounter = mlngBkmkCounter + 1
            End If
        Next i
        newPdfDoc.Save PDSaveFull, pstrToPath
    End If

ExitHere:
    Set origPdfDoc = Nothing
    Set newPdfDoc = Nothing
    Application.StatusBar = False
    Exit Sub

End Sub

-... ,

Public Sub updfInsertBookmark(pstrCaption As String, _
                              plngPage As Long, _
                     Optional pstrPath As String, _
                     Optional pMyPDDoc As Acrobat.CAcroPDDoc, _
                     Optional plngIndex As Long = -1, _
                     Optional plngParentIndex As Long = -1)

    Dim MyPDDoc         As Acrobat.CAcroPDDoc
    Dim jso             As Object
    Dim BMR             As Object
    Dim arrParents      As Variant
    Dim bkmChildsParent As Object
    Dim bleContinue     As Boolean
    Dim bleSave         As Boolean
    Dim lngIndex        As Long

    If pMyPDDoc Is Nothing Then
        Set MyPDDoc = CreateObject("AcroExch.PDDoc")
        bleContinue = MyPDDoc.Open(pstrPath)
        bleSave = True
    Else
        Set MyPDDoc = pMyPDDoc
        bleContinue = True
    End If

    If plngIndex > -1 Then
        lngIndex = plngIndex
    Else
        lngIndex = mlngBkmkCounter
    End If

    If bleContinue = True Then
        Set jso = MyPDDoc.GetJSObject
        Set BMR = jso.bookmarkRoot

        If plngParentIndex > -1 Then
            arrParents = jso.bookmarkRoot.Children
            Set bkmChildsParent = arrParents(plngParentIndex)
            bkmChildsParent.createchild pstrCaption, "this.pageNum= " & plngPage, lngIndex

        Else
            BMR.createchild pstrCaption, "this.pageNum= " & plngPage, lngIndex
        End If

        MyPDDoc.SetPageMode 3 '3 — display using bookmarks'

        If bleSave = True Then
            MyPDDoc.Save PDSaveIncremental, pstrPath
            MyPDDoc.Close
         End If
    End If

ExitHere:
    Set jso = Nothing
    Set BMR = Nothing
    Set arrParents = Nothing
    Set bkmChildsParent = Nothing
    Set MyPDDoc = Nothing
End Sub

:

Public Sub uTest_pdfConcatenate()

    Const cPath As String = "C:\MyPath\"

    updfConcatenate Array(cPath & "Test1.pdf", _
                          cPath & "Test2.pdf", _
                          cPath & "Test3.pdf"), "C:\Temp\TestOut.pdf"
End Sub
+1

iText # (http://www.itextpdf.com/). imho - PDF-. , (), http://java-x.blogspot.com/2006/11/merge-pdf-files-with-itext.html , Java, .NET.

HTH

+1

Docotic.Pdf PDF ().

There is nothing special. You just add all the documents one by one and all that.

using (PdfDocument pdf = new PdfDocument())
{
    string[] filesToMerge = ...
    foreach (string file in filesToMerge)
        pdf.Append(file);

    pdf.Save("merged.pdf");
}

Disclaimer: I work for Bit Miracle, a library provider.

+1
source

Acrobat SDK allows you to create and read bookmarks. Check out the SDK API link for:

PDDocGetBookmarkRoot()

PDBookmark* (AddChild, AddNewChild, GetNext, GetPrev... lots of functions in there)

If the Combine Files dialog box does not give you the control you need, create your own dialog box.

0
source

All Articles