Send this page




JavaScript Corner

How to do (not so simple) form calculations

by Thom Parker, Software Developer/Adventurer, WindJack Solutions, Inc.

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


Acrobat provides threee 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. Select either the Text Field Tool or the more general Select Object Tool. Both are located on the Advanced Editing toolbar (Figure 1)


    Figure 1: Form tools on the Advanced Editing Toolbar


  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).
  3. On the Text Field Properties dialog, select the Calculate tab. 
  4. Select the desired calculation option.
  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.

    Simple Calculation Example
    Download [PDF: 85 KB]  



Figure 2: Text-field calculation options/Selecting a predefined 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 above).


Simplified field notation

The predefined calculations are rather limited. 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


Custom calculation script

To create more complex logic or mathematics, the third option, Custom calculation script, must be used. This 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 (above), can find information in both the Acrobat JavaScript Scripting Reference1 and the Acrobat JavaScript Scripting Guide2. 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.




<< Back to JavaScript Corner main menu.

AcrobatUsers.com  >>  User Groups • News • Events • Articles • Blogs • How To • Resources • Member Log in