Search code examples
javascriptacrobat

Dynamic font color assignment based on a form field value in Acrobat


I have a loan form that calculates a debt:income ratio. I want the field the ration is in (calculated field, no user input allowed) to change the font color based on the ratio.

If the ratio is above 60%, then I want the font color to be an off-red (see color values below), another color if >= 35%, and normal if below 35%.

This is the code I've come up with...

if (event.value >= .6) {
    this.textColor = (255, 153, 0);
}
else if (event.value >= .35) {
    this.textColor = (204, 51, 0);
}
else {
    this.textColor = (0, 102, 153);
}

The code is in a custom validation.

This doesn't work. What am I doing wrong?


Solution

  • There were several problems with your code but also, you were running it in the wrong event. During a validation event, the value isn't actually committed yet. Use a custom Format script to change the appearance of a field after the value has been committed. See image. enter image description here

    Then in your code, you need to get the value of the field triggering the script (event.target) and then you need to set the color property of it (event.target.textColor). Also, colors in PDF are defined by using an array where the first element is the color space, then followed by values that range from 0 to 1. See revised code below.

    if (event.target.value >= .6) {
        event.target.textColor = ["RGB", 255/255, 153/255, 0];
    }
    else if (event.target.value >= .35) {
        event.target.textColor = ["RGB", 204/255, 51/255, 0];
    }
    else {
        event.target.textColor = ["RGB", 0, 102/255, 153/255];
    }