Javascript - changing fill color of all text boxes in document

Explorer ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

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.

 

TOPICS
General troubleshooting , How to , JavaScript

Views

116

Likes

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 2 Correct answers

Community Expert , Aug 22, 2022 Aug 22, 2022

These are comments, not form fields.

Likes

Translate

Translate
Explorer , Aug 22, 2022 Aug 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;
    }
}

Likes

Translate

Translate
Community Expert ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

Try to replace this line

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

with this::

if(field.type == "text")

Likes

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
Explorer ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

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?

Likes

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 ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

How do you use script?

Likes

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 ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

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?

Likes

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
Explorer ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

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?

Likes

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 ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

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

Likes

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
Explorer ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

m2ramos_0-1661178294807.png

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:

m2ramos_1-1661178815880.png

 

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

m2ramos_2-1661178942482.png

 

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

 

Likes

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 ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

These are comments, not form fields.

Likes

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
Explorer ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

Thank you.  I'll try to create one using annotation code then.  I'm sorry for the confusion.  Previously when talking to tech support for an Evermaps plugin, they mentioned that their redaction tool couldn't autoredact "Text Boxes" (same ones I'm using) because Text Boxes were not actual annotations.  This is what made me go down the fields coding.  Thank you for trying to help.  You guided me in the right direction.  Much appreciated.

Likes

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 ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

They are absolutely comments. You added them using the Commenting tool, no?

However, the confusion can be excused because "text box" can mean one of at least three very different things in the PDF world: A text comment, a form field, or static text in the file. On the other hand, a true Redaction tool should be able to remove all of these elements.

Likes

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
Explorer ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

I agree it's confusing when I was dealing with Evermap and their AutoRedact plugin.  It states that people who don't have Acrobat Pro can use other apps to markup PDF's using annotation tools, the plugin will then automatically redact when it sees annotation tools but it never worked for the Text Box in the Comment Tools.  That's when support told me Text Box wasn't really an annotation.  See screenshot for what it can autodetact.  Seems like it considers Highlights, Cross-outs, Underlines and Rectangles only.  Once again, I apologize for the confusion and thank you for your time.

m2ramos_0-1661180636406.png

 

Likes

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 ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

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.

Likes

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
Explorer ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

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

Likes

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 ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

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.

Likes

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
Explorer ,
Aug 22, 2022 Aug 22, 2022

Copy link to clipboard

Copied

LATEST

I understand.  Thanks for the reminder.  In this case, luckily, the color just needed to be changed.

Likes

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