How to Set Conditional Text and Color in PDF Forms Using JavaScript
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.

- 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.
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.
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.
Open your PDF form editor, right-click the designated result text field (e.g., 'TextResult'), and select 'Properties' from the context menu.
Navigate to the 'Calculate' tab inside the Properties window, select the 'Custom calculation script' option, and click the 'Edit' button.
Enter JavaScript to retrieve the calculated total. For example: `var total = this.getField('TextTotal').value;`
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).
Implement Override Logic for Specific High-Risk Values
Modify your script to count how many times a specific high-risk value is selected across multiple radio buttons, overriding the standard total-based logic.
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. Launch the PDF Editor: Open WPS Office on your computer and click on the 'PDF' tab from the home screen.
- 2. Open Your Interactive PDF: Click 'Open' to load your interactive PDF form that contains the configured scripts and fields.
- 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. Save the Document: Once the form is filled out and calculations are validated, click 'Menu' and select 'Save As' to store your completed PDF.

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.




