How to Sum PDF Form Fields Based on Dropdown Selection Using JavaScript
Question details
The user needs to dynamically calculate subtotals in a PDF form by summing up amounts only when a specific option is chosen from a dropdown menu.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating an interactive expense report in a PDF where amounts are added to a subtotal based on conditional logic from dropdown field selections.
- Observed behavior
- The user requires a custom JavaScript loop that iterates through rows of fields, checks the dropdown value, and aggregates the corresponding amounts into a single total field.
Ensure all your PDF form fields follow a consistent naming convention with sequential numbering (e.g., Dropdown1, AmountRow1) so the JavaScript loop can reference them dynamically.
Apply a Custom Calculation Script in Adobe Acrobat
Use Adobe Acrobat's custom calculation feature to run a JavaScript loop that evaluates dropdown selections and sums the corresponding values.
Interactive PDF forms support JavaScript to perform dynamic, conditional calculations. By adding a custom script to your target 'Subtotal' field, the PDF can evaluate the values of other fields in real-time.
The script will iterate through your defined number of rows. It checks if the dropdown selection matches your target category and verifies that the amount field is not empty before adding it to the subtotal.
Open your PDF document in Adobe Acrobat, go to the Tools menu, and select Prepare Form to enable field editing.
Right-click the text field where you want the calculated subtotal to appear (e.g., Subtotal Field) and select Properties from the context menu.
In the Properties dialog box, navigate to the Calculate tab. Select the option labeled 'Custom calculation script'.
Click the Edit button next to the selection and paste your script. For example: var sub = 0; for(var i=1; i<=16; i++){ if(this.getField('Dropdown'+i).valueAsString == 'Advertising Promotion' && this.getField('AmountRow'+i).valueAsString != '') { sub += Number(this.getField('AmountRow'+i).valueAsString); } } event.value = sub;
Click OK to save the script, then close the Properties dialog. Exit the Prepare Form mode and test your calculation by selecting the target dropdown options and entering numerical values.

Edit, Fill, and Manage PDF Forms with WPS Office
If you are looking for a lightweight and cost-effective solution for handling PDF forms and office documents, WPS Office provides an excellent alternative. It offers a comprehensive suite for creating, editing, and managing PDFs alongside Word, Excel, and PowerPoint files.

Frequently Asked Questions
Can I use multiple conditions in my PDF calculation script?
Yes, you can use logical operators like && (AND) or || (OR) within your JavaScript 'if' statement to evaluate multiple dropdown fields or different criteria before summing the values.
Why is my PDF form calculation not updating automatically?
If your subtotal isn't updating, the field calculation order may be incorrect. In Adobe Acrobat, go to Prepare Form > More > Clear Form / Set Field Calculation Order, and ensure the subtotal evaluates after the dependent amount and dropdown fields.
Do I need to know JavaScript to create basic PDF forms?
No. Simple operations like sum, product, or average can be done using the built-in calculation options in the field properties. However, conditional logic—like summing only when a specific dropdown is selected—requires custom JavaScript.
What happens if a user leaves the amount field empty?
The custom script includes a condition (this.getField('AmountRow'+i).valueAsString != '') that checks if the field is empty. This prevents the script from throwing errors or returning a NaN (Not a Number) result in your subtotal.




