Updating PDF Bookmarks Quickly

In another last bookmark-related tip we showed you how to change the properties for bookmarks one by one. This can be somewhat impractical when you have hundreds or thousands of bookmarks.

If we use a little bit of JavaScript we can whizz round all the bookmarks in your PDF (or PDF’s) and set the properties for all the bookmarks in your documents.

  1. Select ‘Document Javascripts…’ from ‘Tools>Javascript’ menu.
  2. Type a name for the script (anything you can remember) in the script name text area.
  3. Click ‘Add’, then Click ‘Edit’
  4. Copy the javascript code below into the Edit Window.
function ChangeBookmarkColors(bm, nLevel)
// !!!!! Change 3: Add Conditional Options under this line
var s = '';
for (var i = 0; i < nLevel; i++)
s += ' ';

// !!!!! Change 1: This line here with other options.
bm.color = ['RGB',0,0,1];

if (bm.children != null)
for (var i = 0; i < bm.children.length; i++)
ChangeBookmarkColors(bm.children[i], nLevel + 1);

// Start the Program
// !!!!! Change 2: this.bookmarkRoot to alternative
ChangeBookmarkColors(this.bookmarkRoot, 0);

When you click the OK button you will be taken back to this screen. If you now choose the Close button Acrobat will activate the selected Javascript on the active document.

You should find all of you bookmarks have been colored blue. ([‘RGB’,0,0,1]).


Instead of just changing the color, try replacing (or adding to) the line indicated with the exclamation marks:

  • To Set Styles:

    • bm.style = bkmrkStyle;
        Where bkmrkStyle is one of the following values:

      • 0 = Plain,
      • 1 = Italic,
      • 2 = Bold and
      • 3 = Bold&Italic
      • For example to get a blue, bold&italic bookmark:
bm.style = 3;
bm.color = ['RGB',0,0,1]

Use a differerent Color Model (‘CYMK’, ‘G’)

  • Instead of using the Red, Green and Blue color model we can use the Cyan, Magenta, Yellow and Black color model
bm.color = ['CYMK',0,1,1,0] 

Start at a Different Bookmark

  • If you don’t want to apply your style changes to all of the bookmarks, then change the ‘this.bookmarkRoot’ at the line indicated (Change 2) to one of the following:

    • this.bookmarkRoot.children[0].children[xxx]; where xxx is the bookmark index number in the tree (see console output for the index number in [] square brackets. Example:


  • The above line would change the second bookmark (the first bookmark is index 0, not index 1) and all of it’s children (if it had any).

Use a Condition

  • At Change postion 3, use a condition to test if the bookmark should be styled or not, for example check the name of the bookmark or check if it has any child bookmarks:
if(bm.name.indexOf('Acrobat') == -1) return; 

You May Also Like

About the Author: Dave Wraight

Leave a Reply