Color Calculated PDF Form Fields by Rank in Adobe Acrobat
Question details
Rank calculated PDF form values and apply progressive fill colors, while handling duplicate and empty values.

- Product
- Adobe Acrobat
- Device & OS
- not provided
- Scenario
- A PDF form contains custom calculated fields that should be colored from lowest to highest, similar to an Excel three-color scale.
- Observed behavior
- Equal values should receive the same color, and empty fields should be excluded from the ranking.
Open the form in an Acrobat edition that supports editing form fields and JavaScript. Note the exact names of the calculated fields and decide which colors to use for the lowest, middle, and highest ranks.
Rank calculated fields with a document-level Acrobat JavaScript function
Use a helper calculation to refresh field colors after the other calculated values are updated.
This example assigns colors by distinct rank, not by the numerical distance between values. Equal values receive the same color; if all values are equal, they receive the middle color. Empty and nonnumeric fields are cleared and excluded.
The script accepts plain numbers, including decimals and negative values. Values containing currency symbols, commas, or other formatting characters must be converted to plain numbers before ranking.
In Acrobat, open All tools > Use JavaScript > Document JavaScripts, create a script, and paste the following. Replace the names in the names array with your calculated field names: function colorRankedFields() { var names = ["Value1", "Value2", "Value3"]; var entries = [], unique = [], i, f, raw, text, value; var low = [1, 0, 0], mid = [1, 1, 0], high = [0, 0.6, 0]; for (i = 0; i < names.length; i++) { f = this.getField(names[i]); if (!f) continue; raw = f.value; text = String(raw).replace(/^\s+|\s+$/g, ""); if (text === "" || !/^[+-]?(?:\d+\.?\d*|\.\d+)$/.test(text)) { f.fillColor = ["T"]; continue; } value = Number(text); entries.push({ field: f, value: value }); } entries.sort(function(a, b) { return a.value - b.value; }); for (i = 0; i < entries.length; i++) { if (i === 0 || entries[i].value !== entries[i - 1].value) unique.push(entries[i].value); } for (i = 0; i < entries.length; i++) { var rank = 0; while (unique[rank] !== entries[i].value) rank++; var t = unique.length === 1 ? 0.5 : rank / (unique.length - 1); var start = t <= 0.5 ? low : mid; var end = t <= 0.5 ? mid : high; var blend = t <= 0.5 ? t * 2 : (t - 0.5) * 2; entries[i].field.fillColor = ["RGB", start[0] + (end[0] - start[0]) * blend, start[1] + (end[1] - start[1]) * blend, start[2] + (end[2] - start[2]) * blend]; } }
Open All tools > Prepare a form and add a text field named ColorRefresh. In its Properties > Calculate tab, select Custom calculation script and enter: event.value = ""; colorRankedFields(); Set the helper field to read-only and hide it if it should not be visible.
In Prepare a form, open More > Set Field Calculation Order. Move ColorRefresh after every calculated field listed in the script, so it reads their updated values.
Enter values in the form and check the calculated fields. Test duplicate values, an empty field, and a single distinct value; then save the PDF.

Work with PDF and Office files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with documents, spreadsheets, presentations, and PDFs. Its Microsoft Office format compatibility helps you move existing files into your workflow. Use Adobe Acrobat for the document-level PDF JavaScript solution described above.

Frequently Asked Questions
How does the script handle equal values?
It builds a list of distinct sorted values, so fields with the same value share the same rank and fill color. If every valid value is equal, all receive the middle color.
What happens to empty or nonnumeric fields?
The function excludes them from the ranking and sets their fill to transparent. Enter plain numeric values or adjust the parsing logic if the calculated values contain currency symbols or commas.
Why are the colors not updating after a calculation?
In Prepare a form, check More > Set Field Calculation Order and place ColorRefresh after all ranked calculated fields. Also check that the field names in the script exactly match the names in the form.
How do I choose different colors?
Edit the low, mid, and high RGB arrays in the document-level function. Each color component must be between 0 and 1; divide a 0–255 RGB component by 255 to convert it.




