Skip to main content
Participating Frequently
March 10, 2018
Question

Calculate Sum of field family from spawned pages

  • March 10, 2018
  • 1 reply
  • 1191 views

My PDF form consist of:

1 normal  page

2 template pages that are being spawned:

template1 has the field1 that I want to get the Toal sum of

template 2 is not relevant

On page 1 I want to have a text field that calculates the sum of all Pnn.template1.fieldname1 fields in the whole document

the number spawned pages is always different depending on the length of the report

how would such a calculation script look like and where would it go? would it be possible to calculate the value every time I spawn a new page or would it go to a button that searches for all fields in the end when I am done with editing of the document?

thanks a lot in advance, any help is much appreciated!!

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
March 10, 2018

Since you know the unique name of the field, all the script needs to do is to sort through all the fields on the PDF. However, since you're using templates, you have to be careful about not including the fields on a hidden page. Hidden pages have negative page numbers.

var strName;

var nSum = 0;

for(var i=0;i<this.numFields;i++)

{

     strName = this.getNthFieldName(i);

     if(/fldname1$/.test(strName))

     {

          oFld = this.getField(strName);

          if(oFld.page >=0)

              nSum += oFld.value;

     }

}

As far as where it goes, it goes of course in a field calculation script. The field where you want the calculation to be displayed.

Calculation scripts are run anytime any field on the form is modified. That probably happens on a spawn, but you'll need to test it. Use the console window. You'll find a video on it here: Free Video Content & Full Listing of Available Videos 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
frejmAuthor
Participating Frequently
March 11, 2018

Thanks so much for your reply Thom!!

var strName;

var nSum = 0;

for(var i=0;i<this.numFields;i++)

{

     strName = this.getNthFieldName(i);

     if(/fldname1$/.test(strName))

     {

          oFld = this.getField(strName);

          if(oFld.page >=0)

              nSum += oFld.value;

     }

}

I suppose fldname1 is the name that I replace with my fieldname?

does the template name go somewhere in the script or is the whole document being checked for the wanted field?

Inspiring
March 11, 2018

Look at A Lesson in Templates for Adobe Acrobat by Dave Wraight. It shows how fields are renamed when the "bRename" parameter is set to "true" when spawning pages from a template. It is even possible to use JavaScript to extract the page number and template name prefixes from a field name by using the event.name for a spawned field for a variable and then using the JavaScript "split" method to convert the variable string into an array.