Acrobat User Community

How to do (not so simple) form calculations

By Thom ParkerJuly 2, 2006

Scope: All Acrobat versions
Skill Level: Beginner
Prerequisites: Familiarity with Acrobat Professional

Acrobat provides three options for creating form-field calculations:

  • Predefined calculations
  • Simplified field notation
  • Custom JavaScript 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:

  1. Activate the Select Object Tool. In Acrobat 9 and earlier, this tool is accessed on the Advanced Editing toolbar. In Acrobat X, it is available in both normal operating mode and in form edit mode. Figure 1 shows screen shots of both modes.

    Figure 1: Activating the Select Object Tool causes the form fields to be highlighted and the field names to be displayed.

  2. Right-click the cursor on the text field where the calculation result will be displayed and select Properties from the popup menu. This action displays the Text Field Properties dialog (Figure 2).

    Figure 2: Text-field calculation options/Selecting a predefined calculation

  3. On the Text Field Properties dialog, select the Calculate tab.
  4. Select the desired calculation option: Predefined, Simplified field notation, or Custom calculation script.
  5. Enter the actual calculation data. This data will, of course, be different for each of the different types of calculations. The accompanying example file, SimpleCalcExample.pdf has one example for each.

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).

Simplified field notation

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

Figure 3: Syntax for using the simplified field notation

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.

Custom calculation script

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


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.