By Thom Parker July 2, 2006
Scope: All Acrobat versions
Skill Level: Beginner
Prerequisites: Familiarity with Acrobat Professional
Acrobat provides three options for creating form-field calculations:
In reality, all three are JavaScript, but for the first two, Acrobat builds the JavaScript code for us. Calculations are only available for text fields and are accessed through the Text Field Properties dialog.
To enter a calculation:
Predefined calculations
The first option on the Calculate tab is for the predefined calculations. To use this option, simply select one of the calculations from the drop-down list, click on the Pick… button and select the fields to be used as inputs (Figure 2).
The predefined calculations are rather limited, for example, there is no division or subtraction. The simplified field notation allows the creation of much more complex calculations. It uses a notation similar to how a calculation would ordinarily be written, i.e., using the regular math symbols, + (addition), -(subtraction), *(multiplication) and /(division). Field names are used as operands. In the example file the equation:
D = (A + B)/C
is calculated by entering (Figure 3)
(Calc2_A + Calc2_B) / Calc2_C
Where Calc2_A, Calc2_B, Calc2_C are the names of text fields
This notation is limited to calculations that only use the standard four math operators. It also does not handle form field names that include punctuation or spaces. Special characters are, in fact, the main reason calculations using this method fail, so let me repeat the restriction again. Simplified field notation cannot operate on any field name that includes spaces or punctuation.
To create more complex logic or mathematics the third option, Custom calculation script must be used. This option requires entering JavaScript and gives full access to all the fields (and other features) on the PDF file, as well as the rich math features in the JavaScript language. However, it also requires use of the full Acrobat JavaScript syntax which makes the calculations significantly longer. For example, the following code solves the same equation used in the simplified field notation example above.
event.value = ( this.getField("Calc2_A").value + this.getField("Calc2_B").value ) / this.getField("Calc2_C").value;
The first thing to notice is that the calculated value is assigned the variable event.value. The value of this variable is assigned to the field when the calculation is completed. All calculation scripts must assign a value to the event.value variable. The second thing to note is that the input field values are explicitly acquired. Take for example the input value
this.getField("Calc2_A").value
The first part of this code segment, this.getField("Calc2_A"), acquires the Field Object for the field named Calc2_A. The actual value for the field is obtained from the value property. Even experienced JavaScript programmers sometimes forget to append the value property, so watch out for this common omission and carefully inspect all the code.
While this is all you need to know to get started using calculations in Acrobat forms, there is more. If you want to use advanced features or do something more complex than described in the example file SimpleCalcExample.pdf , can find information in both the Acrobat JavaScript Scripting Reference1. You may also want to look up the core JavaScript Math object, which provides details on advance mathematical functions. Find information on this object in any standard JavaScript reference, or on line at the Mozilla developer site.