Tips & Tricks

Slimming PDF bookmarks with JavaScript

June 26, 2012

Advertisement
Advertisement
 

PDF bookmarks are a must for the usability and navigability of longer documents. Without them, the browsing process can be quite painful -- especially when you know that you need to skim to that crucial chapter when time is of the essence.

When these bookmarks are created programmatically, they are based on a template created using a combination of user settings and the content of the source document or web page. Since not all such documents were created with PDF bookmarks in mind, cleaning up the resulting bookmark titles can become an important part of the QA process.

Luckily, JavaScript can vastly simplify the process. Using the sample code below, you can iterate through a document's bookmarks, renaming them and trimming off any trailing white space. As always, this code can either be used "as-is" to reproduce the process in its entirety or mixed and matched with other code samples to perform different functions. For instance, one such modification could involve getting the JavaScript to go through and print the bookmark names to create a report.

We'll do this in two step process - firstly: we'll add the 'working functions' as document level JavaScripts - let's call them "TEMP_Trim_Bookmarks"; and secondly we'll execute them using the JavaScript console.

To add a document level JavaScript you need to:

  • Select View, Tools, JavaScript.
  • Select Document JavaScripts from the JavaScripts Panel of the application window.
  • Enter "TEMP_Trim_Bookmarks" as the name of the new script and click the 'Add' button.
  • Delete any text already in the edit window and copy/paste the script below.
  • Click OK to close the Script editor.
  • Click Close to close the Document JavaScript manager.
  • You have now added the 'working functions' and will be able to execute these using the JavaScript console.
// Trim any white space at the end of bookmarks
function TrimBookmarks(bm, nLevel)
{
	bm.name = RTrim(bm.name);
	console.println(bm.name);
	if (bm.children != null)
		for (var i = 0; i < bm.children.length; i++)
			 TrimBookmarks(bm.children[i], nLevel + 1); 

}

// Trims spaces at the end of a string
function RTrim(sString) 
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;var test = String.fromCharCode(144);
}

... The JavaScript panel should still be open on the right hand side of the Acrobat window, click on JavaScript Debugger. Now copy and paste the following function into the JavaScript console and voilah! This will call the document level functions that you added just moments ago. Awesome!


TrimBookmarks(this.bookmarkRoot, 0);

A quick inspection will reveal that the whitespace on the right hand side of each bookmark has been removed. You should remove the document JavaScript at this point - you'll find it easy to find, remember you called it "TEMP_Trim_Bookmarks".

Note: Originally written by ARTS PDF Product Manager Maryanne Montes in early 2005. This tip was designed to be executed via a single custom tool button using Debenu PDF Aerialist (formerly ARTS PDF Aerialist) but has been updated for general usage using Acrobat Professional X.

PDF In-Depth Free Product Trials Ubiquitous PDF

Debenu Quick PDF Library

Get products to market faster with this amazing PDF developer SDK. Over 900 functions and an equally...

Download free demo

PDF Master Series III: Eugene Y. Xiong talks with Planet PDF

Planet PDF talks with another Master of the PDF Universe, Eugene Y. Xiong, Founder and Chairman of the Board at Foxit Software Inc. in Fremont California. Xiong is a quiet yet astounding achiever, you (usually) won't find him talking at conferences, exhibits, or publishings, but what you will find is the result of his leadership in places you would never expect.

September 14, 2016
Platinum Sponsor



Search Planet PDF
more searching options...
Planet PDF Newsletter
Most Popular Articles
Featured Product

Debenu PDF Aerialist 12

The ultimate plug-in for Adobe Acrobat. Advanced splitting, merging, stamping, bookmarking, and link control. Take Acrobat to the next level.

Features

Adding a PDF Stamp Comment

OK, so you want to stamp your document. Maybe you need to give reviewers some advice about the document's status or sensitivity. This tip from author Ted Padova demonstrates how to add stamps with the Stamp Tool along with related comments.