How to Write Conditional Acrobat Form Calculation Scripts for PDFs
Question details
The user needs to use conditional JavaScript logic in Adobe Acrobat to compare two form fields and output a dynamically calculated result into a third field.

- Product
- Adobe Acrobat / PDF Editors
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form that requires a conditional subtraction calculation depending on which of two field values is larger.
- Observed behavior
- Requires a custom JavaScript solution to perform conditional arithmetic (e.g., subtracting the smaller value from the larger one) where the output defaults to zero if the condition isn't met.
Ensure you have access to a PDF editor with form-editing capabilities, and verify the exact names of the input fields you intend to reference in your calculation script.
Apply Custom JavaScript Conditional Logic to a Form Field
Use the ternary operator in a custom JavaScript calculation script to compare two fields and dynamically calculate their difference.
You can use custom JavaScript within your PDF editor's field properties to execute conditional logic. This is highly effective when you need to ensure a calculated result never falls below zero or when the formula must change based on which field contains the greater value.
Right-click the target field where you want the calculated result to appear, and select 'Properties' from the context menu.
Navigate to the 'Calculate' tab located in the Field Properties dialog box.
Select the 'Custom calculation script' option and click the 'Edit' button to open the JavaScript editor.
To calculate A minus B when A is greater than B, enter the following script: var a = Number(this.getField('A').value); var b = Number(this.getField('B').value); event.value = a > b ? a - b : 0;
If you require the result to be B minus A when B is greater than A, use this alternative logic for the final line: event.value = a < b ? b - a : 0; Always replace 'A' and 'B' with the exact field names defined in your document.

Create and Manage Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editor that empowers you to view, fill, and manage interactive PDF forms effortlessly, supporting advanced standard features without a steep learning curve.
- 1. Download and Install WPS Office: Download the free WPS Office suite from the official website and open the built-in PDF Editor.
- 2. Open Your PDF Form: Click 'Open' and select your interactive PDF document containing the required fillable fields.
- 3. Interact and Fill: Simply click on the interactive fields and type your data. The built-in conditional logic and calculation scripts will process your inputs automatically.

Frequently Asked Questions
Why is my PDF form calculation script returning a zero?
This usually occurs if the field names referenced in your JavaScript do not precisely match the actual field names in the form. It can also happen if the conditional logic (like A > B) evaluates to false based on the numbers currently entered.
Can I use predefined math instead of writing JavaScript?
Yes. For straightforward addition, multiplication, or averaging of fields, standard PDF editors offer predefined calculation options under the 'Calculate' tab. However, making calculations conditional (e.g., executing an action only if one field is larger than another) strictly requires custom JavaScript.
How do I prevent users from manually typing over the calculated result?
Open the Properties menu for the calculated field, navigate to the 'General' tab, and check the 'Read Only' option. This ensures the field updates dynamically based on the calculation script but cannot be manually overridden by the user.




