How to Populate a PDF Field with a Discount Amount or Percentage
Question details
The user needs to use JavaScript in a PDF form to calculate a final discount field based on either a fixed discount amount or a percentage field, ensuring both values are not applied simultaneously.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form where a user can enter a discount either as a flat amount or a percentage, and a script calculates the correct value to populate the final output field.
- Observed behavior
- The form requires custom calculation logic to check which discount field is populated, assign that value to the output field, and handle cases where both fields might be empty or zero.
Ensure your PDF editor supports advanced form field editing and custom JavaScript calculations, such as Adobe Acrobat Pro or WPS Office's advanced PDF tools.
Use Custom JavaScript for Field Calculation
Apply a custom JavaScript calculation script to the target field to read the input from the amount and percentage fields and determine which value to output.
To accomplish this, you need to set up a script on the output field (e.g., 'DiscountTotal') that checks the values of your two input fields (e.g., 'Amount' and 'Percentage'). The script evaluates which field is greater than zero and applies it.
Enter the form editing mode in your PDF software. Right-click on the target field where you want the final discount to appear and select 'Properties'.
In the Text Field Properties dialog box, click on the 'Calculate' tab to set up the field's calculation logic.
Select the 'Custom calculation script' option and click the 'Edit' button.
Enter the following script, replacing 'X' and 'Y' with the actual names of your Amount and Percentage fields respectively: var n = this.getField("X").value; if (n > 0) { event.value = n; } var m = this.getField("Y").value; if (m > 0) { event.value = m; } else if (n == 0) { event.value = ""; } Click 'OK' to save.

Create and Manage Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive suite of PDF tools, allowing you to easily create fillable forms, configure text fields, and set up interactive document layouts for your business needs.
- 1. Open your PDF Document: Launch WPS Office and open the PDF file you wish to turn into an interactive form.
- 2. Access Form Tools: Navigate to the 'Form' tab on the top ribbon to reveal the interactive field creation tools.
- 3. Insert Text Fields: Select 'Text Field' and draw the boxes where you want users to input their discount amount or percentage.
- 4. Edit Field Properties: Double-click the inserted fields to rename them, adjust their appearance, and set formatting rules for numbers or percentages.

Frequently Asked Questions
Why is my PDF form calculation not updating automatically?
This typically occurs if the form's auto-calculation setting is disabled or if the calculation order of the fields is incorrect. Check your PDF editor's form settings to ensure calculations are set to occur automatically, and verify that input fields are ordered before the calculation field.
How do I clear a field if a user mistakenly enters both a percentage and an amount?
You can add a custom validation or keystroke script to the input fields. For instance, you can script the 'Amount' field to clear the 'Percentage' field automatically when a value is entered, ensuring only one discount type is active at a time.
Can I use standard Excel formulas in PDF form fields?
No, PDF form calculations do not use standard Excel formulas. They rely on basic preset functions (like Sum, Product, Average) or require custom JavaScript for conditional logic, such as applying a discount based on multiple variables.




