How to Change an Acrobat Text Box Color Based on a Numeric Value
Question details
The user needs to dynamically change the background color of a PDF text field based on the numerical value entered into it using JavaScript.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating dynamic interactive PDF forms where visual feedback, such as a background color change, is required when a user enters specific numerical values.
- Observed behavior
- The user wants the text field's fill color to update automatically based on the underlying raw numeric value, rather than the formatted display string.
Ensure you have access to the 'Prepare Form' tool in your PDF editor and possess basic familiarity with adding custom JavaScript to form fields.
Apply a Custom JavaScript Calculation to the Text Field
Use a custom calculation or validation script to evaluate the numeric value of the field and dynamically update its fillColor property.
When dealing with formatted text fields, the PDF engine separates the actual numeric value from the formatted display string. Your JavaScript must evaluate the raw numeric value to trigger the correct background color change.
It is crucial to test your script against various input scenarios, including empty fields, invalid non-numeric entries, positive numbers, and negative numbers, to prevent script errors during data entry.
Open your PDF document in Adobe Acrobat and navigate to Tools, then select 'Prepare Form'.
Right-click the target text box that requires dynamic formatting and select 'Properties' from the context menu.
Navigate to the 'Calculate' tab, select the 'Custom calculation script' radio button, and click the 'Edit' button.
Type your conditional logic evaluating event.value. For example, use 'if (Number(event.value) < 0) event.target.fillColor = color.red; else event.target.fillColor = color.transparent;'.
Click 'OK' to save the script, close the properties window, exit form editing mode, and test the field by entering various numeric values.

Edit PDFs and Manage Forms Easily with WPS Office
While complex JavaScript is needed for dynamic conditional colors in some editors, WPS Office provides an intuitive PDF editor that lets you easily fill forms, customize text box styles, and manage PDF documents without coding.
- 1. Download and open WPS Office: Launch WPS Office and click on the 'PDF' tab from the main dashboard.
- 2. Open your PDF document: Import your PDF file and navigate to the 'Form' or 'Edit' tab depending on your required task.
- 3. Modify text boxes: Select any text box or form field to instantly access its properties panel and manually adjust background colors, borders, and fonts.
- 4. Save the document: Save your newly customized PDF file directly to your local drive or WPS Cloud for easy sharing.

Frequently Asked Questions
Why isn't my text box color changing when I type a number?
The script might be reading the formatted text string (like '$1,000') instead of the raw number. Ensure your JavaScript converts the field value to a strict number using Number(event.value) before evaluating the condition.
Can I use standard HTML HEX color codes in my PDF script?
No, Acrobat JavaScript typically uses its own color array format (e.g., ["RGB", r, g, b]) where the RGB values are expressed as fractions between 0 and 1, rather than standard HTML HEX codes or 0-255 integers.
Will this JavaScript color change work on mobile PDF viewers?
Custom JavaScript for dynamic styling is fully supported in robust desktop PDF editors, but many mobile PDF viewers, browsers, and lightweight third-party apps do not fully support complex JavaScript form calculations.




