PDF Editing Issues

How to Calculate Totals for Multiple PDF Form Rows with JavaScript

Natalie TaylorNatalie Taylor Nov 10, 2026 868 views

Question details

The user wants to calculate inventory row totals by multiplying the unit price and quantity across multiple rows in a PDF form using a single script.

Calculate Totals for Multiple PDF Form Rows with JavaScript
Product
PDF Editors
Device & OS
not provided
Scenario
Creating an interactive PDF inventory or invoice form that requires automated row total calculations.
Observed behavior
Needs a custom JavaScript calculation script to automate row multiplication without applying a calculation formula to each row individually.
Before you start

Ensure that all form fields in your PDF are correctly named with a consistent, sequential numbering system (e.g., PRICE1, PRICE2, U1, U2, QTY1, QTY2) before applying the calculation script.

Solution 1Recommended

Use a For-Loop JavaScript in Custom Calculation

Use a single custom calculation script with a for-loop to iterate through all rows, multiplying unit price by quantity dynamically.

Instead of assigning individual calculation scripts to dozens of rows, you can utilize a JavaScript loop. This iterates through a defined number of rows based on their sequential field names, instantly processing the math for your entire table.

1
Prepare the Field Names

Open your PDF form in Form Editing mode. Verify that your Unit fields are named sequentially like 'U1', 'U2', your Quantity fields are 'QTY1', 'QTY2', and your Total fields are 'PRICE1', 'PRICE2'.

2
Access Field Properties

Right-click a hidden calculation field or the grand total field, select 'Properties', and navigate to the 'Calculate' tab.

3
Insert the Custom Script

Choose 'Custom calculation script' and click 'Edit'. Enter the loop script: for (var i = 1; i <= X; i++) { this.getField("PRICE" + i).value = Number(this.getField("U" + i).value) * Number(this.getField("QTY" + i).value); }

4
Define the Row Count

Replace the 'X' in the script with the exact number of rows in your table (e.g., 34). Click 'OK' to save and close the properties window.

5
Test the Calculations

Exit form editing mode and enter sample numbers into the unit and quantity fields to ensure the row totals populate correctly.

Use a For-Loop JavaScript in Custom Calculation
Field Calculation Order: If you are calculating a grand total at the bottom of the page based on these row totals, ensure the loop script runs before the grand total calculation in the Field Calculation Order menu.
Edit and Manage PDF Forms Easily

Streamline Your PDF Form Management with WPS Office

WPS Office provides a robust PDF editor that allows you to view, fill, and manage interactive PDF forms seamlessly. It is a powerful, lightweight solution for handling complex PDF documents, supporting interactive elements and broad format compatibility.

  1. 1. Open WPS Office: Launch WPS Office and select the 'PDF' tab from the main home screen.
  2. 2. Load the PDF Document: Click 'Open' and browse your computer to load your interactive PDF form document.
  3. 3. Interact with Form Fields: Use the 'Form' tab tools to click into text fields, input data, and trigger embedded JavaScript calculations like row totals.
  4. 4. Save the Filled Form: Once your data is entered and calculated, click 'Menu' and select 'Save As' to securely export your filled PDF form.
Seamlessly open and fill interactive PDF forms with embedded calculations.Compatible with standard PDF form data, annotations, and signatures.Lightweight and fast PDF viewing and editing experience.Convert PDFs to Word, Excel, and PowerPoint effortlessly with high accuracy.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my PDF JavaScript calculation not updating automatically?

This usually happens if the field calculation order is incorrect. Go to your form editing tools and check the 'Field Calculation Order' to ensure the quantities and unit prices are processed before the row totals.

Can I use this script for addition instead of multiplication?

Yes. You can simply change the multiplication operator (*) in the script to an addition operator (+) if you need to sum values across columns instead.

Do I need to add this script to every single row's properties?

No, the primary advantage of using a loop is that you only need to apply this script to one custom calculation event. It will process all sequentially named rows at once.

Is JavaScript supported in all PDF viewers?

No, complex JavaScript form calculations are primarily supported in advanced PDF editors and comprehensive desktop PDF programs. Basic web browsers or lightweight mobile viewers may not execute these scripts correctly.