Change PDF Form Field Color Based on Text Input
Question details
The user needs to dynamically change the color of a specific PDF form field depending on whether multiple designated text fields are filled or empty.

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating an interactive PDF form that provides visual feedback (like red or green indicators) based on the completion status of required text fields.
- Observed behavior
- A target field needs to turn green when the 'morning', 'afternoon', and 'evening' fields all contain text, and turn red if any of these fields are left empty.
Ensure you have access to a PDF editor that supports JavaScript custom calculations, and verify the exact names of the text fields you intend to monitor (e.g., 'am', 'pm', 'eve').
Use a Custom Calculation Script for Fill Color
Apply a JavaScript calculation to update the field's background color dynamically based on the input values of referenced fields.
By adding a custom calculation script to the target field, you can continuously monitor the status of the 'am', 'pm', and 'eve' text fields and update the background color accordingly.
Open your PDF in form editing mode, right-click the field whose color should change, and select 'Properties' from the context menu.
In the Properties dialog box, click on the 'Calculate' tab and choose the 'Custom calculation script' option.
Click the 'Edit' button and enter the following script: event.target.fillColor = (this.getField('am').valueAsString != '' && this.getField('pm').valueAsString != '' && this.getField('eve').valueAsString != '') ? color.green : color.red;
Click 'OK' to save the script, close the properties window, and fill out the form fields to verify the target field's background color turns green or red as expected.

Use a Custom Calculation Script for Border Color
Change the stroke (border) color of the field instead of the background fill color using a modified conditional script.
Create and Customize Interactive PDF Forms with WPS Office
WPS Office offers a comprehensive suite of PDF tools, making it easy to create fillable forms, adjust field properties, and manage interactive document components without hassle.
- 1. Open PDF File: Launch WPS Office and open your PDF document.
- 2. Access Form Tools: Navigate to the 'Form' tab located on the top ribbon.
- 3. Insert or Edit Fields: Click 'Text Field' to insert new fields, or double-click existing fields to open their configuration menus.
- 4. Adjust Properties: Modify appearance, format, and calculation settings within the field properties dialog.
- 5. Save the Document: Save your interactive PDF document to lock in the form behaviors.

Frequently Asked Questions
Why isn't my form field color updating automatically?
Ensure that JavaScript is enabled in your PDF viewer's preferences. Also, verify that the field names referenced in your custom script exactly match the actual field names assigned to your text boxes, as they are case-sensitive.
Can I apply this color change to multiple groups of fields on the same page?
Yes. After applying the script, you can copy the field group. However, you must rename the copies or use the 'Create Multiple Copies' feature to ensure each new group references its own unique field names rather than the original ones.
Why does changing the stroke color not show any visible change on the form?
If you are using a script to change the 'strokeColor' (border color), the target field's border width must be set to greater than zero in its Appearance properties. If the width is set to zero, the border remains invisible regardless of the assigned color.




