PDF Editing Issues

How to Sum PDF Form Fields Based on Dropdown Selection Using JavaScript

Jason MillerJason Miller Nov 24, 2026 869 views

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.

How to Sum PDF Form Fields Based on Dropdown Selection Using JavaScript
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.
Before you start

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.

Solution 1Recommended

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.

1
Open Prepare Form Mode

Open your PDF document in Adobe Acrobat, go to the Tools menu, and select Prepare Form to enable field editing.

2
Access Subtotal Field Properties

Right-click the text field where you want the calculated subtotal to appear (e.g., Subtotal Field) and select Properties from the context menu.

3
Set Calculation Type

In the Properties dialog box, navigate to the Calculate tab. Select the option labeled 'Custom calculation script'.

4
Insert JavaScript Code

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;

5
Save and Test

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.

Apply a Custom Calculation Script in Adobe Acrobat
Field Naming and Loop Limits: The provided script assumes your dropdowns are named 'Dropdown1' to 'Dropdown16' and your input fields are 'AmountRow1' to 'AmountRow16'. Be sure to update the field names and the loop limit (16) in the script to match your specific form design.
Free Microsoft Office alternative

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.

Easily view, fill, and annotate interactive PDF forms.Highly compatible with standard Adobe PDF and Microsoft Office formats.Lightweight software that runs smoothly on Windows, Mac, Linux, and mobile devices.A free, user-friendly alternative to expensive document management subscriptions.
microsoft office alternative - wps office

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.