• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Reorder bookmarks by their destination

New Here ,
Mar 13, 2018 Mar 13, 2018

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

TOPICS
Acrobat SDK and JavaScript , Windows

Views

277

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 15, 2018 Mar 15, 2018

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

...

Likes

Translate

Translate
Community Expert ,
Mar 13, 2018 Mar 13, 2018

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 13, 2018 Mar 13, 2018

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);

...

}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 13, 2018 Mar 13, 2018

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.

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2018 Mar 14, 2018

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);

}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2018 Mar 15, 2018

Copy link to clipboard

Copied

That error message is not caused by this code.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2018 Mar 15, 2018

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.

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 16, 2018 Mar 16, 2018

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);

}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 16, 2018 Mar 16, 2018

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.execute();

          if (this.pageNum == i){

               BMArray.push(root.children);

          }

     }

}

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

{

     root.insertChild(BMArray,i)

}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 16, 2018 Mar 16, 2018

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});

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 18, 2018 Mar 18, 2018

Copy link to clipboard

Copied

LATEST

Hi Thom, thanks for the additional information! The sentence you suggest below if much more concise, thanks a lot!

BMArray[this.pageNum] = root.children;

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 16, 2018 Mar 16, 2018

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!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines