How to Calculate Totals for Multiple PDF Form Rows with JavaScript
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.

- 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.
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.
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.
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'.
Right-click a hidden calculation field or the grand total field, select 'Properties', and navigate to the 'Calculate' tab.
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); }
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.
Exit form editing mode and enter sample numbers into the unit and quantity fields to ensure the row totals populate correctly.

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. Open WPS Office: Launch WPS Office and select the 'PDF' tab from the main home screen.
- 2. Load the PDF Document: Click 'Open' and browse your computer to load your interactive PDF form document.
- 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. Save the Filled Form: Once your data is entered and calculated, click 'Menu' and select 'Save As' to securely export your filled PDF form.

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.




