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

Script that applies f.setAction to text fields with specific names using some type of wildcard

New Here ,
Mar 19, 2024 Mar 19, 2024

Want to be able to look for different text fields in the document and make changes to the text fields that have a certain text in their name. 

Document has 50 text fields, but I am looking of the ones that are called HoursRow, followed by a number, so HoursRow1 HoursRow2  (total of 15 HourRow)etc 

Is there a way to put in some type of wildcard in results of getting all the text fields. Because I want to be able to change the color and format with the script for ONLY HourRow text fields

This did NOT work

for (var i = 0; i < this.numFields; i++) {
    var fname = this.getNthFieldName(i);
    var f = this.getField(fname);

    // Check if the field value contains "HoursRow"
    if (f.type === "text" && /HoursRow/.test(fname.value)) {
        // Apply your actions only to fields containing "HoursRow" in their value
        f.setAction("Format", "AFNumber_Format(2, 0, 0, 0, \"\", true);");
        f.setAction("Keystroke", "AFNumber_Keystroke(2, 0, 0, 0, \"\", true);");
        f.fillColor = color.red;
    }
}

 

If I put in code below it changes ALL text fields which I do not want to do 

for ( var i=0; i < this.numFields; i++) {
    var fname = this.getNthFieldName(i);
    var f = this.getField(fname);
    if ( f.type == "text" ) f.setAction("Format", "AFNumber_Format(2, 0, 0, 0, \"\", true);");
    f.setAction("Keystroke", "AFNumber_Keystroke(2, 0, 0, 0, \"\", true);");
f.fillColor = color.red; }

 On the same line, is it possible to change range of  text field name, same example

HoursRow1 HoursRow2 etc to only Hours1 Hours2 etc 

Thanks

TOPICS
JavaScript , PDF forms
777
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Mar 19, 2024 Mar 19, 2024

Change this part: fname.value to f.name

You can't change field names with a script, you can only use a script to remove old fields and create new fields in their place with new names.

View solution in original post

Translate
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 ,
Mar 19, 2024 Mar 19, 2024

Change this part: fname.value to f.name

You can't change field names with a script, you can only use a script to remove old fields and create new fields in their place with new names.

Translate
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 20, 2024 Mar 20, 2024

Ugh! But horray, it wouks! 

Thank you !!!!!

 

Translate
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 ,
Aug 10, 2025 Aug 10, 2025

Hello,
Replying here because I used the elegant script above to successfully rotate my form text fields 90°, but I must be missing something about how to iterate this over all of the pages in the document. It is only acting on the first page of the document. Do I need to push the widgets for each field to an array? Is there a way to use javascript to "duplicate the fields across pages" as in the right-click field menu? Thank you for any advice.

var pRot = this.getPageRotation();

for (var i=0; i<this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));
if (f==null) continue;
if (f.type=="text") {
f.rotation = pRot + 90;
    }
}

I also tried this -
for (var p=0; p<this.numPages; p++) {
var pRot = this.getPageRotation(p);
for (var i=0; i<this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));
if (f==null) continue;
if (f.type=="text") {
f.rotation = pRot + 90;
    }
}
}



Translate
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 10, 2025 Aug 10, 2025

If you wish to rotate 'text' fields on each page use this:

for (var i=0; i<this.numFields; i++) {
 var fname = this.getNthFieldName(i);
 var f = this.getField(fname);

 if (f.type === "text") {
  f.rotation = 90;}}
Translate
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 ,
Aug 10, 2025 Aug 10, 2025

Nesa,
Thank you very much for your reply and your previous examples.

I am still only getting the fields to rotate on the first page of the document only. 

I have created a script to add a set of form fields to a pdf  (will be of variable length) in specific locations, on all of the pages.  I need the orientation of these fields to be + 90° from the page rotation.

It's my understanding that the field orientation (.rotation) is controlled at the widget level, so I haven't been able to set it at field creation.  I can't seem to get the fields to rotate with any scripts, even though I can use a similar script to count the number of widgets.  I'm missing something or going about this in the wrong order, but I haven't been able to figure this out myself yet. 

Here is a generic example of the process I am using -

for (var p=0; p<this.numPages; p++) {
var f = this.addField({cName:"customer", cFieldType:"text", nPageNum:p, oCoords:[200,100,225,200]});
}
for (var p=0; p<this.numPages; p++) {
var f = this.addField({cName:"address", cFieldType:"text", nPageNum:p, oCoords:[230,100,255,200]});
}
for (var p=0; p<this.numPages; p++) {
var f = this.addField({cName:"city st zip", cFieldType:"text", nPageNum:p, oCoords:[260,100,285,200]});
}


I am attaching a file that shows my results, where only the fields on the first page have rotated + 90.

Thank you for your assistance.


Translate
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 11, 2025 Aug 11, 2025

@melissa_9782 I understand now, you are adding multiple fields with same name, in that case you can set rotation in same script when you add fields.

This script should add fields and set rotation:

var fields = [
 {name: "customer", coords: [200, 100, 225, 200]},
 {name: "address", coords: [230, 100, 255, 200]},
 {name: "city st zip", coords: [260, 100, 285, 200]}
];

for(var i=0; i<fields.length; i++) {
 var fData = fields[i];
 for (var p = 0; p < this.numPages; p++) {
  this.addField(fData.name, "text", p, fData.coords);
  this.getField(fData.name + "." + p).rotation = 90;}}

 

Translate
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 ,
Aug 11, 2025 Aug 11, 2025
LATEST

Thank you, Nesa! 
Would you mind also posting this solution here -  Re: Changing the .rotation of all widgets of a for... - Adobe Product Community - 15452300
so I can mark yours as the correct answer?

Translate
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