How to Use Conditional PDF Form Fields in a Calculation
Question details
The user needs a way to calculate a value in a PDF form by multiplying one of two fields by a constant (1.25), depending on whether the second field is empty or populated.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating a dynamic PDF form that requires conditional logic to calculate totals based on dynamic user input.
- Observed behavior
- The form needs to automatically choose the correct input field (Field 1 or Field 2) for the calculation based on whether Field 2 contains a value.
Ensure you have a PDF editor that supports advanced form field properties and custom JavaScript execution, as basic PDF readers cannot process or edit form scripts.
Use a Custom Calculation Script
Apply a custom JavaScript snippet to the target calculation field to handle the if/else conditional logic automatically.
PDF forms support custom JavaScript for advanced calculations that go beyond simple sums and products. By using a basic if/else statement, you can check if a specific field is populated and perform calculations accordingly.
Open your PDF in form editing mode, right-click the specific calculation field where you want the final result to appear, and select 'Properties'.
In the Properties dialog box, click on the 'Calculate' tab at the top of the window.
Choose the option for 'Custom calculation script' and click the 'Edit' button to open the JavaScript text editor.
Input the following script: var fld1 = this.getField('Field1').value; var fld2 = this.getField('Field2').value; if (fld2 != '') { event.value = fld2 * 1.25; } else { event.value = fld1 * 1.25; }
Click 'OK' to save the script, close the properties window, and test your form by entering numeric values into Field 1 and Field 2.

Create and Edit Smart PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editor that makes it easy to create interactive forms, add calculation scripts, and manage complex PDF documents smoothly.
- 1. Launch WPS Office PDF Editor: Open WPS Office and load your PDF document using the WPS PDF tool.
- 2. Access Interactive Form Tools: Navigate to the 'Form' tab on the top ribbon to access interactive form creation and editing tools.
- 3. Edit Field Properties: Add or select a text field, double-click to open its properties, and configure your custom calculation scripts in the Calculate tab.

Frequently Asked Questions
Why isn't my custom PDF calculation script working?
Check for syntax errors in your JavaScript, ensure the field names in the script match your form fields exactly (including capitalization), and verify that your PDF viewer is set to allow JavaScript calculations.
Can I use standard mathematical operators in PDF JavaScript?
Yes, standard operators such as '+' (addition), '-' (subtraction), '*' (multiplication), and '/' (division) are fully supported within custom PDF calculation scripts.
How do I make a PDF form field hidden unless a specific condition is met?
You can control field visibility using JavaScript by setting 'this.getField("FieldName").display = display.hidden;' or 'display.visible' inside an if/else block based on your required conditions.




