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

PDF Form Javascript for numbering pages from a template

Explorer ,
Nov 12, 2020 Nov 12, 2020

I have a two page form. Page 2 has a "Add Page" button on it that generates a copy of it from a template. 

 

On this replicating page I have two page number fields the left is the current page number and the right is the total replicated pages. The number starts at 1.

 

I'm running into a problem with the additional pages. When generating page the second replicated page it the last page says 2, as it should. But when I continue generating pages the third generation then says -1 and updates to 3 when I add the 4th but then the 4th is -1 until I add the 5th and so on.

 

My script in this field is :

event.target.value = event.target.page;
if(event.target.value == "-1"){
this.calculateNow();}

 

What am I missing? please help!

 

 

3.5K
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
1 ACCEPTED SOLUTION
Community Expert ,
Nov 12, 2020 Nov 12, 2020
LATEST

On another note. I would suggest you look in the console to see if there are any errors being reported. It sounds like there are a lot of things happening on this form, so it would be very easy to have scripts that walk on top of each other and cause problems, especially if there are a lot of calculation scripts. 

For example, are there long delays after a page is spawned or data is entered?  

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

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
Explorer ,
Nov 12, 2020 Nov 12, 2020

I should add, if I click any button in the form it updates all the numbers to their correct value, but I want it updated when the generate new page button is clicked, not have to do an additional step.

 

Moved from Using the Community (which is about the forums) to a better forum... Mod
To find a forum for your program please start at https://community.adobe.com/

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
Explorer ,
Nov 12, 2020 Nov 12, 2020

Nevermind, it works now. I added to the second field that is the total generated pages with the script:

var numpag = this.numPages - 1;
event.value = numpag;
this.calculateNow();

 

and it updates both fields when the new page is generated. I don't understand why it needed it but it seems like that second script created the event it needed to update.

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 ,
Nov 12, 2020 Nov 12, 2020

The page number for any hidden template is "-1". 

I would suggest that you do not use a calculation script for this, unless you want the page number to be dynamic. Calculations are called anytime any field on the form is changed. So this code is called quite frequently. Page number are usually stable, so it should be set once. A good place to do this would be in the code that spawns the page. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Explorer ,
Nov 12, 2020 Nov 12, 2020

The total number of pages change based on how many pages are spawned aditionally the counting of 1 of x starts on page 2. How would I write it into the button that's spawning the page so that the updated numbers appear in the fields?

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 ,
Nov 12, 2020 Nov 12, 2020

It's important to think about the order in which things happen.

So the first thing, if the scripts you've shown on this post are used in calculations, then you need to remove the "calculateNow" line right away. This function is not to be used inside a calculation. It's also unnecessary because if the the "calculateNow" line runs, then the code above it has already run. It doesn't make anything happen that hasn't aready happened. I hope that is both clear and self evident.

 

If the page numbers are dynamic, then it is fine to use a calculation script.  You just need that script to run at the right time, and not cause problems in other places.  Calculation scripts are tricky for a number of reasons. You can read more about them here:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

 

The esiest way to force the calculations on the newly created page, or the other pages after a deletion, is to put the "calculateNow" line in the scripts that spawn and delelete template pages. But it has to be placed after the code that does the spawn or deletion. 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Nov 12, 2020 Nov 12, 2020
LATEST

On another note. I would suggest you look in the console to see if there are any errors being reported. It sounds like there are a lot of things happening on this form, so it would be very easy to have scripts that walk on top of each other and cause problems, especially if there are a lot of calculation scripts. 

For example, are there long delays after a page is spawned or data is entered?  

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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