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

Page counting [Javascript]

Participant ,
Jun 08, 2021 Jun 08, 2021

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!

TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF forms
2.6K
Translate
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
Participant ,
Jun 08, 2021 Jun 08, 2021

Is there a more reliable way to do this?

Translate
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 ,
Jun 08, 2021 Jun 08, 2021

Use this also where you spawn and delete pages.

Translate
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
LEGEND ,
Jun 08, 2021 Jun 08, 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?

Translate
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 ,
Jun 08, 2021 Jun 08, 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();

Translate
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 ,
Jun 08, 2021 Jun 08, 2021
LATEST

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.

Translate
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