How to Calculate Mileage Amounts in a PDF Form Using JavaScript
Question details
The user needs to calculate the total for 15 mileage fields in a fillable PDF form by multiplying each field's value by a specific rate (0.54) and displaying the final sum.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating an interactive fillable PDF expense or mileage log that requires automatic calculation of costs based on input values.
- Observed behavior
- The user attempted to paste a custom calculation script into the total field's properties, but the calculation is not executing or updating as expected.
Ensure all your individual mileage input fields follow a consistent naming convention (e.g., MilageRow1, MilageRow2) so the script can correctly loop through them.
Use a Custom Calculation Script for Mileage Totals
Add a JavaScript loop in the total field's calculation properties to dynamically fetch, sum, and multiply the values of all mileage fields.
To calculate across multiple sequenced fields, using a loop in a Custom Calculation Script is the most efficient method. This prevents you from having to manually add each field together.
The script will retrieve the value of each field, convert it to a number, add it to a running total, and finally multiply that total by your mileage rate (e.g., 0.54).
Open your PDF form in editing mode, right-click the designated 'Total' field (e.g., Sum16), and select 'Properties' from the context menu.
In the Field Properties dialog box, click on the 'Calculate' tab to view the calculation options for that specific field.
Select the 'Custom calculation script' radio button, click 'Edit', and paste the following code: var total = 0; for (var i = 1; i <= 15; i++) total += Number(this.getField("Milage 54mileRow" + i).valueAsString); event.value = total * 0.54;
Check that the base field name in the script (e.g., "Milage 54mileRow") exactly matches the text preceding the numbers in your actual form field names. Fix any spelling, spacing, or capitalization discrepancies.
Click 'OK' to save the script, close the properties window, and test the calculation by entering sample numbers into the mileage fields.

Create and Edit Fillable PDF Forms Easily with WPS Office
WPS Office provides a comprehensive suite of PDF tools, allowing you to easily create interactive fillable forms, manage field properties, and set up calculations without relying on complex external software.
- 1. Open the PDF Tool: Launch WPS Office and open your PDF document using the built-in PDF editor.
- 2. Navigate to Form Editing: Click on the 'Form' tab in the top ribbon to access all interactive form controls.
- 3. Add Form Fields: Select 'Text Field' to insert input boxes for your mileage data and your final total.
- 4. Configure Field Properties: Double-click the total field to open its properties, navigate to the calculation settings, and apply your mathematical operations.

Frequently Asked Questions
Why is my custom JavaScript calculation not working after pasting the code?
This is almost always caused by a mismatch between the field names in your script and the actual field names in your PDF form. Ensure that spelling, spaces, and capitalization match perfectly.
How do I change the mileage rate in the calculation script?
To update the rate, open the properties of your total field, go to the Calculate tab, edit the custom script, and change the multiplier at the end of the code (e.g., change 0.54 to 0.65).
Do I need to know JavaScript to create a calculating PDF form?
While complex logic requires custom JavaScript, most PDF editors provide built-in calculation functions for simple operations like sums, products, and averages, requiring zero coding experience.
Can I hide the calculation field if the total is zero?
Yes. You can add a conditional statement to your JavaScript to check if the total is zero. If it is, you can set event.value to an empty string ("") instead of zero.




