Other PDF Issues

Calculate Internal and External PDF Form Totals with JavaScript

Muhammad TalhaMuhammad Talha Nov 3, 2026 869 views

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.

Calculate Internal and External PDF Form Totals with JavaScript
Product
PDF
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.
Before you start

Ensure your PDF form fields are named systematically (e.g., Activity1, Total1, Activity2, Total2) so that a calculation loop can reference them iteratively.

Solution 1Recommended

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.

1
Access field properties

Open your PDF form in editing mode, right-click the 'Internal Total' field, and select 'Properties' from the context menu.

2
Enter the custom script

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;

3
Configure the External 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").

4
Calculate the Grand Total

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.

Use a Custom Calculation Script for Condition-Based Totals
Field Naming Convention: The provided JavaScript relies on strict field naming. Your dropdown fields must be exactly named Activity1 through Activity14, and your cost fields Total1 through Total14 for the loop to function.
Advanced PDF Form Editing

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. 1. Open your document: Launch WPS Office, switch to the PDF module, and open your form document.
  2. 2. Access form tools: Click on the 'Form' tab in the top ribbon to reveal all interactive field tools.
  3. 3. Insert form elements: Use the 'Text Field' and 'Combo Box' options to build out your rows, ensuring you name them sequentially.
  4. 4. Set calculation properties: Double-click your total fields to open properties, navigate to the calculation settings, and apply your logic.
Intuitive tools for creating and editing interactive PDF formsSupports advanced form field properties, formulas, and formattingHighly compatible with standard Adobe PDF formats and scriptsLightweight application with a familiar, user-friendly interface
microsoft office alternative - wps office

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.