Sample of the Week:
It’s not as simple as stapling two piles of paper together. Combining PDF files can be a tricky business. When assembling a new PDF file from a series of separate PDF files, it’s pretty obvious what to do with the page content, links, and annotations. What to do with things like bookmarks, fields, layers, and metadata is less so. While there is no “correct” answer (it really depends on what you want from the merged file) the Datalogics PDF Java Toolkit makes it easy to programmatically imitate the behavior of Adobe Acrobat while giving developers the flexibility to diverge from that if they need to.
When combining multiple files into a single PDF with Acrobat, the page content, links, annotations, and form field widgets will be copied over to the new file. Any form fields that have the same name in the merged documents are merged into one field and their values will all be the same.
Additionally, when Acrobat merges the bookmarks from the separate files, it creates a new top level bookmark using the base file name as the label and then puts the inserted document bookmarks under it. That way the user can navigate the sections of the document without bookmark collisions caused by the process of combining the documents. The destinations of the bookmarks are then resolved to point to the correct page in the new file… all automatically.
We created this Gist to demonstrate how to imitate the behavior of Acrobat when the Datalogics PDF Java Toolkit is used to merge PDF files. The Gist uses the insertPages() method of the PMMService to append the pages of three separate files to a new blank PDF file and uses the base file name as the title of the new bookmark that wraps the bookmarks in the source document before it is added after the last bookmark in the new, merged, document.
To try it out for yourself, download the Gist then get all the other samples and documentation by requesting an evaluation of the Datalogics PDF Java Toolkit.
You’ll also want to download this .zip file containing a set of input files that are used in this Gist.