Sample of the Week:
Last month I wrote a series of articles that took us “Back to Basics” where I described how to perform common modifications to PDF files in the same way that Acrobat does. August 2015 I’m taking you “Beyond Acrobat”; this whole month I’ll be posting articles that describe how to do things to PDF with the Datalogics PDF Java Toolkit that Acrobat doesn’t… at least not without 3rd party plug-ins. The first of these is creating spreads.
Users can set up Acrobat so that it displays documents that were created as single pages as spreads instead. However, this just changes the way the document is presented. As you can see in the image below, the pages panel reveals that the spreads are still stored in the PDF as separate pages; you can even see the dotted lines that Acrobat adds to show where the page separation is. However, Acrobat does not have a feature that would let you “stitch” the separate pages from the spread into a single double-wide page… and there may be times when that’s exactly what you want.
The Datalogics PDF Java Toolkit combining multiple source pages to create a single page pretty simple though. The PMMService class is used to supports manipulations on a PDF document; you can use it to append pages to the end of a document, insert pages between other pages, delete pages, and overlay pages. It can also manipulate layers, bookmarks, links, and other annotations. It’s the overlay functionality that we’re going to take advantage of.
In order to assemble the spreads, we’re going to need to map all of the content from two separate pages in the source document and add them to the content of a single page in the target document… repeatedly… but we don’t want them to simply overlay each other, we need to be able to shift the origin point for one of the pages so that they appear side by side. As it turns out, the PMMService class helps us do this as well. As parameters, you can pass in a PMMPagesMapping object which allows you to create a map from a source page to a single target page or a source page and an array of target pages. To shift the pages we need to be on the right side of the larger page, we only need to invoke the PMMOptions class to translate the coordinates of oregin of the page overlay. The PMMService class then automatically manages all of the PDF page content and resources making it extremely easy to do otherwise incredibly complicated PDF manipulations in a single line of code.
The Gist referenced in the post begins by simply creating a new target document and copying the first page of the source document. If you think of a folded booklet, this makes sense; the cover isn’t a spread.
We assume that all of the pages are the same size, so our spreads will be twice as wide as any one of the pages in the source document. We use the source page dimensions to calculate the new spreads that we’re going to use for each of the internal pages.
Then we loop through all but the last page of the source document, assembling the spreads as we go; placing the left side at the original coordinates and shifting the right side to the right by the width of the left side. Finally, adding the new spread to the target document.
Now we just add the last page in the same way as we added the first. This would be the back of the booklet.
Once the file is saved, you should get something that displays like the image below in Acrobat. You can see from the pages panel on the left that the individual pages have been merged.
This input file in particular demonstrates the level of precision that you can achieve with the Datalogics PDF Java Toolkit you can zoom into the center “seam” where the two pages are stitched together where the photo assembled from two page backgrounds appears to be perfect and continuous across the spread. Now that’s impressive!