Skip to main content
DavidSweeneyBear
Inspiring
December 11, 2020
Answered

How to apply javascript to specific areas of a pdf via bookmarks

  • December 11, 2020
  • 2 replies
  • 831 views

This is really a "part 2" leading on from an earlier post Solved: Re: Cumulative word count... - Adobe Support Community - 11669143

 

Having been given an elegant piece of code by @try67  to paste a cumulative word count at the bottom of every page -

 

 

var r = [5, 25, 50, 5];
var counter = 0;
for (var p=0; p<this.numPages; p++) {
var numWords = this.getPageNumWords(p);
counter+=numWords;
//console.println("Number of (total) words on page " + (p+1) + ": " + counter);
var f = this.addField("PageText"+p, "text", p, r);
f.readonly = true;
f.defaultValue = counter;
f.value = counter;
}

 

 

This works perfectly for the document as a whole. What I'm hoping to achieve next is to apply that same script to separate "chapters" within a pdf.

Chapters are defined by a bookmark at the start of each chapter, so the script needs to start the cumulative count at the first bookmark, up to the second, then start again from the second.. and so on.

 

Am looking for any suggestions as to how this can be achived via javascript?

 

This topic has been closed for replies.
Correct answer try67

(You could have posted this as a follow-up to the original question, but never mind)

 

This will require a more complex script, of course. For starters, is your bookmarks structure flat (ie. only has one level), or does it have multiple sub-levels? The latter is extremely complex to process and will require using recursion.

The former is somewhat simpler. Basically you need to create a loop over the top-level bookmarks (the "children" of the bookmarkRoot object), executing each one, and then examining the current page number. That will help you construct a list of the pages in each "chapter", which you could then iterate over, adding up the words in each one.

 

If you're interested I could develop this script for you, for a small fee. You can contact me privately via [try6767 at gmail.com] to discuss it further.

2 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
December 11, 2020

(You could have posted this as a follow-up to the original question, but never mind)

 

This will require a more complex script, of course. For starters, is your bookmarks structure flat (ie. only has one level), or does it have multiple sub-levels? The latter is extremely complex to process and will require using recursion.

The former is somewhat simpler. Basically you need to create a loop over the top-level bookmarks (the "children" of the bookmarkRoot object), executing each one, and then examining the current page number. That will help you construct a list of the pages in each "chapter", which you could then iterate over, adding up the words in each one.

 

If you're interested I could develop this script for you, for a small fee. You can contact me privately via [try6767 at gmail.com] to discuss it further.

DavidSweeneyBear
Inspiring
December 11, 2020

@try67 

Was hoping to break my problem into smaller pieces, which other people may find useful individually...

 

Yes, the bookmark structure for Chapters is flat. I do sometimes add child bookmarks within chapters, but from what little I do know of jscript i think these can be ignored?

 

I'll send you an email - many thanks

&lt;br&gt;~ David Sweeney-Bear ~
DavidSweeneyBear
Inspiring
December 11, 2020

note - sorry don't know how to get the code to appear in nice grey box like the pros do! 🙂

&lt;br&gt;~ David Sweeney-Bear ~
DavidSweeneyBear
Inspiring
December 11, 2020

sorted.

&lt;br&gt;~ David Sweeney-Bear ~