How to Calculate Tax Automatically in PDF Forms with Custom JavaScript
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.

- 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.
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.
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.
Right-click the tax field where you want the result to appear and select 'Properties' from the context menu.
In the Field Properties dialog box, click on the 'Calculate' tab.
Choose the option 'Custom calculation script' and click the 'Edit' button to open the JavaScript editor.
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;
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.

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. Open PDF in WPS Office: Launch WPS Office and open your PDF file using the built-in WPS PDF tool.
- 2. Access Form Tools: Navigate to the 'Form' tab in the top menu to view all interactive form editing options.
- 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. Save the Document: Click 'Save' or press Ctrl+S to preserve your form settings and calculations.

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.




