How to Create Acrobat JavaScript If-Then Statements with Colored Results
Question details
The user needs to write custom JavaScript calculation scripts in a PDF form to evaluate a points field, output conditional text labels (HIGH, MEDIUM, LOW), and automatically adjust the text color. They also need a script to handle conditional subtraction for balances and refunds.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- Creating interactive and dynamic PDF forms that require automated scoring, financial calculations, and visual color coding based on user input.
- Observed behavior
- The goal state is for the PDF form fields to automatically calculate values upon input, update the text to reflect specific categories (like LOW, HIGH, or Refund), and dynamically change the font color (e.g., green, orange, red) to match the condition.
Ensure you have a professional PDF editor that supports custom JavaScript for form fields, and verify that all referenced form fields (like 'TotalPoints', 'TotalCharges', and 'TotalCredits') are named exactly as they appear in your script to avoid calculation errors.
Implement Custom Calculation Script for Categorized Scores
Apply a custom JavaScript snippet to your target field's properties to evaluate a 'TotalPoints' field and dynamically assign both a text label and a corresponding color.
This script uses basic if-then-else logic to check the value of the 'TotalPoints' field. Depending on the score range, it assigns a specific string (LOW, MEDIUM, HIGH) to the field and uses Acrobat's built-in color array or RGB arrays to change the text color.
Right-click the destination text field in your PDF form (e.g., the 'Description' field) and select 'Properties' from the context menu.
In the Properties window, click on the 'Calculate' tab and select the 'Custom calculation script' radio button.
Click the 'Edit' button and input the script to retrieve the points: var totalPoints = Number(this.getField("TotalPoints").valueAsString);
Below the variable declaration, add your if-then conditions: if (totalPoints < 7) { event.value = "LOW"; event.target.textColor = color.green; } else if (totalPoints <= 9) { event.value = "MEDIUM"; event.target.textColor = ["RGB", 1, 0.64, 0]; } else { event.value = "HIGH"; event.target.textColor = color.red; }
Click 'OK' to save the script, then close the Properties window. Enter different values into the 'TotalPoints' field to verify that the text and colors update correctly.

Create Conditional Logic for Balance Due and Refunds
Use a subtraction script to calculate the difference between charges and credits, routing the result dynamically to either a Balance field or a Refund field with customized red text.
Edit and Manage PDF Forms Easily with WPS Office
WPS Office provides a comprehensive PDF toolkit that allows you to view, fill out, and manage interactive PDF forms seamlessly across all your devices.
- 1. Open WPS Office: Launch the WPS Office application on your computer or mobile device.
- 2. Access the PDF Tools: Click on the 'PDF' tab from the main dashboard to access the PDF reader and editor.
- 3. Open Your PDF Form: Select 'Open' and browse your computer to load the interactive PDF form you need to fill out or review.
- 4. Fill and Save: Click directly into the form fields to enter your data, then click 'Save' to preserve your interactive form responses.

Frequently Asked Questions
What are the standard color variables available in Acrobat JavaScript?
Acrobat JavaScript supports several standard color variables out of the box, including color.red, color.green, color.blue, color.black, color.white, color.cyan, color.magenta, and color.yellow. These can be assigned directly to the textColor property.
How do I define custom RGB colors in PDF JavaScript?
You can define custom colors using arrays. The format requires the color space string followed by values between 0 and 1. For example, to create orange, you would use the array ["RGB", 1, 0.64, 0] and assign it to event.target.textColor.
Can I hide a PDF form field conditionally using JavaScript?
Yes, you can dynamically hide or show fields within your if-then statements by using the display property. For example, use this.getField("FieldName").display = display.hidden; to hide a field, and display.visible to show it.




