Slimming PDF bookmarks with JavaScript

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)
{ = RTrim(;
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’.

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.

You May Also Like

About the Author: Karl De Abrew

Leave a Reply