How to Compare PDF Form Fields and Change Colors with JavaScript
Question details
The user needs to compare numerical values between two PDF form fields and dynamically change a field's background color based on whether the values match.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating dynamic and interactive PDF forms with automated visual feedback.
- Observed behavior
- The target field should turn green when values match, and red (or transparent) when they differ, based on a custom JavaScript calculation.
Ensure you have a PDF editor that supports form field creation and JavaScript execution. Verify that both fields you intend to compare are formatted to accept numeric values.
Use JavaScript to Compare Values and Change Fill Color
Add a custom calculation script to the target field's properties to evaluate values and dynamically apply a fill color.
This method uses standard PDF JavaScript APIs to conditionally change the fillColor property of a form field. The script compares the value of the current field against another specific field and updates the visual state accordingly.
Enter form editing mode in your PDF editor. Right-click the target field (e.g., Field B) and select 'Properties'.
In the Properties dialog box, go to the 'Calculate' tab and select the option for 'Custom calculation script'.
Click 'Edit' and paste the following script: if (event.value == this.getField("A").value) { event.target.fillColor = color.green; } else { event.target.fillColor = ["T"]; }. Replace ["T"] with color.red if you prefer a red background instead of transparent when values differ.
Ensure that the calculation order is correct, meaning Field A is calculated before the script in Field B executes. Save the form and test by entering numeric values.

Easily Manage Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive PDF toolkit that lets you view, fill out, and manage interactive PDF forms seamlessly. It maintains high compatibility with complex form structures.
- 1. Open PDF in WPS Office: Launch WPS Office and open your interactive PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab to access interactive form filling and management tools.
- 3. Fill and Save: Enter your data into the interactive fields. The software will respect built-in calculation scripts. Click 'Save' to preserve your inputs.

Frequently Asked Questions
Why isn't my PDF form color changing when values match?
This usually happens due to an incorrect calculation order or mismatched field names. Ensure JavaScript is enabled in your PDF viewer and that the field name in your script precisely matches the properties of the target field.
Can I use Hex color codes in PDF JavaScript?
Standard PDF JavaScript uses predefined color objects (e.g., color.red, color.green) or arrays (e.g., ["RGB", 1, 0, 0]). Using standard HTML hex codes directly won't work without converting them into a supported color array first.
Does the custom calculation script run automatically?
Yes, if placed in the Custom Calculation Script section of the field properties, the script will trigger automatically whenever the form's field values are updated or recalculated.




