How to Use SUMIF-Style JavaScript Calculations in Acrobat PDF Forms
Question details
The user needs to create conditional, category-based subtotals in an interactive PDF expense form by adding specific amounts only when a dropdown or text field matches a defined expense type.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Designing an interactive expense report or timesheet PDF form that automatically calculates subtotals based on category selections.
- Observed behavior
- Looking for a JavaScript solution to implement Excel's SUMIF logic inside a PDF form, iterating through fields to dynamically calculate sums based on matching criteria.
Ensure that all the form fields you plan to include in the calculation have consistent, sequential naming conventions (e.g., Drop1.0, Drop1.1, Amount1.0, Amount1.1) so the JavaScript loop can properly iterate through them.
Apply a Custom JavaScript Calculation to the Subtotal Field
Use Adobe Acrobat's custom calculation script feature to loop through the designated fields and add values conditionally when criteria are met.
PDF forms do not natively support Excel formulas like SUMIF. To achieve this functionality, you must write a short JavaScript loop that checks the value of your category fields and adds the corresponding amount to a running total if the condition is met.
Open your interactive PDF form in Adobe Acrobat and navigate to the 'Prepare Form' tool from the right-hand pane.
Right-click the subtotal field where you want the calculated result to appear (e.g., a field named 'Traveltot') and select 'Properties' from the context menu.
Navigate to the 'Calculate' tab in the Properties dialog box and select the radio button for 'Custom calculation script'.
Click the 'Edit' button and input your JavaScript loop. For example: define `var total = 0;`, use a `for` loop to iterate through the field indices (`i`), check if the category field matches your criteria using `if (this.getField("Drop1." + i).valueAsString == "Travel")`, and if true, add `Number(this.getField("Amount1." + i).valueAsString)` to the total. Finally, set `event.value = total;`.
Click 'OK' to save the script, close the Properties window, and exit the Prepare Form mode to test your dynamic calculation by selecting categories and entering amounts.

Manage PDF Forms and Data Easily with WPS Office
While advanced JavaScript programming in PDFs can be highly complex to troubleshoot, WPS Office provides an intuitive, lightweight PDF editor and spreadsheet suite to help you fill, manage, and extract data from PDF forms seamlessly. If PDF calculations become too cumbersome, you can easily bridge your PDF data to spreadsheets for native SUMIF functionality.
- 1. Install WPS Office: Download and install the free WPS Office suite on your device.
- 2. Open your PDF Form: Launch WPS PDF and open your interactive PDF form document.
- 3. Fill or convert data: Use the 'Form' tools to fill in your data. Alternatively, use the 'PDF to Excel' tool to extract the form data into a spreadsheet if you prefer managing complex conditional calculations in a spreadsheet environment.
- 4. Save and export: Save your filled PDF or export the newly created spreadsheet directly to your local drive or cloud storage.

Frequently Asked Questions
Why is my PDF JavaScript calculation not updating automatically?
The field calculation order might be incorrect. In Acrobat's 'Prepare Form' mode, go to 'More' > 'Set Field Calculation Order'. Ensure that your subtotal field is positioned at the bottom of the list so it calculates after all the input fields have been processed.
Can I use Excel formulas like SUMIF directly in a PDF?
No, standard PDF forms do not support Excel formulas directly. You must use Acrobat JavaScript to replicate Excel functions like SUMIF by creating `for` loops and conditional `if` statements to evaluate the fields.
How should I name my PDF form fields for JavaScript loops?
You should use hierarchical or dot-sequential naming, such as 'Amount.0', 'Amount.1', and 'Amount.2'. This structure allows your JavaScript `for` loop to dynamically target the fields using a variable index, like `this.getField("Amount." + i)`.




