How to Change PDF Form Font Color Based on a Number
Question details
The user wants to apply conditional formatting to a PDF form field so that the text color changes automatically depending on the numeric value entered (e.g., black for under 10, yellow for 10-19, red for 20 and above).

- Product
- PDF Editor
- Device & OS
- not provided
- Scenario
- Creating or modifying an interactive PDF form that requires visual cues based on specific numerical data ranges entered by respondents.
- Observed behavior
- The goal is to have the form field dynamically update its font color based on mathematical conditions applied to the user's input.
Ensure you are using a PDF editor that supports custom JavaScript for form fields, and verify that the target field is set up to accept numeric data.
Use Custom JavaScript for Form Field Validation
Apply a custom JavaScript snippet to the PDF form field's properties to evaluate the entered number and change the text color accordingly.
By converting the field value to a number, you can use an if/else logic statement to test the value against your defined ranges. The script can be applied as a calculation, formatting, or validation script.
Right-click on the specific text field in your PDF form and select 'Properties' from the context menu.
Switch to the 'Format' or 'Validate' tab within the Properties dialog box.
Choose 'Custom' or check the box for 'Run custom validation script' depending on which tab you are using.
Input the following script: var n = Number(event.value); if (n <= 9) event.target.textColor = color.black; else if (n < 20) event.target.textColor = color.yellow; else event.target.textColor = color.red;
Click 'OK' to save the script, close the properties window, and enter different numbers into the field to verify the text color changes correctly.

Easily Create and Edit Interactive PDF Forms with WPS Office
WPS Office provides a comprehensive suite of PDF tools, allowing you to seamlessly view, edit, and manage fillable PDF forms without needing heavy or expensive software.
- 1. Open your PDF form: Launch WPS Office and open your interactive PDF document.
- 2. Access form editing tools: Navigate to the 'Form' tab on the top ribbon to view all form fields.
- 3. Modify field properties: Double-click any text field to access its properties, where you can adjust standard text colors, fonts, and appearance.
- 4. Save your document: Click the 'Save' icon or press Ctrl+S to preserve your updated PDF form settings.

Frequently Asked Questions
Can I change a PDF form field color without using JavaScript?
You can change the default, static font color of a form field through its Appearance properties in any standard PDF editor. However, to dynamically change the color based on the value entered (conditional formatting), JavaScript is required.
Why is my PDF JavaScript color code not updating the text immediately?
If the color doesn't update until you click the field again, you may have placed the script in the 'Calculate' tab without a trigger. Placing the script in the 'Validate' or 'Format' tab ensures it executes as soon as the value is committed.
What standard color names can I use in PDF JavaScript?
Standard color arrays built into Acrobat JavaScript include color.black, color.white, color.red, color.green, color.blue, color.cyan, color.magenta, and color.yellow. For custom colors, you can use the syntax ['RGB', r, g, b].




