Skip to main content
Inspiring
August 22, 2022
Answered

Javascript - changing fill color of all text boxes in document

  • August 22, 2022
  • 2 replies
  • 5149 views

Hello,

I created several text boxes in a document and I was then asked to change the fill colour of all the boxes to a specific colour.  I wanted to create an action since there are several documents that I need to go through.  I'm new to javascript and I surmise that text boxes are technically numFields (I could be wrong).  So I created the following codes (neither of which worked):

First One:

for ( var i = 0; i < this.numFields; i++) {
    var field = this.getField(this.getNthFieldName(i));
    if ( field.name.indexOf(this.getNthFieldName(i)) > -1 ) {
      field.fillColor = color.green;
    }
}

 

Second One:

for ( var i = 0; i < this.numFields; i++) {
    var field = this.getField(this.getNthFieldName(i));
    if (this.getField(this.getNthFieldName(i)).type == "text") {
      field.fillColor = color.green;
    }
}

 

Can anyone tell me what I'm doing wrong?

 

Thanks.

 

This topic has been closed for replies.
Correct answer m2ramos

These annotations are just used to mark an area on the page. Their tool probably treates that area as the area to be redacted. It has nothing to do with the contents of the comments associated with them, I'm guessing. That's why they are not redacting text in a text box (or the area on which it is placed), although technically it's quite easy to do.


In case anyone views this thread and experiences the same issue, I managed to get it working with the code below.  Thanks again everyone.

var annots = this.getAnnots();
for (var i = 0; i < annots.length; i++) {
    if (annots[i].type == "FreeText") {
        annots[i].fillColor = color.yellow;
        annots[i].strokeColor = color.yellow;
        annots[i].opacity = 1;
    }
}

2 replies

try67
Adobe Expert
August 22, 2022

The second code is convoluted (as Nesa pointed out) but should have worked.

However, you must disable the Fields Highlighting option to see the fill color you're setting. Did you do that?

m2ramosAuthor
Inspiring
August 22, 2022

Thank you both for the advice.  I realized afterwards that I could replace the long text since I created the variable just prior to it.  I'm not sure where the setting is to disable fields highlighting but I don't think it's that because if I manually go to one of the text boxes and right click to open its properties, I can change the fill color and it changes colors properly.  The script doesn't do anything when I attempt to use it but I get no errors, so I'm not sure if I"m using the wrong syntax for fillColor maybe.

 

Any ideas?

m2ramosAuthor
Inspiring
August 22, 2022

What does you see in the properties of the fields after using the script?


The above is what I see.

 

However, I was doing more testing and when I use the code below, I get zero for the number of fields in document.  I have two text boxes at least.  I was curious about this and then created a form and added a Text Box but from within the form UI and ran the code again, this time I got a result of 1 using the below code.  This leads me to believe that the Text Box option outside the form UI is different.  Please see screenshots below for the two different "Text Boxes" created.  The text boxes I'm trying ot change are the ones outside of the form UI.

console.println(this.numFields); 

 

The one I'm trying to change:

 

The one that shows up in debugger console with this.numfields:

 

I attached the pictures as well in case the post shows them too small in the thread.

 

Nesa Nurani
Inspiring
August 22, 2022

Try to replace this line

if (this.getField(this.getNthFieldName(i)).type == "text")

with this::

if(field.type == "text")

m2ramosAuthor
Inspiring
August 22, 2022

Thank you for this, I realized after the fact that I created the variable but then forgot to change that line.  Thank you for pointing that out.  However, I'm still having issues and replied to the person that replied to you.  Perhaps you have some ideas?

Nesa Nurani
Inspiring
August 22, 2022

How do you use script?