How to Highlight Negative Numbers in PDF Forms Using JavaScript
Question details
The user wants to automatically change the fill color of a PDF form field when a negative number is entered or calculated.

- Product
- Adobe Acrobat / PDF Forms
- Device & OS
- not provided
- Scenario
- Creating or editing an interactive PDF form that requires visual cues, such as yellow highlights, for negative financial or calculated values.
- Observed behavior
- The user needs the form field background to highlight in yellow when the value drops below zero, and return to transparent when the value is zero or positive.
Ensure you have access to the PDF form in editing mode and that your PDF editor supports custom JavaScript calculations for interactive form fields.
Add a Custom JavaScript Calculation to the PDF Form Field
Insert a short JavaScript conditional statement to dynamically check the field's value and change its fill color automatically.
Advanced PDF forms support JavaScript to automate visual changes. By adding a simple conditional script to the calculation properties of your form field, the background color will update instantly whenever the inputted or calculated result changes.
Open your PDF form in Adobe Acrobat and enter 'Prepare Form' mode from the tools menu.
Right-click the specific text field you want to modify and select 'Properties' from the context menu.
In the Properties dialog box, click on the 'Calculate' tab and select the 'Custom calculation script' option.
Click the 'Edit' button and paste the following code at the end of your existing script: event.target.fillColor = (event.value < 0) ? color.yellow : color.transparent;
Click 'OK', then close the properties window. Exit the form editing mode and test the field by entering a negative number to verify the yellow highlight appears.

Read, Fill, and Manage PDF Forms Effortlessly with WPS Office
While advanced JavaScript creation requires specialized form-building tools, WPS Office provides a seamless and lightweight experience for opening, filling, editing, and managing interactive PDF forms.
- 1. Download WPS Office: Download and install the free WPS Office suite on your computer or mobile device.
- 2. Open Your PDF Form: Launch WPS PDF and open your interactive or calculated PDF form.
- 3. Fill in the Data: Click on the interactive form fields to enter your numerical data or text directly.
- 4. Sign the Document: Navigate to the 'Fill & Sign' tab if you need to add a secure digital signature after reviewing the calculated numbers.
- 5. Save and Share: Save your completed PDF form locally or export it to share with colleagues or clients.

Frequently Asked Questions
Can I use custom hex color codes instead of standard colors in Acrobat JavaScript?
Yes, you can use custom RGB or CMYK arrays instead of standard named colors like color.yellow. For example, using event.target.fillColor = ['RGB', 1, 0, 0]; would make the background pure red.
Why isn't my JavaScript calculation working in the PDF form?
Ensure that JavaScript execution is enabled in your PDF reader's security preferences. Also, check that the field's formatting isn't conflicting with the calculation script, and verify there are no syntax errors or typos in your code snippet.
Can I change the text color instead of the background fill color for negative numbers?
Absolutely. To change the text color (known as the stroke color) for negative numbers, you can modify the script to target the text property: event.target.textColor = (event.value < 0) ? color.red : color.black;
Do these JavaScript highlights work on mobile PDF readers?
Support for custom JavaScript calculations varies widely on mobile devices. While standard Adobe Acrobat Reader for mobile supports some basic scripts, many third-party or built-in PDF apps on iOS and Android may not execute the color change script properly.




