PDF Editing Issues

How to Create Acrobat JavaScript If-Then Statements with Colored Results

Kushani NimanthikaKushani Nimanthika Nov 3, 2026 869 views

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.

How to Create Acrobat JavaScript If-Then Statements with Colored Results
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.
Before you start

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.

Solution 1Recommended

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.

1
Open Field Properties

Right-click the destination text field in your PDF form (e.g., the 'Description' field) and select 'Properties' from the context menu.

2
Navigate to the Calculate Tab

In the Properties window, click on the 'Calculate' tab and select the 'Custom calculation script' radio button.

3
Enter the JavaScript Logic

Click the 'Edit' button and input the script to retrieve the points: var totalPoints = Number(this.getField("TotalPoints").valueAsString);

4
Add Conditional Text and Colors

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; }

5
Save and Test

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.

Implement Custom Calculation Script for Categorized Scores
Color Definitions: Acrobat JavaScript supports simple color definitions like color.green and color.red, as well as exact RGB arrays like ["RGB", 1, 0.64, 0] for custom colors like orange.
Powerful PDF Editor

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. 1. Open WPS Office: Launch the WPS Office application on your computer or mobile device.
  2. 2. Access the PDF Tools: Click on the 'PDF' tab from the main dashboard to access the PDF reader and editor.
  3. 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. 4. Fill and Save: Click directly into the form fields to enter your data, then click 'Save' to preserve your interactive form responses.
Easily fill out and manage interactive PDF formsHighly compatible with standard Adobe PDF formatsLightweight application with fast rendering speedsIncludes a full suite of document, spreadsheet, and presentation tools
microsoft office alternative - wps office

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.