PDF Editing Issues

How to Compare PDF Form Fields and Change Colors with JavaScript

Khadija KhanKhadija Khan Nov 2, 2026 869 views

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.

How to Compare PDF Form Fields and Change Colors Using JavaScript
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.
Before you start

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.

Solution 1Recommended

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.

1
Open Form Field Properties

Enter form editing mode in your PDF editor. Right-click the target field (e.g., Field B) and select 'Properties'.

2
Navigate to the Calculate Tab

In the Properties dialog box, go to the 'Calculate' tab and select the option for 'Custom calculation script'.

3
Enter the JavaScript Code

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.

4
Verify Calculation Order

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.

Use JavaScript to Compare Values and Change Fill Color
Case-Sensitive Field Names: Make sure the field name inside this.getField("A") exactly matches the actual name of your first field, as JavaScript is case-sensitive.
Advanced PDF Form Handling

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. 1. Open PDF in WPS Office: Launch WPS Office and open your interactive PDF document.
  2. 2. Access Form Tools: Navigate to the 'Form' tab to access interactive form filling and management tools.
  3. 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.
Easily view and fill interactive PDF forms with reliable data retention.Highly compatible with Adobe Acrobat PDF formats and interactive elements.Lightweight software with a user-friendly interface for faster workflows.Seamlessly convert between PDF, Word, Excel, and PowerPoint.
microsoft office alternative - wps office

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.