How to Add Custom JavaScript for Quantity-Based Pricing in PDF Forms
Question details
Implement dynamic pricing calculations based on a quantity field in a PDF order form.

- Product
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF order form that requires automatic price calculation based on the total quantity of items the user wants to purchase.
- Observed behavior
- The form needs to dynamically charge $3 per item for quantities below 20, and $2.50 per item for quantities of 20 or more, using a custom calculation script.
Ensure your PDF editor supports JavaScript for form fields, and verify that the exact name of your quantity field (e.g., 'TotalQty') matches the references in your script.
Apply a Flat Discount to the Entire Order
Use a conditional script to apply the discounted price to all items if the total quantity reaches the defined threshold.
This approach uses a straightforward if/else statement. If the quantity is 19 or less, it multiplies the quantity by the standard price. If it is 20 or more, the entire quantity is multiplied by the discounted price.
Open your PDF form in editing mode, right-click the field where the total cost will be displayed, and select 'Properties'.
In the Properties window, click on the 'Calculate' tab and select the 'Custom calculation script' option.
Click the 'Edit' button and input the following JavaScript: var qty = Number(this.getField("TotalQty").value); if (qty <= 19) { event.value = qty * 3; } else { event.value = qty * 2.5; }
Click OK and close the properties panel. Switch to preview mode and enter different quantities to ensure the total updates correctly.

Calculate Tiered Pricing Discounts
Use this method if you only want to apply the discounted price to the specific items that exceed the threshold quantity, while keeping the standard price for the initial units.
Create and Edit PDF Forms Seamlessly with WPS Office
WPS Office provides a comprehensive PDF toolkit that lets you easily fill, create, and manage interactive PDF forms without the need for complex programming.
- 1. Open your PDF document: Launch WPS Office and open the PDF file you wish to turn into an interactive order form.
- 2. Navigate to the Form tab: Click on the 'Form' tab located on the top ribbon to access the interactive form controls.
- 3. Add form fields: Select the required form controls (such as Text Fields for quantity and totals) and click on the document to place them.
- 4. Adjust properties: Double-click the inserted fields to rename them, adjust their appearance, and configure basic data formatting.
- 5. Save and share: Save the finished interactive PDF form and distribute it to your clients for easy ordering.

Frequently Asked Questions
Why isn't my PDF JavaScript calculation updating automatically?
This usually happens if the calculation order is incorrect. In your PDF editor, check the form properties to ensure the quantity field is evaluated before the total cost field. Also, verify that auto-calculation is enabled in your PDF viewer preferences.
Can I apply multiple pricing tiers using JavaScript?
Yes, you can use multiple 'if...else if' statements to create various pricing brackets. For example, you can set one price for 1-19 items, another for 20-49, and a final price for 50+ items.
What happens if a user enters text instead of numbers in the quantity field?
Using the 'Number()' function in your script safely converts text strings into a number or returns 0 if it cannot be parsed. This prevents script errors and ensures the total shows $0 instead of breaking the form.




