Calculate Surplus or Shortfall Between Two PDF Form Fields
Question details
The user needs to calculate the difference between 'Net Sold' and 'Net List' fields in a PDF form, displaying the resulting surplus, shortfall, or zero when values are equal.

- Product
- Device & OS
- not provided
- Scenario
- Creating or editing a fillable PDF form that requires automatic field calculation for financial or inventory tracking.
- Observed behavior
- The user wants a dynamic 'Surplus/Shortfall' field that automatically updates with a positive, negative, or zero value based on the input of two other numerical fields.
Ensure your PDF editor supports JavaScript and custom calculation scripts for form fields. You will also need to know the exact field names (e.g., 'Net Sold' and 'Net List') to reference them correctly in the formula.
Use a Custom JavaScript Calculation in the Target Field
Apply a simple custom calculation script to the Surplus/Shortfall field to automatically subtract the list value from the sold value.
PDF forms use JavaScript for advanced mathematical operations. By writing a short script, you can easily pull the values from two separate fields and calculate their difference. The result will return a positive number if there is a surplus, a negative number for a shortfall, and exactly zero if the values match.
Open your PDF form in editing mode, right-click the 'Surplus/Shortfall' text field, and select 'Properties' from the context menu.
In the Properties dialog box, click on the 'Calculate' tab to view the available mathematical operations.
Select the 'Custom calculation script' option and click the 'Edit' button. Paste the following script: var sold = Number(this.getField("Net Sold").valueAsString); var list = Number(this.getField("Net List").valueAsString); event.value = sold - list;
Click 'OK' to save the script, then close the Properties window. Enter test numbers into the 'Net Sold' and 'Net List' fields to verify the calculation works correctly.

Create and Calculate PDF Form Fields with WPS Office
WPS Office provides comprehensive PDF editing tools, allowing you to easily create fillable forms, manage field properties, and implement custom calculations for dynamic documents.
- 1. Open PDF Form: Launch WPS Office and open your PDF file containing the form fields.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access the interactive form editing tools.
- 3. Edit Field Properties: Double-click the target field (e.g., Surplus/Shortfall) to open its properties panel.
- 4. Apply Calculation: Go to the 'Calculate' tab, choose 'Custom calculation script', and input your JavaScript formula.

Frequently Asked Questions
Why is my PDF calculation field not updating automatically?
If your calculation field doesn't update, ensure that the field calculation order is set correctly in your PDF editor. Also, verify that the field names in your JavaScript perfectly match the actual names of the text fields, including exact capitalization and spacing.
How do I make the result blank instead of zero when the input fields are empty?
You can add an 'if' statement to your JavaScript to check if the fields are empty before calculating. For example: if (this.getField('Net Sold').valueAsString === '' && this.getField('Net List').valueAsString === '') { event.value = ''; } else { ... calculation logic ... }
Can I use standard calculation options instead of a custom script for subtraction?
Most standard PDF calculation options only include pre-built functions for sum, product, average, minimum, and maximum. Since subtraction involves specifically deducting one field from another in a set order, a custom JavaScript calculation is usually required.




