Skip to main content
Inspiring
June 8, 2021
Question

Page counting [Javascript]

  • June 8, 2021
  • 4 replies
  • 2877 views

Hello, I'm trying to make a dynamic page counter that will display the total number of pages on my pdf document (I have templates spawn and I delete pages aswell). the current code i am using is:

this.getField("PageNo").value = this.numPages;

This is located in document Javascript.

 

The problem that I am having is that the field nearly never shows the right number and dose not update when the page numbers change.

 

Is there a fix for this?

Thank you for your help in advance!

4 replies

try67
Community Expert
Community Expert
June 8, 2021

The problem is that adding or deleting pages will not cause the calculation event to trigger, so it will not show the correct number after you do that. You will have to manually trigger all calculations in your script, by adding this command to it:

this.calculateNow();

try67
Community Expert
Community Expert
June 8, 2021

PS. If this is indeed a calculation script then change it to:

event.value = this.numPages;

If it's not, then just add that line directly to your spawn/delete script, instead of the call to calculateNow.

Legend
June 8, 2021

That code set the field pageNo at the moment it is run. Nothing will change it automatically later. What you haven't told us is what event(s) you use to call that code?

Bernd Alheit
Community Expert
Community Expert
June 8, 2021

Use this also where you spawn and delete pages.

Flynn0101Author
Inspiring
June 8, 2021

Is there a more reliable way to do this?