Skip to main content
Community Manager
June 9, 2026
Tutorial

Custom calculations in PDF forms: answered for the whole community

  • June 9, 2026
  • 0 replies
  • 10 views

Calculation questions make up a large share of the traffic in the PDF Forms section of this community. Most of them are variations of the same core scenarios — the same situations, described in different ways, by different users, year after year. This post collects the most common ones and provides complete answers in one place. Every script here is ready to use. Adjust the field names to match your form, paste the script, and the calculation works.

 

Before you begin

How to add a calculation script to any field

  1. Open your PDF in Acrobat. Select All Tools → Prepare a Form to enter form editing mode. Create a text field for each input value and one for the result.
  2. Double-click the result field to open its Properties. Select the Calculate tab, then choose Custom calculation script and select Edit.
  3. Paste the script. Select Close, then save the PDF. Acrobat runs the calculation automatically whenever an input field changes.
  4. Field names are case-sensitive. If the script references UnitPrice, your field must be named exactly UnitPrice. A space, different capitalisation, or a typo will stop the calculation from working. For a full walkthrough, see Configure form field calculations in Acrobat on Adobe Help document.

1 Quantity × unit price, with a grand total across all rows

    Order forms, purchase requests, invoices

This is the most frequently asked form calculation in the community. Users typically have quantity and price fields in multiple rows and need each row to calculate its own total, with all row totals added into a single grand total. 

Two scripts are needed. The first calculates each row's line total (quantity × price). The second, placed in the Grand Total field, sums all line totals. A loop handles any number of rows — just change one number.

Field names to use:

Qty_1 Price_1 Total_1   . Qty_2 Price_2 Total_2 →  GrandTotal

Paste this script in Total_1 → Calculate. For each additional row, copy the same script and change _1 to match the row number.

var qty = parseFloat(this.getField("Qty_1").value) || 0;
var price = parseFloat(this.getField("Price_1").value) || 0;
event.value = (qty * price).toFixed(2);

Paste this script in GrandTotal → Calculate. Change rows to the number of rows in your form.

var total = 0;
var rows = 5; // change to match your form
for (var i = 1; i <= rows; i++) {
var f = this.getField("Total_" + i);
if (f) total += parseFloat(f.value) || 0;
}
event.value = total.toFixed(2);

Calculation order. If GrandTotal always shows zero, go to Prepare a Form → More → Set field calculation order and move all Total_N fields above GrandTotal in the list. GrandTotal must calculate last.

 

 

2 Tax amount and percentage of a subtotal

     Invoices, receipts, expense reports

This is the most common source of incorrect results in community threads. Users apply the Percentage format category to a field and find their result is 100 times too large. The cause and the solution are both straightforward.

Acrobat's Percentage format multiplies the stored value by 100 for display. A field showing "7%" is actually storing 0.07. To calculate tax correctly, format your tax fields as Number — not Percentage — and divide the rate by 100 in the script.
Field names to use:

Subtotal TaxRate TaxAmount       GrandTotal

The user enters the rate as a plain number — for example, 7 for 7%. Paste this in TaxAmount → Calculate.

var sub = parseFloat(this.getField("Subtotal").value) || 0;
var rate = parseFloat(this.getField("TaxRate").value) || 0;
event.value = (sub * (rate / 100)).toFixed(2);

Paste this in GrandTotal → Calculate.

var sub = parseFloat(this.getField("Subtotal").value) || 0;
var tax = parseFloat(this.getField("TaxAmount").value) || 0;
event.value = (sub + tax).toFixed(2);

i   Format TaxAmount and GrandTotal as Number in the field's Format tab. See Format form fields in Acrobat for full formatting options.

 

3 Auto-fill today's date when the form is opened

    All form types

This question appears across every category in the community. Users want a date field that populates automatically without the person filling in the form having to type anything.

A custom calculation script placed in the date field reads the system clock every time any field on the form is updated, keeping the date current. The display format is set using util.printd — adjust the format string to match your region.

Field names to use:

TodayDate

Paste this in TodayDate → Calculate. The field's Format tab can be left as None — the script controls how the date appears.

var d = new Date();
event.value = util.printd("dd/mm/yyyy", d);
// For US format: "mm/dd/yyyy"
// For written month: "dd mmm yyyy" → 15 Jan 2025

i.  The date recalculates whenever any field on the form changes, not only when the form opens. If you need the date locked to the first open, use a document-level script instead. See Add and debug JavaScript in Acrobat for document-level scripting.

 

 

4 Checkboxes that each add a value to a running total

    Registration forms, event booking, service selection

A common pattern in registration and booking forms — each checkbox represents an option with a fixed price, and a total field should reflect the sum of all selected options.

Set each checkbox's Export Value (in Properties → Options) to its price. The calculation script then reads all checkboxes and adds up only the ones that are checked. Unchecked checkboxes return the string "Off", not zero — the script handles this correctly. 

Field names to use:

CB_1 CB_2 CB_3 →  CBTotal

In each checkbox's Properties → Options, set the Export Value to its price (e.g. 500, 250, 750). Paste this in CBTotal → Calculate. Change count to match your number of checkboxes.

var total = 0;
var count = 3; // number of checkboxes
for (var i = 1; i <= count; i++) {
var cb = this.getField("CB_" + i);
if (cb && cb.value !== "Off") {
total += parseFloat(cb.value) || 0;
}
}
event.value = total.toFixed(2);

i.  An unchecked checkbox always returns the string "Off" — not zero and not blank. The !== "Off" check is what correctly excludes unchecked items from the total.

 

 

5 Dropdown selection that changes what gets calculated

    Conditional pricing, membership tiers, service types

Users frequently need a result field to behave differently based on a dropdown choice — apply a multiplier if the answer is "Yes", show zero if it's "No", or apply different rates for different membership levels.

An if/else statement in the calculation script reads the dropdown's current value and runs the appropriate calculation for each option. This pattern works for Yes/No choices, multi-tier pricing, discount types, or any selection that determines how the maths should work.

Field names to use:

DDChoice BaseAmount Multiplier Result

The string in the if statement must match the dropdown's Export Value exactly — check it in the dropdown's Properties → Options tab. Paste this in Result → Calculate.

var choice = this.getField("DDChoice").value;
var base = parseFloat(this.getField("BaseAmount").value) || 0;
var mult = parseFloat(this.getField("Multiplier").value) || 0;

i.  To handle more than two options, add else if (choice === "Option2") blocks between the if and the final else. Each branch can contain completely different maths.

 

6 Calculate age from a date of birth field

    Medical intake, HR, school enrolment, membership forms

This question comes up regularly in medical, HR, and school form discussions. Users need the age to calculate from a DOB field and update automatically — including handling leap years and the boundary case where a birthday falls later in the year.

The script calculates age in whole years, accounting for whether the person's birthday has occurred yet this calendar year. Using valueAsString rather than value avoids a known Acrobat issue with date fields where certain dates after May produce an incorrect raw number. 

Field names to use:

DOB →  Age

Format DOB as Date (mm/dd/yyyy) in its Format tab. Paste this in Age → Calculate.

var dob = this.getField("DOB").valueAsString;
if (dob === "") {
event.value = "";
} else {
var born = new Date(dob);
var today = new Date();
var age = today.getFullYear() - born.getFullYear();
var m = today.getMonth() - born.getMonth();
if (m < 0 || (m === 0 && today.getDate() < born.getDate())) {
age--; // birthday not yet reached this year
}
event.value = age;

7 A result field shows NaN — what it means and how to fix it

    Applies to every calculation script

NaN appears in follow-up replies on almost every calculation thread. It is the single most common issue encountered when building a PDF form with calculations for the first time.

NaN stands for "Not a Number." It appears when Acrobat attempts to perform maths on an empty or non-numeric value. The standard fix is to append || 0 after every parseFloat() call. This tells the script to use zero if the field is empty, keeping the calculation valid at all times. Every script in this post already includes it.

Without the fix — shows NaN when a field is empty 

event.value = parseFloat(this.getField("FieldA").value) * 2;

With the fix — works correctly whether the field is empty or not

event.value = (parseFloat(this.getField("FieldA").value) || 0) * 2;

i.  If a script from another source shows NaN in your form, add || 0 after every parseFloat() in that script. That resolves it in almost every case.  

 

 

 

Quick reference

What each part of the script does

this.getField("Name")

Locates a field in the PDF by its exact name.

.value

Reads the current contents of that field.

parseFloat(…) || 0

Converts the value to a number. Returns zero if the field is empty.

event.value = …

Sets the result in the field that contains the script.

.toFixed(2)

Rounds to 2 decimal places — suitable for currency and hours.

valueAsString

Safer way to read date fields. Avoids a known Acrobat date conversion issue.

 


Adobe Help document — read further

Official documentation for form calculations and scripting

  1. Configure form field calculations in Acrobat
    Step-by-step guide to the three calculation methods — predefined, simplified field notation, and custom JavaScript. Covers calculation order.
  2. Manage form field properties in Acrobat
    How to set field names, formats, tooltips, required fields, and calculated values -- the foundation for everything in this post. 

  3. Format form fields in Acrobat
    Covers Number, Percentage, Date, and Currency format options -- including why the Percentage category affects calculated values and how to handle it.

  4. Add and debug JavaScript in Acrobat
    How to use the JavaScript Console and Debugger in Acrobat to test scripts, view errors, and troubleshoot calculations that aren't producing the expected result.

  5. Apply actions and scripts to PDFs 
    Broader reference for JavaScript actions in Acrobat — triggers, document-level scripts, and links to the full Acrobat JavaScript API Reference.


A note on JavaScript support in Acrobat forms

The calculation scripts in this post use Acrobat JavaScript — Adobe's implementation of JavaScript for PDF forms. Before using them, there are a few things worth knowing about where and how they work.

What is required to author a form with these scripts Writing custom calculation scripts requires Acrobat Pro. Acrobat Standard supports predefined calculations (sum, product, average) but not custom JavaScript in the Calculate tab. For a full breakdown of what each plan supports, see Applying actions and scripts to PDFs on Adobe HelpX.

Where scripts run when the form is filled in Scripts execute in Adobe Acrobat desktop (Pro, Standard, and Reader) and in the Acrobat mobile app. They do not run in browser-based PDF viewers such as Chrome's built-in viewer, Firefox PDF.js, Safari, or Preview on macOS. If a recipient opens the form in one of those environments, calculated fields will not update. To ensure calculations work for all recipients, include a note in the form advising users to open it in Adobe Acrobat or Adobe Reader.

Security settings can affect script execution Acrobat allows JavaScript to be disabled at the application level. If a recipient has turned off JavaScript in their Acrobat preferences, calculation scripts will not run. See Restrict JavaScript API access in Acrobat for details on how these settings work.

For the complete official reference on Acrobat JavaScript — including all supported objects, methods, and properties — the JavaScript for Acrobat API Reference.

 


 

Was this post helpful? If the scripts here solved your problem, select Yes on the helpful prompt below — it helps the community team understand which guides are worth expanding.

Your scenario isn't listed here? Please start a new question in the Acrobat Discussions section of the community rather than replying to this thread. A new post means the right people can find it, experts can respond, and the question becomes searchable for others with the same need. Include your field names and describe the result you are trying to achieve — that gives the community what it needs to help you quickly.