Skip to main content
May 30, 2017
Answered

How to create a bookmark tree using app.execmenu("NewBookmark")

  • May 30, 2017
  • 2 replies
  • 1872 views

Hi,

I wanted to create a bookmark tree using app.execMenu("NewBookmark").

In top level I can able to create the bookmark with name.

If I wanted to create a bookmark tree with many childs in many level I am unable to create the bookmark.  can any one help me on this?

the output should be

1st child(1st level)

     aaa(2nd level)

     bbb(2nd level)

     ccc(2nd level)

2nd child(1st level)

     ddd(2nd level)

     eee(2nd level)

     fff(2nd level)


Thank you

Ariv

This topic has been closed for replies.
Correct answer Thom Parker

I've created many tools that use the "NewBookmark" menu item to create bookmarks. It is the only way from JS to create bookmarks with destinations. But it is tricky because of timing issues. The bookmark is not created synchronously when "app.execMenuItem" is called. The menu item execution happens asynchronously. So the newly created menu item may not be available to JS code for an unknown amount of time. So it's best not to look for then new item right after creating it. The technique described below handles this issue by creating and manipulating the bookmarks in different loops. Not perfect but it usually works.

To create the bookmark tree with JavaScript you'll need to know the pattern of the tree up front. The most important bit is the pages that need bookmarks. As a simple example, you could use an array, where each entry in the array is another array containing the zero based page number and a name. like this:  var BKData = [ [0,"title page"], [1, "TOC"], [5, "intro"] ]

The code should work as follows:

1. Loop on array to create a flat set of unnamed bookmarks

  for(var i=0;i<BKData.length;i++)

  {

      this.pageNum = BKData[0];

      app.execMenuItem("NewBookmark",this);

}

2. Loop on bookmarks to set names

  var bkMarks = this.bookmarkRoot.children;

  for(var i=0;i<bkMarks.length;i++)

  {

      bkMarks.name = BKData[1];

  }

The "NewBookmark" menu item creates the new bookmark at the bookmark with the current focus. The assumption in this code is that there are no bookmarks initially, so the new bookmarks are created from the top in order in which they appear in the "BKData" array. And they are the only bookmarks in the document.

You can make this much fancier by putting hierarchy data, such as a level number, into the "BKData", Then adding a 3rd loop to sort the bookmarks into the correct hierarchy.

2 replies

Thom Parker
Thom ParkerCorrect answer
Community Expert
May 30, 2017

I've created many tools that use the "NewBookmark" menu item to create bookmarks. It is the only way from JS to create bookmarks with destinations. But it is tricky because of timing issues. The bookmark is not created synchronously when "app.execMenuItem" is called. The menu item execution happens asynchronously. So the newly created menu item may not be available to JS code for an unknown amount of time. So it's best not to look for then new item right after creating it. The technique described below handles this issue by creating and manipulating the bookmarks in different loops. Not perfect but it usually works.

To create the bookmark tree with JavaScript you'll need to know the pattern of the tree up front. The most important bit is the pages that need bookmarks. As a simple example, you could use an array, where each entry in the array is another array containing the zero based page number and a name. like this:  var BKData = [ [0,"title page"], [1, "TOC"], [5, "intro"] ]

The code should work as follows:

1. Loop on array to create a flat set of unnamed bookmarks

  for(var i=0;i<BKData.length;i++)

  {

      this.pageNum = BKData[0];

      app.execMenuItem("NewBookmark",this);

}

2. Loop on bookmarks to set names

  var bkMarks = this.bookmarkRoot.children;

  for(var i=0;i<bkMarks.length;i++)

  {

      bkMarks.name = BKData[1];

  }

The "NewBookmark" menu item creates the new bookmark at the bookmark with the current focus. The assumption in this code is that there are no bookmarks initially, so the new bookmarks are created from the top in order in which they appear in the "BKData" array. And they are the only bookmarks in the document.

You can make this much fancier by putting hierarchy data, such as a level number, into the "BKData", Then adding a 3rd loop to sort the bookmarks into the correct hierarchy.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
May 30, 2017

Yes, I actually thought about it and realized after posting that it's probably possible to do it by creating the bookmark using NewBookmark and then renaming and moving them around to create a hierarchy... It's sounds like a bit of a mess to write such a code, though.

Thom Parker
Community Expert
May 30, 2017

try67  wrote

.. It's sounds like a bit of a mess to write such a code, though.

Compared to what? writing a plug-in?  If you can get away with a funky JS solution, then it is a better solution than creating a plug-in for the simple reason that it's fast, easy, and will work across platforms, and even in many 3rd party tools (except when menu items are used). That is not to say that I don't create a lot of single purpose plug-ins for filling in the holes in the JS model. But you have to be very careful for exactly the same reasons I've mentioned above, i.e. Plug-ins activated from JS operate asynchronously. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
May 30, 2017

That's not a good way of creating new bookmarks using a script.

Use the createChild or insertChild method of the Bookmark object, instead.

May 30, 2017

Thank you for your response.

But I don't want to display this.pageNum=I,2,3, etc in properties page.  Any solutions is there?  I need to send this to client

try67
Community Expert
May 30, 2017

No, there's no solution within Acrobat to this issue, unless you create the bookmarks using a plugin.

Another option is to use a script to create the bookmarks and then convert their actions using a standalone tool from executing JS code to a "Go to a page-view" action. I've developed such a tool, and you can find it here: Custom-made Adobe Scripts: Convert JavaScript Links To Real Links