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

This post is more than 2 years old.

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 senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

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

Archived Comments

Comment 1 by Mike Collins posted on 4/11/2011 at 5:02 PM

This works great, but does not work with XFA pdfs. So if you created the pdf via LC Designer you will need to find another way, or transform them if you own LC.

Comment 2 by Mike Collins posted on 4/12/2011 at 5:01 PM

Commented to soon,
<cfpdf action="write" source="pdetail_static.pdf" destination="pdetail_flat.pdf" flatten="yes" overwrite="true">

However - I also needed to save the PDF from inside LC Designer as a PDF Static and not dynamic.

Check out this doc for more on the difference:

I am now also getting a popup when I opened the PDF telling me a need a newer version of Acrobat. Not sure how to get around that one yet.

Also check out http://old.nabble.com/CFPDF...

for a workaround for page numbering after a merge. Fix is due for CF10.
Using the flatten prior to the merge works!!

Comment 3 by Raymond Camden posted on 4/12/2011 at 5:03 PM

Thanks for the updates Mike!

Comment 4 by Reuben Brown posted on 4/15/2011 at 6:26 PM

Ray - Great work on putting these guides together for PDF creation and manipulation. It helped me get a project working with a full ToC and nested bookmarks. I so wish that ACF had a create ToC function for CFPDF, or even an option when merging documents to create it. Maybe in the next version?

Comment 5 by Raymond Camden posted on 4/19/2011 at 3:15 PM

@Reuben: Remember you can always make a request like this here:


No guarantee Adobe will add it - but you should at least request it.

Comment 6 by Reuben Brown posted on 4/19/2011 at 5:19 PM

Maybe I should help the Railo and OpenBD guys implement it in their CFPDF implementations ;)

Comment 7 by Michael Cummins posted on 11/19/2013 at 11:12 PM

Some PDFs I download open the bookmark toolbar by default, while the PDFs I create in ColdFusion do not. Is there a way to force open the bookmark toolbar when I create my PDF from ColdFusion? It's apparently a property of the document.


Comment 8 by Raymond Camden posted on 11/20/2013 at 1:54 AM

Going by memory here as I'm on the road- but I believe this can be done via DDX. There is a way to set the opening state of the PDF. Do some research into CF and DDX (I have some posts here) and you may have success.