How to Use JavaScript Variables to Calculate PDF Form Values
Question details
The user wants to write a custom calculation script for a PDF form that evaluates the text values of multiple fields, maps specific combinations to a numeric value, and multiplies it by a quantity field.

- Product
- PDF Forms
- Device & OS
- not provided
- Scenario
- Creating interactive PDF forms with dynamic calculations based on conditional text field values.
- Observed behavior
- The user needs the correct JavaScript syntax to compare string values from different form fields and perform a multiplication operation based on the matched conditions.
Ensure your PDF editor supports custom JavaScript calculations for form fields, and write down the exact names of the form fields you need to reference before writing the script.
Use a Custom JavaScript Calculation Script
Add a custom JavaScript code snippet to the properties of the result field to evaluate specific text conditions and calculate the final value.
When dealing with complex calculations involving text fields (like '2KG' or 'PAL'), you must first compare these values as strings. Once the condition is met, you can convert the quantity field into a number and multiply it by your desired factor to output the final calculation.
Right-click the result field where you want the calculated total to appear and select 'Properties' from the context menu.
Go to the 'Calculate' tab in the properties window and select the option 'Custom calculation script'.
Click the 'Edit' button to open the JavaScript editor. Use the property `this.getField("FieldName").valueAsString` to accurately capture and compare text values.
Input the script: `if (this.getField("peso01").valueAsString == "2KG" && this.getField("uni01").valueAsString == "PAL") { event.value = Number(this.getField("quant01").valueAsString) * 5; }`. Replace the values with your actual business rules.
Click 'OK' to save your script, close the properties window, and test your form by entering the specified text values to see if the calculation runs properly.

Edit and Manage Interactive PDF Forms with WPS Office
WPS Office provides robust PDF editing capabilities, making it easy to create, fill out, and manage interactive forms without extensive technical knowledge.
- 1. Open your PDF document: Launch WPS Office and open the PDF file that contains the form you wish to edit.
- 2. Access the Form tab: Navigate to the 'Form' tab on the top ribbon to access the various interactive form controls.
- 3. Modify form fields: Add, resize, or modify text fields, checkboxes, and combo boxes according to your data collection needs.
- 4. Save and share: Save your newly edited interactive PDF to share it securely with clients or colleagues.

Frequently Asked Questions
Why is my PDF JavaScript calculation not working?
This usually happens if the field names in your script do not exactly match the actual field names in the form (case-sensitive), or if the calculation order of the fields is incorrect. Double-check your spelling and test the script for syntax errors.
How do I ensure a field value is treated as text in PDF JavaScript?
Use the `.valueAsString` property instead of just `.value`. This guarantees that the content of the field is evaluated strictly as a string, preventing unexpected type conversion errors during conditional comparisons.
Can I use 'else if' for multiple conditions in a PDF form calculation?
Yes, you can chain multiple `else if` statements or use a `switch` statement in your custom calculation script to handle various combinations of text inputs and apply different multiplier values accordingly.
How do I change the calculation order of fields in a PDF?
In most advanced PDF editors, you can adjust the calculation order by entering form editing mode, selecting 'More Options' or 'Tools', and choosing 'Set Field Calculation Order'. This ensures that dependent fields calculate in the right sequence.




