Copy link to clipboard
Copied
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!!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Yes, "fieldName1" is the value you need to substitute. You don't need to worry about the template name or page number as long as the field being summed has a unique name, or partial name. The script simply searches all fields on real pages for names that match. The Regular expression may need to be adjusted for your particular search.