Skip to main content
Participant
May 1, 2025
Answered

Summary total across spawned pages

  • May 1, 2025
  • 1 reply
  • 697 views

Hi Experts, I am now trying to add hidden totals across spawned pages to a summary page,  can someone help me out, Thanks

 

See below is the script i tried using, i also attached the current PDF

 

var result = this.getField("Food");
var sumAd = 0;

for (i = 0; i < this.numPages-1; i++) {
var getCurrent = this.getField("P" + i + (".Additional.subTotal"));
sumAd = getCurrent.value + sumAd;
}
event.result.value = sumAd

this.pageNum = this.numPages-1;

Correct answer Thom Parker

So, the script is not far off, but there are a couple of major issue:

1. the page range has to match the page range of the spawned templates, which are uneven in your form. 

2. this is a calculation script on the "Food" field, so the first line is a problem, i.e., it is not necessary to get the field on which a script is running. All results of a calc script are returned to "event.value". 

See this article:

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

 

Now, there are a few different ways to search for fields on a template. You're script will work with a modification. 

The getField() function returns null if a field name doesn't exist, so all the script needs to do is check for a null value.

 

var sumAd = 0, oFld;

for (i = 0; i < this.numPages-1; i++) {
   oFld = this.getField("P" + i + (".Additional.subTotal"));
   if(oFld)
     sumAd += Number(oFld.value);
}
event.value = sumAd

 

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
May 1, 2025

So, the script is not far off, but there are a couple of major issue:

1. the page range has to match the page range of the spawned templates, which are uneven in your form. 

2. this is a calculation script on the "Food" field, so the first line is a problem, i.e., it is not necessary to get the field on which a script is running. All results of a calc script are returned to "event.value". 

See this article:

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

 

Now, there are a few different ways to search for fields on a template. You're script will work with a modification. 

The getField() function returns null if a field name doesn't exist, so all the script needs to do is check for a null value.

 

var sumAd = 0, oFld;

for (i = 0; i < this.numPages-1; i++) {
   oFld = this.getField("P" + i + (".Additional.subTotal"));
   if(oFld)
     sumAd += Number(oFld.value);
}
event.value = sumAd

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Vc00Author
Participant
May 1, 2025

thank you, it seems to be working, ifollowed you suggestion and moved the template to the top, but whenever i spawn a new page it ends up skipping a page

Thom Parker
Community Expert
Community Expert
May 1, 2025

The script I provided handles the case where the field names don't have to match the spanwed pages.  It does this by checking for a valid field  object before using it to add to the sum.  So the position of the template pages isn't important in this case. 

 

Don't put the template page first. Put it where it works best. 

 

BTW: the solution to the page range not matching the template pages is to change the range, not move the template page. 

 

 

 

 

 

 

 

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