How to Create a JavaScript Tax Calculation for PDF Form Radio Buttons
Question details
The user needs to implement a custom JavaScript calculation in a PDF form to compute tax based on selected radio button options.

- Product
- Device & OS
- not provided
- Scenario
- Building an interactive PDF invoice or order form that requires conditional tax calculations, such as standard tax, tax-exempt, or a custom manual tax input.
- Observed behavior
- The user wants to dynamically calculate 5% tax, 0 tax, or allow manual entry in a specific Tax field depending on the active radio button selection.
Ensure your PDF form fields are correctly named before adding the script. Specifically, verify the base amount field is named 'Total' and your radio button group is named 'TaxType'.
Add a Custom JavaScript Calculation to the Tax Field
Use a custom calculation script in the Tax field's properties to evaluate the selected radio button and apply the corresponding tax formula or allow manual input.
This solution utilizes standard Acrobat JavaScript. It relies on a switch statement to check the export value of the selected radio button. By default, it locks the field for automatic calculation, but unlocks it if the manual entry option is selected.
Open your document in PDF form preparation mode. Double-click the 'TaxType' radio buttons to open their Properties. Under the Options tab, ensure their Export Values are set to 'Choice1' (for 5%), 'Choice2' (for zero), and 'Choice3' (for manual entry).
Right-click the 'Tax' text field where the result will appear, select 'Properties', and navigate to the 'Calculate' tab.
Select the 'Custom calculation script' option and click 'Edit'. Paste the following JavaScript code: var nTotal = Number(this.getField("Total").value); var nChoice = this.getField("TaxType").value; event.target.readonly = true; switch (nChoice) { case "Choice1": event.value = 0.05 * nTotal; break; case "Choice2": event.value = 0; break; case "Choice3": event.rc = false; event.target.readonly = false; break; default: event.value = ""; break; }
Click 'OK' to close the JavaScript editor, then close the Properties dialog. Exit form editing mode and click the radio buttons to ensure the tax calculates correctly based on the 'Total' field.

Easily Manage and Edit Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive PDF editor that lets you view, fill, and manage interactive PDF forms with ease. It perfectly processes standard form fields and calculations without complex configurations.
- 1. Open the PDF File: Launch WPS Office and open your interactive PDF form.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to access robust form filling and editing tools.
- 3. Fill and Calculate: Input your data into the form fields. WPS PDF will automatically process embedded JavaScript calculations to display accurate results.

Frequently Asked Questions
Why isn't my PDF JavaScript calculation updating automatically?
The field calculation order might be incorrect. Ensure that the 'Total' field is calculated before the 'Tax' field. You can adjust this by going to the form editing mode, selecting 'More', and choosing 'Set Field Calculation Order'.
Can I change the tax rate from 5% to a different percentage?
Yes, locate the line 'event.value = 0.05 * nTotal;' in the script and change '0.05' to your desired decimal rate (for example, use '0.08' for an 8% tax rate).
How do I group radio buttons together correctly for this script?
All radio buttons in a specific group must share the exact same Name (e.g., 'TaxType' in the General tab), but each button must have a unique 'Radio Button Choice' or 'Export Value' under the Options tab.
Why can't I type manually when Choice3 is selected?
If manual entry fails, ensure that the Tax field does not have the 'Read Only' box permanently checked in its General properties tab. The script controls the read-only state dynamically, but a permanent setting may override it.




