How to Create Conditional Mileage Calculations in a PDF Form
Question details
The user needs to extract a total mileage value and distribute it across different form fields using conditional logic.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Building an interactive expense or mileage reimbursement PDF form where values fall into specific tiers.
- Observed behavior
- The form must automatically evaluate the 'Total Mileage' input and output the corresponding adjusted value into specific tier-based text fields (e.g., 11-60 miles, 61+ miles).
Verify the exact name of your primary input field (e.g., 'Total Mileage') before writing your calculation scripts, as PDF JavaScript references are case-sensitive.
Apply Custom JavaScript for Conditional Mileage Fields
Use JavaScript within your PDF form field properties to calculate and display mileage automatically based on the inputted total.
PDF forms support custom JavaScript logic to dynamically calculate values. By extracting the value from a primary field, you can use conditional 'if/else' statements to determine exactly what should be displayed in the target fields.
In your PDF form editor, right-click the target field (for example, '11-60 Miles Qty') and choose 'Properties' from the context menu.
In the Text Field Properties dialog box, click on the 'Calculate' tab to view the automated calculation options.
Select the 'Custom calculation script' radio button and click 'Edit'. Input the following code: var mileage = Number(this.getField("Total Mileage").valueAsString); if (mileage > 10 && mileage <= 60) event.value = mileage - 10; else event.value = "";
Repeat the process for your '61+ Miles' field. In its custom calculation script window, enter: var mileage = Number(this.getField("Total Mileage").valueAsString); if (mileage >= 61) event.value = mileage - 10; else event.value = "";

Create and Edit Interactive PDF Forms with WPS PDF
WPS Office includes a fully featured PDF editor that supports interactive form creation, text fields, and custom calculations to streamline your automated workflows.
- 1. Open your PDF: Launch WPS Office and open your PDF document. Navigate to the 'Form' tab on the top ribbon.
- 2. Insert or select a field: Click on 'Text Field' to insert a new form element, or double-click an existing text field to open its Properties panel.
- 3. Enable calculations: In the Properties window, go to the 'Calculate' tab and select 'Custom calculation script'.
- 4. Apply your logic: Paste your conditional JavaScript code into the editor to automate calculations instantly.

Frequently Asked Questions
Why is my PDF form calculation not updating when I enter the total?
You may need to check the field calculation order. In PDF editors, calculations execute in a specific sequence. Ensure the 'Total Mileage' field calculates before the dependent tier fields evaluate their values.
Can I hide the result field entirely if the mileage condition is not met?
Yes. The provided script assigns an empty string ("") when the conditional limits are not met. This effectively leaves the field completely blank and prevents any negative or unwanted numbers from appearing.
Does custom JavaScript in PDF forms work in web browsers?
While fully featured desktop PDF editors like WPS PDF and Adobe Acrobat support robust JavaScript calculations, many default web browser PDF viewers offer limited or no support for custom JavaScript logic. It is recommended to fill out calculated forms using a dedicated desktop PDF application.




