Calculating field values and more

Calculations are one of the most important activities performed by an electronic form. In the simplest instance, a calculation is a mathematical combination of the values from other fields on a form. For example, multiplying the quantity and price entries on an order form to find the cost, or adding all the cost fields together to find the total cost of the order. But of course, there are many different types of calculations that a form might need, and many different ways a calculation might be done. For example, a calculation could depend on whether a check box is marked. This is a conditional calculation. Or, a calculation might be used to add up the number of checked boxes in a table. Or, it might be used for something completely different. There is a lot of variation in calculation scripts.

In an AcroForm (a regular PDF form), field calculations are performed by the Calculation Event Script. This event can be applied to any form field and it's triggered whenever any field on the PDF is modified. But of course, it is most often associated with Text Fields. In fact, Acrobat only provides a user interface for entering calculations into the Text Field and the ComboBox Field. Entering a calculation script into one of the other field types requires the use of a script (see articles below for more info on entering calculation scripts).

The Calculation Event is triggered for every field that has a calculation script every time any field on the form changes. However, field changes made through the official Calculation Event mechanism do not trigger further calculation events. This can be a tricky issue. If a script makes changes to a field outside of the event mechanism then it can cause more calculation events. Obviously, this is not a good situation since there is a possibility of infinite events looping. Also, since calculation events are called frequently, there is a chance they could cause the form to have serious performance issues.

  1. Always set the calculated field value with the event.value property.
    (Examples shown below in the articles)
  2. Never use the calculate event to set other field values, unless you know exactly what you are doing. Changing other fields can cause large (possibly infinite) cascades of follow on calculate events. That said, this event can be useful for controlling sets of fields, especially radio buttons and checkboxes, which do not have editable calculate events. But special care has to be taken when using a calculate event in this way.
  3. Keep calculation scripts as short as possible since they are run frequently.
  4. Keep the number of calculation scripts on a form to a minimum.
  5. Never use a Blocking Action in a calculation script. A Blocking Action is any code that stops execution and waits for the user, or something else to respond, such as an alert box.

The Calculation Event does not have to be used for calculations. In fact, in a more general sense it's just an event that gets called when a form field changes. So, it can be used to run a script you want triggered any time the form fields change. For example, a form could have its "submit' button initially hidden, and then use the Calculate Event to test certain required fields and only show the submit when they are filled out correctly. This creates a kind of form-level validation. But again, this has to be done with care to prevent performance problems or worse.

  • Article - Entering Calculation Scripts
  • Article - How to Write a Basic PDF Calculation Script
  • A set of sample PDF files with Calculation Script examples
  • Entering Calculation Scripts (9:21, 7.6 MB) (Member Only Content) How to Write a Basic PDF Calculation Script
    Thom Parker, windjack.com

    The built-in PDF calculation functions are extremely limiting and problematic. Whereas calculation scripts are powerful, flexible, and break down all barriers to functionality. This article will get you started with writing calculation scripts by dissecting a simple calculation script into it's basic parts. It shows how each part works and discussed how it could be expanded for more complex calculations. keep reading

    The Field Properties Dialog Methods for accessing the Properties Dialog Prepare Form Select Object Tool JavaScript More on this topic Field Scripting Basics (8:09) Quick Form Field Fixups . keep reading

    It is often desirable for an unfilled form or form section to be empty. Unfortunately, if there are calculations, an unfilled form can have several fields full of zeros. This article will teach you 4 different ways to blank out zero values. keep reading

    One of the most common PDF error messages encountered by new form designers, is "The value entered does not match the format of the field." This message is displayed whenever one of the pred. keep reading

    This article covers the basics of entering Calculation Scripts into a PDF. It is primarily for beginning scripters, but there are also tidbits of interest for intermediate and advanced users. keep reading

    This article covers the rules and techniques for writing AcroForm Calculation Scripts. Several examples are provided to demonstrate the ideas discussed. keep reading

    Form Table Calculation Samples
    AcroForm, Calculation, Table, LiveCycle,Form

    This page lists all the download samples with table calculations, as well as a short discussion on how to write table calculations. keep reading

    Form level validation is used to ensure all the required form data is filled in, and/or to make sure any data dependencies between fields are met before the form is submitted, saved, printed, or emailed. keep reading

    There are many commercial applications (press releases, product descriptions, ad text, etc.) as well as others where counting text elements like characters and words is important. The scripts on this page provide several different techniques for finding this information, including filtering text for specific characters and words. keep reading

    It's often important to count data entries on a form. For example, for an average calculation the number of filled fields needs to be counted. Or, to score a test the number of checked Radio Buttons may need to be counted. This short article covers different techniques for building calculation scripts that count stuff. keep reading

    This page is a listing of sample PDFs that use the calculation event. The samples are collected from different areas of this site. They are not necessarily "Calculation" examples, but they. keep reading

    Several common date calculations, including finding age, the number of days and hours between two dates, calculating expiration dates, and many others. keep reading