Tips & Tricks

Slimming PDF bookmarks with JavaScript

February 02, 2005

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.

Note: Although I originally wrote this to be executed via a custom tool button using ARTS PDF Aerialist, you can also execute it from Acrobat Professional's JavaScript debugger.

// 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); 

}

TrimBookmarks(this.bookmarkRoot, 0);

// 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);
}
Related Products at PDF Store

Nitro PDF Professional

Nitro PDF Professional, your PDF creation and editing product. Priced at $99, Nitro PDF Pro is the m... View full product details
Download free demo

ARTS PDF Split & Merge Lite

The easiest way to split and merge PDFs! It provides a simpler method of splitting and merging your ... View full product details
Download free demo

ARTS PDF Aerialist

Take Acrobat to the next level with advanced splitting and merging; flexible bookmark creation and m... View full product details
Download free demo

PDF In-Depth Free Product Trials Ubiquitous PDF

Nitro PDF Professional

the perfect PDF product for business and enterprise, combining an extremely competitive price with a...

Download free demo

XpdfViewer

This ActiveX control (OCX) provides a PDF file viewer component, enabling developers to add PDF viewing...

Download free demo

Ubiquitous PDF: PDF eBooks-Library

If you are looking for a good store of PDF content, you could do a lot worse than visiting eBooks-Library.com...

September 03, 2009
Search Planet PDF
more searching options...







Download PDF Creator

Download The Best of Planet PDF volume 2
Planet PDF Newsletter
Most Popluar Articles
Features

Collating PDFs using JavaScript

Despite the numerous benefits, there can be potential issues with the conversion of paper documents into electronic archives. When scanning paper pages into PDF, it's possible to end up with the odd- and even-numbered pages in separate PDF files. It can be very time-consuming to collate them manually, but there is an easier way. Sean Stewart explains.

Featured Product

BCL easyPDF SDK

BCL easyPDF SDK is a set of PDF Programming Libraries designed specifically to help Software Developers / Programmers build and deploy enterprise class PDF applications for corporate wide PDF...

Platinum Sponsor
Create & Edit PDF - Nitro PDF Software

ARTS PDF

Silver Sponsors

PDF-Tools enfocus

QuickPDF: The Unrivaled PDF Developer Toolkit