Quick Tip: Adding a bookmark to a PDF when it has none

A reader wrote in to me with an interesting question yesterday. He was using ColdFusion 9’s nice new ability to work with Office documents to create a merged PDF of various sources. His application let people drag and drop different documents into the browser (using HTML5) and then make use of cfdocument to convert them all into PDFs. Once done, all the PDFs would then be merged into one PDF.

This worked fine, but he ran into a problem when PDFs were part of the bunch of files that were to be merged. Obviously he didn't need to convert PDFs to PDFs, but because he wasn't using cfdocument to create the result, the PDF sources didn't have bookmarks. His final merged PDF would have a bookmark for all the bits except inner PDFs.

I double checked the docs and saw no way to add a bookmark to an existing PDF. You can't source cfdocument with a PDF. You can't do it with cfpdf either. Luckily though DDX once again came to the rescue. I noticed that the DDX example in the docs made use of the PDF tag to do a merge. (Which isn't necessary anymore since we can do merges with CFPDF.)

<?xml version="1.0" encoding="UTF-8"?> <DDX xmlns="http://ns.adobe.com/DDX/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ns.adobe.com/DDX/1.0/ coldfusion_ddx.xsd"> <PDF result="Out1"> <PDF source="Title"/> <TableOfContents/> <PDF source="Doc1"/> <PDF source="Doc2"/> <PDF source="Doc3"/> </PDF> </DDX>

On a whim I looked up the reference for the PDF tag in DDX and discovered that it includes a "bookmarkTitle" attribute. Here is a sample script I used:

<cfsavecontent variable="ddx"> <?xml version="1.0" encoding="UTF-8"?> <DDX xmlns="http://ns.adobe.com/DDX/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ns.adobe.com/DDX/1.0/ coldfusion_ddx.xsd"> <PDF result="Out1"> <PDF source="Doc1" bookmarkTitle="TESTING!!" /> </PDF> </DDX> </cfsavecontent>

And it worked like a charm. I took a random PDF as my source and when I checked the output, the "inner" PDF had a bookmark called "TESTING!!".

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments