PDF Editing Issues

How to Calculate Balance Due Fields with Acrobat JavaScript

Maira MehtabMaira Mehtab Oct 28, 2026 868 views

Question details

The user needs to create custom calculation scripts in a PDF form to conditionally calculate the balance due to either the company or the employee based on cash advanced and total expenses.

How to Calculate Balance Due Fields Using Acrobat JavaScript
Product
Adobe Acrobat
Device & OS
not provided
Scenario
Creating an interactive PDF expense report form that automatically calculates who owes money based on the difference between cash advanced and total expenses.
Observed behavior
The form requires conditional logic to display the correct balance in the appropriate field (Company or Employee) while leaving the other field blank if the condition is not met.
Before you start

Ensure you have a PDF editor that supports custom JavaScript calculations (like Adobe Acrobat Pro) and verify the exact field names used for your cash advance and total expenses fields.

Solution 1Recommended

Apply Custom JavaScript for Conditional Calculations

Use Acrobat's custom calculation script feature to evaluate the form values and output the result only when specific financial conditions are met.

By using JavaScript's conditional (ternary) operator, you can configure the PDF fields to display a calculated number when a specific condition is true, and remain blank when it is false. This prevents both the 'Balance Due Company' and 'Balance Due Employee' fields from showing values simultaneously.

1
Open Field Properties

In PDF form editing mode, right-click the 'Balance Due Company' text field and select 'Properties' from the context menu.

2
Navigate to the Calculate Tab

Click on the 'Calculate' tab at the top of the Properties dialog box, select the 'Custom calculation script' radio button, and click the 'Edit' button.

3
Enter the Company Script

Input the following script to calculate the company balance: `(function(){var CAE=+getField("CAE").value; var GTE=+getField("GTE").value; event.value=CAE>GTE?CAE-GTE:"";})();` (Replace "CAE" and "GTE" with your actual field names for Cash Advance and Total Expenses).

4
Enter the Employee Script

Repeat the process for the 'Balance Due Employee' field, but reverse the comparison logic in the script: `(function(){var CAE=+getField("CAE").value; var GTE=+getField("GTE").value; event.value=GTE>CAE?GTE-CAE:"";})();`.

Apply Custom JavaScript for Conditional Calculations
Number Conversion: The unary plus operator (+) before `getField` is crucial as it converts the retrieved string value into a number, ensuring the subtraction and greater-than operators work mathematically.
Free Microsoft Office alternative

Edit and Fill PDF Forms Easily with WPS Office

While advanced JavaScript authoring may require specialized tools, WPS Office provides a comprehensive, lightweight, and free solution for viewing, filling, and editing PDF forms, along with full compatibility with Microsoft Office applications.

Seamlessly view, fill, and annotate interactive PDF formsFully compatible with Microsoft Office formats including DOCX, XLSX, and PPTXLightweight architecture ensures fast loading across all your devicesBuilt-in PDF conversion tools to easily switch between Word, Excel, and PDF
microsoft office alternative - wps office

Frequently Asked Questions

Why is my Acrobat JavaScript calculation not updating automatically?

Form calculations in Acrobat trigger based on a specific calculation order. If your balance due fields are calculating before the expense total fields, the output will be delayed or incorrect. Go to Tools > Prepare Form > More > Calculate Order to ensure the source fields calculate first.

Can I hide the zero value if the source fields are completely empty?

Yes. By using the conditional ternary operator (as shown in the solution with `event.value = condition ? result : ""`), the script intentionally assigns an empty string instead of a zero when the condition is false, keeping the form interface clean.

Where can I find more information on JavaScript operators for PDFs?

Acrobat JavaScript is based on core JavaScript. You can refer to standard JavaScript documentation, such as the Mozilla Developer Network (MDN) Web Docs, specifically looking under 'Expressions and Operators' to understand ternary operators, subtraction, and variable declarations.