Acrobat Javascript - Bookmark Page Count?

1.8k views Asked by At

Okay so I'm here trying to get myself acquainted with Adobe Acrobat's Javascript API -- I feel like I may be missing some easy ways of doing certain things, but let's find that out together.

The Question: How would I go about finding the amount of pages that belong to a bookmark?

For example, I have the following Bookmark layout:

Intro [3 pages]  
Factions [2 pages]  
Character [3 pages]  
End [1 page]  

(would have posted a picture, but I don't have the permission to do so :/)

Essentially I would like to be able to automate the extraction of the # of pages each bookmark has, for a little project I'm working on to speed stuff up at work.

My code thus far:

/* Count Bookmark Children
    TODO: Count Pages of each Bookmark */

function CountBm(bm) {
    var count = 0;

    console.println("Bookmark name: " + bm.name);
    bm.execute(); // goto bm -- not necessary, just for personal reasons

    console.println("Bookmark Start Page: " + (this.pageNum+1));

    /* This would only work if each page in the bookmark was a child
       of the bookmark being checked */
    if (bm.children != null) {
        for (var i = 0; i < bm.children.length; i++)
            count++;
    }
    console.println("Pages in Bookmark: " + count);
}

var bkmk = bookmarkRoot.children[2]; // Character Bookmark
CountBm(bkmk);

Also, for the last two lines of that code, is there a better way to reference specific bookmarks? By name, perhaps?

1

There are 1 answers

0
jss On BEST ANSWER

I have done this by using the current bookmark's execute() destination relative to the next bookmark's execute() destination. So assuming the bookmarks follow the flow of the document, just run execute() on the next bookmark, and use this.pageNum to figure out how many pages you have jumped forward.

Something like:

this.pageNum = 0;

for (var i = 1; i < this.bookmarkRoot.children.length; i++) {
     page = this.pageNum;
     this.bookmarkRoot.children[i].execute();
     console.println("This bookmark is " + (this.pageNum-page) + " pages long");
}

You can add handling for grandchildren bookmarks as well, depending on your application. The right solution is dependent upon the structure of your bookmarks. The print to console line above could be replaced with this.extractPages(...) for your application.

Unfortunately that's the only way to reference bookmarks. If you wanted to find a bookmark by it's name, you could store all the bookmark names in an object with their child indexes. It's a hack, but it can be helpful when you have a document with a large number of bookmarks.