PDF Editing Issues

How to Set Conditional Text and Color in PDF Forms Using JavaScript

John WilsonJohn Wilson Nov 5, 2026 871 views

Question details

The user needs to configure an interactive PDF form where a result text field dynamically updates its text value and font color based on a calculated total from radio buttons, as well as handling overriding conditions.

How to Set Conditional Text and Color in PDF Forms Using JavaScript
Product
PDF Editor
Device & OS
not provided
Scenario
Creating a risk and authorization matrix in an interactive PDF form that requires automated status output.
Observed behavior
The text field needs to display specific statuses and apply precise text colors (color.green, color.yellow, color.red) based on numeric thresholds and specific radio-button count conditions.
Before you start

Ensure your interactive PDF already has the necessary radio button groups and text fields properly named (e.g., 'TextTotal' and 'TextResult') before adding custom calculation scripts.

Solution 1Recommended

Use Custom Calculation Scripts to Set Dynamic Text and Colors

Apply a custom JavaScript calculation to the result text field to evaluate the total score and automatically change the display text and font color.

Interactive PDFs allow you to use JavaScript to fetch data from other fields, evaluate them against your thresholds, and modify field properties like value and text color.

1
Access Field Properties

Open your PDF form editor, right-click the designated result text field (e.g., 'TextResult'), and select 'Properties' from the context menu.

2
Select Custom Calculation

Navigate to the 'Calculate' tab inside the Properties window, select the 'Custom calculation script' option, and click the 'Edit' button.

3
Write the Fetch Logic

Enter JavaScript to retrieve the calculated total. For example: `var total = this.getField('TextTotal').value;`

4
Apply Conditional Formatting

Use `if/else if` statements to check the total. Within each condition, assign the text output using `event.value = 'Status Name';` and set the color using `event.target.textColor = color.green;` (use color.yellow or color.red for other conditions).

Avoid Overlapping Conditions: Ensure your numeric thresholds are mutually exclusive (e.g., < 5, 5-10, > 10) so the script does not conflict and accurately assigns the correct status.

Manage Advanced PDF Forms Smoothly with WPS Office

WPS Office provides a highly compatible PDF editor that allows you to read, fill out, and interact with complex PDF forms. Whether your document contains calculated fields or dynamic scripts, WPS PDF ensures seamless form handling and data entry.

  1. 1. Launch the PDF Editor: Open WPS Office on your computer and click on the 'PDF' tab from the home screen.
  2. 2. Open Your Interactive PDF: Click 'Open' to load your interactive PDF form that contains the configured scripts and fields.
  3. 3. Fill the Form Fields: Click on the available radio buttons or text fields to input your data. The calculated fields and colors will process automatically as you type or click.
  4. 4. Save the Document: Once the form is filled out and calculations are validated, click 'Menu' and select 'Save As' to store your completed PDF.
Seamlessly fill out and interact with advanced PDF forms.Highly compatible with Adobe Acrobat PDF formats and embedded field calculations.View, edit, and annotate PDFs in a lightweight, user-friendly interface.
microsoft office alternative - wps office

Frequently Asked Questions

Can I use exact Hex color codes in PDF JavaScript instead of default colors?

Yes, you can use custom colors by defining them as an array. The PDF JavaScript API uses RGB, CMYK, or Gray color spaces. For a specific RGB color, you can use `event.target.textColor = ['RGB', 0.8, 0.1, 0.1];` where the values are between 0 and 1.

Why isn't my PDF form calculation updating automatically?

The calculation order might be incorrect. If 'TextResult' relies on 'TextTotal', 'TextTotal' must be calculated first. Go to your form editing tools, find the 'Calculate Order' settings, and ensure your fields are sorted in the correct sequence.

How do I correctly reference radio buttons in PDF JavaScript?

You can retrieve the selected value of a radio button group by using `this.getField('RadioButtonGroupName').value`. Make sure all buttons in the same group share the exact same field name but possess unique 'Export Values' in their properties.

Will these custom JavaScript calculations work on mobile PDF viewers?

Compatibility varies. While desktop applications like Adobe Acrobat and WPS Office can handle embedded PDF JavaScript smoothly, many mobile PDF readers have limited or no support for custom calculation scripts.