PDF Editing Issues

Calculate a Total from Multiple PDF Form Fields with JavaScript

Phi Hung VoPhi Hung Vo Nov 5, 2026 869 views

Question details

The user needs to combine calculated individual row totals into one overall grand total field within a PDF timesheet or interactive form.

How to Calculate a Total from Multiple PDF Form Fields Using JavaScript
Product
PDF
Device & OS
not provided
Scenario
Creating a PDF timesheet or invoice where multiple row totals need to be accurately summed up into a final total field using custom calculation scripts.
Observed behavior
Individual row totals are being calculated correctly, but a combined overall total needs to be extracted and displayed in a separate field using JavaScript.
Before you start

Verify that all individual row fields in your PDF form are correctly named (e.g., Row1, Row2) and are formatted to process numbers so that the JavaScript engine can properly read their values.

Solution 1Recommended

Apply Custom JavaScript for Individual Rows and the Grand Total

Use custom calculation scripts in the field properties to correctly assign values using event.value and sum the row fields together.

When dealing with nested calculations in PDF forms, it is essential to assign the result of a field's calculation using event.value instead of event.target.value. This ensures the output is properly registered as the field's actual value, which can then be picked up by a master total field.

1
Modify the individual row scripts

Right-click an individual row total field and select Properties. Navigate to the Calculate tab. Ensure your custom calculation script assigns the final calculated value to 'event.value' rather than 'event.target.value'.

2
Create the grand total field

Add a new Text Field at the bottom of your timesheet or form where you want the final combined sum to appear. Right-click this new field and open its Properties.

3
Add the total calculation script

In the Properties window, go to the Calculate tab. Select the 'Custom calculation script' option and click the Edit button. Enter JavaScript to sum your rows, for example: 'event.value = this.getField("Row1").value + this.getField("Row2").value;'. Click OK to save.

4
Test the calculation

Close the Properties dialog and switch to the hand tool or preview mode. Input test numbers into your timesheet rows to verify that both the individual rows and the new grand total update instantly.

Apply Custom JavaScript for Individual Rows and the Grand Total
Check Field Calculation Order: If the grand total field lags one step behind your inputs, you need to fix the Field Calculation Order in your PDF editor. Ensure the individual row fields are placed higher in the calculation order list than the overall total field.
Advanced PDF Forms

Create and Calculate Interactive PDF Forms with WPS PDF

WPS Office includes a feature-rich PDF editor that makes it simple to create interactive forms, add text fields, and set up calculations for timesheets or invoices seamlessly.

  1. 1. Open your PDF in WPS Office: Launch WPS Office on your device and open the timesheet or invoice PDF document.
  2. 2. Access the Form tools: Navigate to the 'Form' tab located in the top ribbon to view all available interactive field options.
  3. 3. Add calculation fields: Insert text fields where needed. Double-click the fields to access the properties panel and set up basic mathematical calculations for your totals.
  4. 4. Save the interactive PDF: Once your fields and calculations are set, save the document. The interactive properties and automated calculations will be preserved for any user who opens the file.
Easily insert text fields, checkboxes, and calculation boxes without heavy coding.Fully compatible with standard PDF formats and Adobe Acrobat interactive forms.Lightweight and intuitive interface designed for smooth document editing.Provides a cost-effective alternative for comprehensive PDF manipulation.
microsoft office alternative - wps office

Frequently Asked Questions

What is the difference between event.value and event.target.value in PDF JavaScript?

In PDF form calculations, 'event.value' explicitly sets the calculated value of the current field triggering the script. 'event.target.value' refers to the value of the target object, which can cause erratic behavior or fail to pass the value to subsequent calculations in a multi-step form.

Why is my PDF grand total field not updating immediately after I input row data?

This happens when the Field Calculation Order is incorrect. A PDF calculates fields in a specific sequence. If the grand total is calculated before the individual rows, it will use the old row values. You must open the Field Calculation Order settings in your PDF editor and move the grand total field to the bottom of the list.

Can I calculate a total from multiple fields without using custom JavaScript?

Yes, most advanced PDF editors allow you to bypass coding. In the field's Properties under the Calculate tab, you can select 'Value is the sum (+) of the following fields:' and simply click a button to pick the specific row fields you want to combine.