Skip to main content
DavidSweeneyBear
Inspiring
December 11, 2020
Answered

Cumulative word count...

  • December 11, 2020
  • 2 replies
  • 1558 views

This is step 1 of a much more complex process I'd like to achieve using javascript - unfortunately with no javascript skills whatsoever!

 

What I have at the moment is a javascript plugin (abacadabra tools) which will give me the word count for an individual page (whichever page my cyrsor is on) in a message box... it's a start!

 

I'd like to get a cumulative word count for each page of a document and be able to create text output at the bottom of each page with that number in  it... 

 

Can anyone advise if this is at least theoretically possible?

This topic has been closed for replies.
Correct answer try67

Sorry to have to ask, but google hasn't helped me... what would be the syntax for getting the addField function to use the "counter" var to add a field on the current page, and where/how would I insert that in the "loop" you defined earlier?


Here you go, it will add a field to the bottom-left corner of each page with the words count:

 

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

2 replies

try67
Community Expert
Community Expert
December 11, 2020

Yes, is it, both theoretically and practically.

All you need is a simple counter and a loop, like so:

 

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

 

If you want to add this value to the page itself you can use a field or an annotation, or even a watermark.

DavidSweeneyBear
Inspiring
December 11, 2020

Brilliant - that's both elegant and effective... can the jscript generate the field, annotation or watermark automatically?

&lt;br&gt;~ David Sweeney-Bear ~
try67
Community Expert
Community Expert
December 11, 2020

Yes, it can, by using the addField, addAnnot and addWatermarkFromText methods, respectively.

JR Boulay
Community Expert
Community Expert
December 11, 2020

I'm the developer of the free abracadabraTools (not abacadabra tools), you just gave me an idea for the next version.

Thank you.

Acrobate du PDF, InDesigner et Photoshopographe
DavidSweeneyBear
Inspiring
December 11, 2020

Glad to hear it... I look forward to that 🙂

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