Merging PDF Files Using the Datalogics PDF Java Toolkit

Merging PDF Files Using the Datalogics PDF Java Toolkit

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.

eval-nowThe PMMService class in the Datalogics PDF Java Toolkit has several methods that allow developers merge documents, manipulate and manage PDF page content and to deal with any repercussions of those manipulations; resolving link actions or managing layer options for example. There are methods that allow developers to decide exactly how form fields are handled, how layers (Optional Content Groups) are handled, how file attachments and JavaScripts are handled and just about anything else you can imagine. It’s defaults, however, are designed to function in much the same way as Acrobat.

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.

Leave a Reply

Your email address will not be published.