Skip to main content
Inspiring
August 22, 2022
Answered

Javascript - changing fill color of all text boxes in document

  • August 22, 2022
  • 2 replies
  • 5150 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
Community 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?

try67
Community Expert
August 22, 2022

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;
    }
}

Just be aware this does NOT redact anything in the file. Even if you flatten it, the text that's under these boxes will still be there. It will only be gone if you re-create the file from scratch, for example by exporting it as an image and then re-creating it from those images, and even then it might still be possible to retrieve it, unless the fill color is completely opaque. The only secure way to remove contents from a PDF file is by redacting it.

Nesa Nurani
Community Expert
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
Community Expert
August 22, 2022

How do you use script?