PDF Editing Issues

How to Add Custom JavaScript for Quantity-Based Pricing in PDF Forms

Ayan MasoodAyan Masood Nov 5, 2026 870 views

Question details

Implement dynamic pricing calculations based on a quantity field in a PDF order form.

How to Add Custom JavaScript for Quantity-Based Pricing in PDF Forms
Product
PDF
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.
Before you start

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.

Solution 1Recommended

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.

1
Access form field properties

Open your PDF form in editing mode, right-click the field where the total cost will be displayed, and select 'Properties'.

2
Navigate to the Calculate tab

In the Properties window, click on the 'Calculate' tab and select the 'Custom calculation script' option.

3
Enter the custom script

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; }

4
Save and test

Click OK and close the properties panel. Switch to preview mode and enter different quantities to ensure the total updates correctly.

Apply a Flat Discount to the Entire Order
Field Names: Make sure to replace 'TotalQty' in the script with the actual name of the quantity field used in your specific document.
Efficient PDF Form Management

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. 1. Open your PDF document: Launch WPS Office and open the PDF file you wish to turn into an interactive order form.
  2. 2. Navigate to the Form tab: Click on the 'Form' tab located on the top ribbon to access the interactive form controls.
  3. 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. 4. Adjust properties: Double-click the inserted fields to rename them, adjust their appearance, and configure basic data formatting.
  5. 5. Save and share: Save the finished interactive PDF form and distribute it to your clients for easy ordering.
Easily insert text fields, checkboxes, and drop-down lists into PDFsHigh compatibility with standard PDF formats and Adobe Acrobat formsLightweight application that runs smoothly on Windows, Mac, and LinuxFeature-rich interface designed for both beginners and advanced users
microsoft office alternative - wps office

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.