How to Sum PDF Form Fields Based on Combo Box Selections
Question details
The user needs a method to calculate a total sum from multiple PDF expense fields, but only when corresponding combo box fields contain a specific selected value.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Building an interactive PDF form, such as an expense report, where the total is calculated dynamically based on categorized dropdown selections.
- Observed behavior
- Standard calculation tools sum all fields indiscriminately; the user requires a conditional calculation utilizing JavaScript to filter which fields are added to the final total.
Ensure that your PDF form fields are systematically named with numerical suffixes (e.g., Type1, Type2 and OtherExpense1, OtherExpense2) so they can be referenced easily in a JavaScript loop.
Apply Custom JavaScript for Conditional Calculation
Use a custom calculation script on your total field to loop through the combo boxes, check their values, and sum the corresponding expense fields if the condition is met.
Built-in PDF calculation tools only support simple arithmetic operations across selected fields. To perform conditional logic based on a user's combo box selection, you must apply a custom JavaScript calculation.
The script will iterate through each row of your form, check the text string selected in the combo box, and add the matching expense value to a running total only if the exact string matches your target criteria.
Enter form editing mode in your PDF editor. Right-click the field where you want the final sum to appear, and select 'Properties'.
In the Field Properties dialog box, select the 'Calculate' tab to view calculation options.
Choose the option for 'Custom calculation script' and click the 'Edit' button to open the JavaScript editor.
Type 'var total = 0;' to initialize the counter. Then, create a for-loop that checks each combo box: 'for (var i = 1; i <= 11; i++) { if (this.getField("Type" + i).valueAsString == "Training") total += Number(this.getField("OtherExpense" + i).valueAsString); }'.
Finish the script by assigning the calculated total to the field: 'event.value = total;'. Click 'OK', then close the properties window and test your form.

Manage Interactive PDF Forms Seamlessly with WPS PDF
WPS PDF offers a robust and lightweight environment for handling complex interactive PDF forms. Whether you need to fill out expense reports with embedded JavaScript calculations or organize your documents, WPS Office ensures accurate processing and smooth performance.
- 1. Open Your PDF Form: Launch WPS Office, click on the 'PDF' tab, and select your interactive PDF file to open it.
- 2. Interact with the Form: Navigate to the combo boxes and make your selections. WPS PDF will smoothly process the form's logic.
- 3. View Automated Calculations: Watch as the total fields automatically calculate and update based on the embedded JavaScript formulas as you input data.

Frequently Asked Questions
Why is my PDF calculation field staying at zero?
This commonly occurs if the field names referenced in your JavaScript loop do not perfectly match the names of the form fields in the PDF document. Check for spelling errors, incorrect numbers, or mismatched casing.
Can I perform conditional sums without using JavaScript?
No. The standard calculation functions provided in most PDF editors (such as Sum, Product, Average) are applied globally to the selected fields. Conditional logic requiring "if/then" statements always requires a custom calculation script.
Will this JavaScript update the total dynamically when a user changes a dropdown?
Yes. When a custom calculation script is assigned to a target field, the PDF viewer automatically triggers the script and recalculates the total every time any related form field is modified.




