PDF Editing Issues

How to Calculate Mileage Amounts in a PDF Form Using JavaScript

Camila MilosovichCamila Milosovich Nov 2, 2026 869 views

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.

How to Calculate Mileage Amounts in a PDF Form Using JavaScript
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.
Before you start

Ensure all your individual mileage input fields follow a consistent naming convention (e.g., MilageRow1, MilageRow2) so the script can correctly loop through them.

Solution 1Recommended

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).

1
Access Field Properties

Open your PDF form in editing mode, right-click the designated 'Total' field (e.g., Sum16), and select 'Properties' from the context menu.

2
Navigate to the Calculate Tab

In the Field Properties dialog box, click on the 'Calculate' tab to view the calculation options for that specific field.

3
Enter the Custom Script

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;

4
Verify Field Names

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.

5
Save and Test

Click 'OK' to save the script, close the properties window, and test the calculation by entering sample numbers into the mileage fields.

Use a Custom Calculation Script for Mileage Totals
Case Sensitivity: JavaScript is extremely case-sensitive. A mismatch as small as a lowercase letter or a missing space will cause the script to fail silently, resulting in no calculation.
Advanced PDF Form Tools

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. 1. Open the PDF Tool: Launch WPS Office and open your PDF document using the built-in PDF editor.
  2. 2. Navigate to Form Editing: Click on the 'Form' tab in the top ribbon to access all interactive form controls.
  3. 3. Add Form Fields: Select 'Text Field' to insert input boxes for your mileage data and your final total.
  4. 4. Configure Field Properties: Double-click the total field to open its properties, navigate to the calculation settings, and apply your mathematical operations.
Easily insert and customize interactive text fields, checkboxes, and drop-downsConfigure automatic field calculations for invoices and expense reportsHighly compatible with Adobe Acrobat form fields and PDF standardsLightweight, fast, and user-friendly interface for all your document needs
microsoft office alternative - wps office

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.