• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Automatic field color change for different value range?

New Here ,
Feb 23, 2016 Feb 23, 2016

Copy link to clipboard

Copied

Hi, I am a new user to Acrobat Pro and I am trying to figure out if it is possible to have the field color change when calculated value is within a specific range.  I have a calculated field box and I want it to turn yellow when it reaches a specific numerical range, and red when it gets to another.  A font color change would work too..

TOPICS
Acrobat SDK and JavaScript

Views

1.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 23, 2016 Feb 23, 2016

The correct code could be:

(function() {

    var v = +event.value;

    if (v < 16) {

        event.target.fillColor = color.green;

        return;

    }

    if (v < 27) {

        event.target.fillColor = color.yellow;

        return;

    }

    // Value is greater than 27

    event.target.fillColor = color.red;

})();

This line of code in particular isn't valid:

if (16<v>27){

Something like this would be:

if (v >= 16 && v < 27) {

which translated to English is: If the value of the variable v is greater than or equa

...

Votes

Translate

Translate
LEGEND ,
Feb 23, 2016 Feb 23, 2016

Copy link to clipboard

Copied

Sure it's possible. Are you currently using a custom calculation script or one of the built-in calculation options?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 23, 2016 Feb 23, 2016

Copy link to clipboard

Copied

‌Just the built in sum of selected fields.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2016 Feb 23, 2016

Copy link to clipboard

Copied

You can use a custom Validation script for the field, something like the following:

// Custom Validate script for text field

(function () {

    // Set background color to white if blank

    if (!event.value) {

        event.target.fillColor = color.white;

        return;

    }

    // Convert value to a number

    var v = +event.value;

    if (v < 5) {

        event.target.fillColor = color.red;

        return;

  }

  if (v < 10) {

        event.target.fillColor = color.yellow;

        return;

  }

  // Value is greater than 10

    event.target.fillColor = color.green;

})();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 23, 2016 Feb 23, 2016

Copy link to clipboard

Copied

Thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 23, 2016 Feb 23, 2016

Copy link to clipboard

Copied

So, here is what I have...It accepts it, but nothing happens.

(function(){

var v = +event.value;

if (v<16){

event.target.fillColor=color.green;

}

if (16<v>27){

event.target.fillColor=color.yellow;

}

//Value is greater than 26

event.target.fillColor=color.red;

})();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 23, 2016 Feb 23, 2016

Copy link to clipboard

Copied

The correct code could be:

(function() {

    var v = +event.value;

    if (v < 16) {

        event.target.fillColor = color.green;

        return;

    }

    if (v < 27) {

        event.target.fillColor = color.yellow;

        return;

    }

    // Value is greater than 27

    event.target.fillColor = color.red;

})();

This line of code in particular isn't valid:

if (16<v>27){

Something like this would be:

if (v >= 16 && v < 27) {

which translated to English is: If the value of the variable v is greater than or equal to 16 and less than 27...

So another functionally equivalent script could be:

var v = +event.value;

if (v < 16) {

    event.target.fillColor = color.green;

} else if (v >= 16 && v < 27) {

    event.target.fillColor = color.yellow;

} else {  // Value is greater than or equal to 27

    event.target.fillColor = color.red;

}

I'm not sure if it's exactly what you want, but you should be able to revise it to suit.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 24, 2016 Feb 24, 2016

Copy link to clipboard

Copied

The first code you posted is not equivalent in functionality to your later code. In the former you're missing both the else-command and the more full conditions that are in later code. I would suggest to use the latter.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 24, 2016 Feb 24, 2016

Copy link to clipboard

Copied

The background color of the field is set the same for the same field value, which is what I mean by functionally equivalent. Note the return statements.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 24, 2016 Feb 24, 2016

Copy link to clipboard

Copied

Thank you George, woks perfect!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 24, 2016 Feb 24, 2016

Copy link to clipboard

Copied

My apologies. Didn't notice the return commands...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 24, 2016 Feb 24, 2016

Copy link to clipboard

Copied

Any idea why the yellow range doesn't work in Adobe Reader?  Works perfect in PDF Expert and Acrobat, and when it gets to the red values it works.  I am doing a Risk Assessment tool for my pilots and I have Reader as their PDF app.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 24, 2016 Feb 24, 2016

Copy link to clipboard

Copied

Could be because color.yellow is a CMYK color, while the others are RGB-based. Maybe the Reader app for iOS only supports RGB-colors...

Try replacing color.yellow with this: ["RGB", 1, 1, 0]

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 24, 2016 Feb 24, 2016

Copy link to clipboard

Copied

That worked perfect!  Thank you both very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 29, 2016 Feb 29, 2016

Copy link to clipboard

Copied

One last question...is there a way to put a "reset" button on the form that zeros out all the fields, so that my pilot can just hit that to reset the fields and eliminate errors from them forgetting to zero them out themselves?  Thanks again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 29, 2016 Feb 29, 2016

Copy link to clipboard

Copied

You can add a reset button that clears all of the values, sure, but you'll need to add additional code that changes the fill color of the fields. It would look something like this:

this.resetForm();

this.getField("Text1").fillColor = color.transparent;

this.getField("Text2").fillColor = color.transparent;

this.getField("Text3").fillColor = color.transparent;

etc.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 03, 2016 Mar 03, 2016

Copy link to clipboard

Copied

LATEST

Thanks again for your help....sorry I'm such a rookie at this stuff, but it is fun to learn about it.

So, do I go into each field to add the code that changes the fill color, or is there a way to put a code in for the whole project?  Where would I go to put the code that you gave me?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines