Extracting bookmarks from a PDF with ColdFusion

Sid asked:

Hey Ray is there any code using jquery/coldfusion/java, a way we can extract the bookmarks in a PDF.

Via CFPDF, this is not possible. As you know, CFPDF supports DDX, which talks to an embedded LCDS instance. But the DDX support is a bit limited. That's not terrible considering how much LCSD costs. One thing I'm fairly sure LCDS can do is extract bookmarks, but this is not possible within ColdFusion. On a whim though I decided to look at iText. I blogged about this a few weeks ago and thought I'd check to see if it was possible. Turns out - it was incredibly easy. Check it out:

<cfset reader = CreateObject("java", "com.lowagie.text.pdf.PdfReader").init("C:\Users\Raymond\Documents\My Dropbox\ColdFusion\coldfusion_9_admin.pdf")> <cfset simpleBookmark = createObject("java","com.lowagie.text.pdf.SimpleBookmark")> <cfset bookmarks = simpleBookmark.getBookmark(reader)> <cfif isNull(bookmarks)> <cfoutput> No bookmarks. </cfoutput> <cfabort> </cfif> <cfset iterator = bookmarks.listIterator()> <cfloop condition="iterator.hasNext()"> <cfset bm = iterator.next()> <cfdump var="#bm#"> </cfloop>

As with my previous example, I begin by creating a 'reader' object pointing to my PDF. At that point I created an instance of iText's SimpleBookmark object and called the getBookmark method. And that's it. The results are Hashmaps which cfdump handles for you. If you want to use the keys manually though use a get method like so:

<cfoutput>#bm.get("Title")#</cfoutput>

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