How to Use JavaScript to Subtract VAT in PDF Forms Based on a Yes/No Field
Question details
The user wants to configure a PDF form field to conditionally subtract a VAT amount from a total price using a custom calculation script that triggers only when an exemption field is set to 'Yes'.

- Product
- Device & OS
- not provided
- Scenario
- Creating an advanced interactive PDF form that requires conditional mathematical calculations for billing or invoicing.
- Observed behavior
- The form needs a script that calculates the total minus VAT when 'Yes' is selected, but leaves the result field blank if the condition is not met.
Before applying custom scripts, ensure your PDF form fields are correctly named and that the fields containing the total price and VAT are properly formatted to accept numeric values.
Apply Custom JavaScript for Conditional VAT Subtraction
Use a custom calculation script within the result field's properties to check the value of the Yes/No field before executing the VAT subtraction.
PDF forms support JavaScript to handle complex conditional logic that standard calculation tools cannot perform. By assigning variables to your form fields, you can instruct the result field to verify the choice field's status before processing the math.
Open your PDF in Form Editing mode, right-click the designated result field, and select 'Properties' from the context menu.
Navigate to the 'Calculate' tab in the Properties window, select the 'Custom calculation script' option, and click the 'Edit' button.
Enter the following script, replacing FieldA (Total Price), FieldC (VAT), and FieldB (Yes/No field) with your actual field names: `var a = this.getField("FieldA"); var b = this.getField("FieldB"); var c = this.getField("FieldC"); if (b.value == "Yes") { event.value = a.value - c.value; } else { event.value = ""; }`
Click 'OK' to save the script, close the properties dialog, and test the form by toggling the Yes/No choice field to ensure the result calculates correctly.

Easily Manage and Edit PDF Forms with WPS Office
WPS Office provides a comprehensive suite of PDF tools, allowing you to view, edit, fill, and manage PDF forms seamlessly. Whether dealing with interactive fields or standard document editing, WPS Office offers a powerful and user-friendly experience.
- 1. Download and Install: Download WPS Office for free from the official website and complete the quick installation process.
- 2. Open Your PDF Form: Launch WPS Office and drag and drop your interactive PDF file directly into the workspace.
- 3. Fill and Manage Fields: Use the PDF tools to interact with form fields, input data, and select choices accurately.
- 4. Save and Export: Once your form is filled, save your document securely or export it as needed for sharing.

Frequently Asked Questions
Why is the JavaScript calculation not working in my PDF form?
This usually happens if the field names in the script do not exactly match the actual field names in the form properties. Check for typos, ensure the source fields are set to output numeric values, and confirm that the choice field's export value exactly matches the string (like 'Yes') used in the script's condition.
Can I use standard calculations instead of JavaScript for conditionally subtracting VAT?
No. Standard calculations built into basic PDF forms (such as Sum, Product, or Average) do not support conditional logic (IF/ELSE statements). You must use a custom JavaScript calculation script to check conditions before applying the math.
Does the word 'Yes' in the PDF script need to be case-sensitive?
Yes, JavaScript is completely case-sensitive. If your checkbox or dropdown exports 'Yes' with a capital 'Y', your script must check for 'Yes'. Using 'yes' or 'YES' in the script will cause the condition to fail.




