In PDF Form Reset Tricks I described why I prefer using a script instead of the Reset a form function built in to the field properties of the Acrobat user interface. One of the advantages is that you can name all the fields you want to reset and push them into an array. While the built-in Reset a form function allows you to select the fields you want to reset, the problems comes when you create more fields after the fact. They are automatically added to the list. So you either have to make sure the reset button is the last thing you do to the form, or you have to remember to go back and unselect fields that have been created since you added the reset button. The scripting method does not have this problem.
Resetting One Page Only
This seems easy at first glance.
Loop through all the field names. Get the page property of each field. Test whether the page number of the field matches the current page number. If it matches, push the field name into an array and reset the array of fields.
Not So Fast
Fields can have multiple copies (“widgets”) throughout the document. In other words, the field names for multiple fields are the same. This is handy when you need to have the same value in fields throughout the document, since value is a field-level property, not a widget-level property. When there is only one copy of a field and you call the page property of that field it returns a number (the 0-based page index of the page on which that field is located). When you call the page property of a field that has multiple copes it returns an array of numbers (one for each widget). Testing whether a number is equal to an array will always return false.
What’s The Solution
First, thank you to Thom Parker who provided the script in an answer on the Acrobat Users Forum. Here’s what you need to know.
Loop through all the field names in the document and test the page number of each field.
If the page number of the field returns an array, leave it.
If the page number returns a number, convert it to an array.
Test whether the array contains any numbers that match the current page number.
If the previous test returns true, push the field name into an array.
Reset the fields that have names in the array.
var tpage = this.pageNum; //Line 1
var oFld, fldName, aPgs, aFldNames=[]; //Line 2
for(var i=0;i<this.numFields;i++)//Line 3
{
fldName = this.getNthFieldName(i); //Line 4
oFld = this.getField(fldName); //Line 5
aPgs = (typeof(oFld.page) == "number")?[oFld.page]:oFld.page; //Line 6
if(aPgs.some(function(a){return a==tpage})) //Line 7
{
aFldNames.push(fldName); //Line 8
}
}
this.resetForm(aFldNames); //Line 9
Script Explanation
Line 1: Define a variable that represents the current page number
Line 2: Define 4 more variables representing field object, field name, page numbers, and an array of field names to reset.
Line 3: Loop through all field names in the document.
Line 4: Get the Nth field name.
Line 5: Get the field object with the name of the Nth field name.
Line 6: Test the page property of the field object. If it returns a number, convert to an array. If returns an array, leave it.
Line 7: Use the array.some( ) JavaScript function to determine whether the array contains the current page number.
Line 8: If true, push the field name into the aFldNames array.
Line 9: Reset the fields.
Hi David, I appreciate your weekly emails which are very useful and store them for daily and future use. If you may answer a question, I would appreciate it, which is this:
When using several calculation fields using the Acrobat addition in the calculation tab, sometimes the calculation order gets out of whack and sometimes the calculation results will go unnoticed thus providing a wrong total. Can this calculation order problem be avoided by using a script in every field rather than using the calculation tab in Acrobat ? Thank you.