Calculate Internal and External PDF Form Totals with JavaScript
Question details
The user needs to calculate separate totals for internal and external activities across 14 rows in a PDF form, followed by a combined grand total.

- Product
- Device & OS
- not provided
- Scenario
- Setting up advanced field calculations in a dynamic PDF form to categorize and sum costs based on dropdown selections.
- Observed behavior
- Requires a script or method to conditionally sum specific row totals based on the string value ('Internal' or 'External') selected in the corresponding dropdown.
Ensure your PDF form fields are named systematically (e.g., Activity1, Total1, Activity2, Total2) so that a calculation loop can reference them iteratively.
Use a Custom Calculation Script for Condition-Based Totals
Implement a JavaScript loop in your total fields that iterates through all form rows, checks the activity type dropdown, and sums up the matching values.
To achieve conditional calculations in a PDF form, standard sum functions are not sufficient. You must use a custom JavaScript snippet that evaluates each row's dropdown selection before adding its value to the cumulative total.
Open your PDF form in editing mode, right-click the 'Internal Total' field, and select 'Properties' from the context menu.
Navigate to the 'Calculate' tab, choose 'Custom calculation script', click 'Edit', and input the following code: var total = 0; for (var i = 1; i <= 14; i++) { if (this.getField("Activity" + i).valueAsString == "Internal") { total += Number(this.getField("Total" + i).valueAsString); } } event.value = total;
Repeat the previous steps for your 'External Total' field, but modify the condition in the script to check for 'External' instead: if (this.getField("Activity" + i).valueAsString == "External").
Open the properties for the 'Grand Total' field, go to the Calculate tab, select 'Value is the sum of the following fields', and pick both your Internal Total and External Total fields.

Create and Calculate PDF Forms effortlessly with WPS Office
WPS Office provides robust PDF editing capabilities, allowing you to easily build interactive forms, insert text fields, dropdowns, and manage advanced mathematical properties.
- 1. Open your document: Launch WPS Office, switch to the PDF module, and open your form document.
- 2. Access form tools: Click on the 'Form' tab in the top ribbon to reveal all interactive field tools.
- 3. Insert form elements: Use the 'Text Field' and 'Combo Box' options to build out your rows, ensuring you name them sequentially.
- 4. Set calculation properties: Double-click your total fields to open properties, navigate to the calculation settings, and apply your logic.

Frequently Asked Questions
Why is my PDF form calculation not updating automatically?
PDF form calculations execute in a specific order based on their creation or defined hierarchy. If your Grand Total relies on the Internal Total, ensure the Field Calculation Order is set correctly in your PDF form properties so row totals calculate first.
Do I need JavaScript for the Grand Total as well?
No, you do not need JavaScript for the final addition. Because the Grand Total simply adds two existing fields (Internal Total and External Total), you can use the built-in 'Value is the sum of the following fields' calculation option.
What happens if a row's total field is left empty by the user?
When using the `Number()` wrapper in JavaScript, an empty string is treated as a mathematical zero. Empty fields in your 14 rows will not break the script or cause calculation errors.




