Automatically Display a Message Based on a PDF Form Total
Question details
The user wants to automatically display a specific text message in a PDF form depending on whether a calculated total field is positive, negative, or zero.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Setting up an interactive PDF form (such as an expense report) where a text field updates its message dynamically based on the calculated sum of other fields.
- Observed behavior
- Attempting to use a calculated field to show different messages (e.g., 'Employee Owes Company' vs 'Company Owes Employee'), but occasionally encountering errors like 'getField(...) is null' due to incorrect field names.
Ensure your PDF editor supports custom JavaScript calculations for form fields and verify the exact, case-sensitive name of the field containing your total.
Use a Text Field with a Custom Calculation Script
Apply a custom JavaScript calculation to a text field so it evaluates the total field's value and outputs the corresponding message.
Using a text field is more effective than a drop-down menu when the value is being determined automatically by a script. The script checks the total and assigns the correct string to the text field.
Check the exact name of the field that calculates your total (e.g., 'Total'). Remember that field names in JavaScript are strictly case-sensitive.
Add a new text field to your PDF form where you want the dynamic message to appear. Set this field to read-only if you do not want users editing the generated message.
Right-click the new text field, choose Properties, and navigate to the Calculate tab.
Select the option for 'Custom calculation script', click Edit, and paste the following code: var total = Number(this.getField("Total").valueAsString); event.value = total > 0 ? "Employee Owes Company" : total < 0 ? "Company Owes Employee" : "";

Create Dynamic PDF Forms with WPS Office
WPS Office provides robust PDF editing capabilities, making it easy to create interactive forms, add text fields, and configure custom JavaScript calculations for dynamic documents.
- 1. Open PDF Form: Launch WPS Office and open your PDF document containing the form fields.
- 2. Insert Form Fields: Navigate to the Form tab to insert the necessary text fields for your totals and dynamic messages.
- 3. Configure Properties: Double-click the message field to open its Properties, then set your custom calculation script under the Calculate tab.

Frequently Asked Questions
Why am I getting a 'getField(...) is null' error in my PDF script?
This error usually occurs because the field name referenced in your JavaScript does not match the actual field name in the document. Check for typos, extra spaces, and ensure the capitalization matches perfectly, as JavaScript is case-sensitive.
Can I use a drop-down menu instead of a text field for calculated messages?
While it is technically possible, it is much easier and more standard to use a text field. Since the value is being automatically determined by a script based on a total, a drop-down menu offers no functional benefit to the user.
How do I handle a total of zero in the custom script?
In the standard ternary operator script (e.g., total > 0 ? 'Message 1' : total < 0 ? 'Message 2' : ''), the final empty quotes ensure that if the total is exactly zero or left blank, the message field remains empty.




