Skip to main content
Participating Frequently
March 13, 2018
Answered

Reorder bookmarks by their destination

  • March 13, 2018
  • 2 replies
  • 2432 views

Hi,

I'm wondering for a PDF doc whose bookmarks are not pointing to consecutive pages like 1, 2, 3, 4, 5...,  if there is any way to reorder bookmarks by their destination page using JavaScript? Thanks a lot!

My Product Information:

Acrobat Pro 9.1.3, Windows 10

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

Hi Thom, thanks for your answer! But I change my code as below, but after running the script, there is an error: "NotAllowedError: Security settings prevent access to this property or method. Collab.reviewersEmail:-1:Console undefined:Exec", can you kindly advise?

var BMDestination = new Array();

var root = this.bookmarkRoot;

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

{

     root.children.execute();

     BMDestination.push({bk:root.children, page:this.pageNum});

}

BMDestination.sort();

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

{

     root.insertChild(BMDestination);

}


Are you running this code in the Console Window?

Have you done any debug to determine on which line the error occurs? The error is not a real error message. It is either extraneous, as Try suggests, or there is an internal bug that is reporting the wrong error. This happens quite frequently since security changes were made to Acrobat a few versions ago. Either way you need to determine the line that is causing it.

Have you checked the results of the sort operation?  I doubt it is sorting correctly. Since the array contains objects, you'll need to pass a helper function into the sort function for determining which object is greater or lesser. Look up the sort function in the Core JavaScript reference online.

There is nothing obvious in the code that would cause an error, but the final loop really should use the length of BMDestination for the increment limit, instead of the root.children since the loop is working on the bookmarkroot. Its possible that something dynamic is happening to the root.children that is causing the error.

2 replies

Ranjit Konkar
Participant
May 17, 2023

There is no built-in utility to arrange the bookmarks in order of occurrence of their destinations?! In fact I don't see any utility to sort bookmarks by any criteria at all. When creating a new bookmark how does Acrobat decide where to show it in the Bookmarks list? Sorted by its destination seems so natural a way so that I can later go from bookmark to bookmark in their order of occurrence in the document. I don't even see a Goto Next/Previous Bookmark. It is direly needed. Acrobat, please put this on your Wanted Features list. In the meantime, can this community guide me on how to obtain and install a Java Script that does this? Sorry I'm jumping into this conversation late, much might have happened since but I don't see any sort function in my Bookmarks menu. Am using Adobe Acrobat Pro.

 

Ranjit

Thom Parker
Community Expert
Community Expert
May 17, 2023

If you would like to request a feature, you'll need to fill out the feature request form.

https://www.adobe.com/products/wishform.html

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
March 13, 2018

Yes, it's possible. You would need to iterate over the entire bookmarks tree, executing each bookmark and then checking the current page number. Then store that information in an array, sort that array and then sort the bookmarks tree based on the sorted array.

I've already written a tool that does that (as well as several other types of sorting), if you're interested. You can purchase it from here: Custom-made Adobe Scripts: Acrobat -- Sort Bookmarks

Participating Frequently
March 13, 2018

Hi try67, thanks for your kind answer! I write the following code as per your suggestion, but how to "sort the bookmarks tree based on the sorted array", can you kindly advise?

var BMDestination = new Array();

var root = this.bookmarkRoot;

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

{

root.children.execute();

BMDestination.push(this.pageNum);

...

}

Thom Parker
Community Expert
Community Expert
March 13, 2018

You need to save a bit more info in the BMDestination array in order to move the bookmarks

BMDestination.push({bk:root.children, page:this.pageNum});

After sorting use the  root.insertChild() function to reorder the bookmarks.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often