Copy link to clipboard
Copied
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
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 correctl
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
...
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
That error message is not caused by this code.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi Thom, thanks for your detailed answer! I restart Acrobat and I don't experience this error again, but there is another error: ".
MissingArgError: Missing required argument.
Bookmark.insertChild:23:Console undefined:Exec
===> Parameter oBookmark.", I don'w know how to use the sentence “BMDestination.push({bk:root.children, page:this.pageNum})” you suggest, is it a two-dimensional array? As I only have the experience of using one-dimensional array like "BMDestination.push(i)". For method "InsertChild", its grammer is insertChild(oBookmark, nIndex), how can I utilize array BMDestination to pass parameters to method InsertChild? Thanks a lot again!
Here is my sample document:
https://pan.baidu.com/s/1JcBtG9eETQAK8cPzPzbGaw
snm1
Here is my code:
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);
}
Copy link to clipboard
Copied
Hi Tom, finally I use another way without sorting the bookmarks and it workes! Thanks a lot anyway for your help these days!
var BMArray = new Array;
var root = this.bookmarkRoot;
for (var i = 0; i < root.children.length; i++)
{
for (var j = 0; j < root.children.length; j++)
{
root.children
if (this.pageNum == i){
BMArray.push(root.children
}
}
}
for (var i = 0; i < root.children.length; i++)
{
root.insertChild(BMArray,i)
}
Copy link to clipboard
Copied
I can't believe I missed the missing parameter in insertChild, definitely need the insert index.
You didn't use the sort function, but you are sorting the array by using the page number as an index.
You could have made the code much simpler by using one loop and setting the array index with the page number directly.
BMArray[this.pageNum] = root.children;
This technique will only work when there is exactly one bookmark for each page number. You really aught to use the sort to handle variations.
EX:
BMDestinations.sort(function(a,b){return b.page - a.page});
Copy link to clipboard
Copied
Hi Thom, thanks for the additional information! The sentence you suggest below if much more concise, thanks a lot!
BMArray[this.pageNum] = root.children;
Copy link to clipboard
Copied
Hi try67, also thanks for your helpful hints! I got your help on PlanetPDF forum years ago and I was really impressive by your expertise.
Wish you have a good day!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
The tool I created and linked to above allows you to do that (sort by page number), and it comes with detailed instructions on how to install and use it.
Copy link to clipboard
Copied
Is it where you say - "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".
So it is for sale? Was hoping there would be something shareable free. Thanks anyway.
Copy link to clipboard
Copied
Yes, it's a paid-for tool.