PDF Editing Issues

How to Calculate Tax Automatically in PDF Forms with Custom JavaScript

Steve KSteve K Oct 28, 2026 869 views

Question details

The user needs to calculate the tax amount automatically based on the 'Subtotal Parts' and 'Subtotal Labor' fields using a custom JavaScript in a PDF form.

How to Calculate Tax Automatically in PDF Forms with Custom JavaScript
Product
PDF Editor
Device & OS
not provided
Scenario
Creating an interactive PDF invoice or form that requires automatic tax calculation from multiple subtotal fields.
Observed behavior
The user's initial script attempts to add the field objects directly rather than their numeric values, resulting in incorrect calculations or errors.
Before you start

Ensure you have your PDF form open in a professional PDF editor with form editing capabilities and verify the exact names of your subtotal fields before writing the script.

Solution 1Recommended

Use Custom Calculation Script with Numeric Values

Correctly extract the numeric values from the source fields as strings, convert them to numbers, and apply the mathematical formula to calculate the tax.

When working with JavaScript in PDF forms, simply adding the field objects will concatenate them or throw errors. You must explicitly get the field's value as a string and convert it into a numeric format before performing mathematical operations like calculating a percentage.

1
Open Field Properties

Right-click the tax field where you want the result to appear and select 'Properties' from the context menu.

2
Navigate to the Calculate Tab

In the Field Properties dialog box, click on the 'Calculate' tab.

3
Select Custom Calculation Script

Choose the option 'Custom calculation script' and click the 'Edit' button to open the JavaScript editor.

4
Input the JavaScript Code

Enter the following code (assuming an 8% tax rate): var parts = Number(this.getField("Subtotal Parts").valueAsString); var labor = Number(this.getField("Subtotal Labor").valueAsString); event.value = Math.round((parts + labor) * 0.08 * 100) / 100;

5
Save and Test

Click 'OK' to save the script, close the Properties window, and enter test values into the Subtotal Parts and Labor fields to verify the calculation.

Use Custom Calculation Script with Numeric Values
Check Field Names: Ensure that 'Subtotal Parts' and 'Subtotal Labor' exactly match the names of the fields in your PDF, including spaces and capitalization.
Powerful PDF Form Editor

Create and Edit Interactive PDF Forms with WPS Office

WPS Office provides a comprehensive PDF toolkit that allows you to view, fill, edit, and manage interactive PDF forms effortlessly. Create professional documents without the high costs of other PDF editors.

  1. 1. Open PDF in WPS Office: Launch WPS Office and open your PDF file using the built-in WPS PDF tool.
  2. 2. Access Form Tools: Navigate to the 'Form' tab in the top menu to view all interactive form editing options.
  3. 3. Edit Form Fields: Double-click on any text field to modify its properties, set up calculations, or adjust its formatting according to your needs.
  4. 4. Save the Document: Click 'Save' or press Ctrl+S to preserve your form settings and calculations.
Easily edit PDF text, images, and form fields.Seamlessly convert between PDF and Word/Excel formats.Cross-platform support for Windows, Mac, iOS, and Android.Highly compatible with standard PDF documents and interactive forms.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my PDF calculation script returning an error?

This usually happens if the script references incorrect field names or tries to calculate field objects directly instead of extracting their numeric values using the valueAsString method.

How do I change the tax rate in the JavaScript?

In the provided script, locate the decimal multiplier (e.g., 0.08 for 8%). Change it to your required rate, such as 0.05 for 5% or 0.10 for 10%.

What does Math.round do in the PDF calculation?

Using Math.round(value * 100) / 100 ensures that the calculated tax is accurately rounded to two decimal places, which is the standard format for displaying currency.