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

change the format of several fields in one step

New Here ,
Oct 03, 2021 Oct 03, 2021

I have a problem: i generate many pdf-formulars for many companies. I often have to change the format for over 50 fields at the same time. 50 fields will change from standard to integer format. Sometimes i have to change 50 fields from integer format with two decimal places to one decimal place. 

 

How can i do that for several fields at the same time? Currently i change one filed by another. And that is no clean solution!

 

Best regards and thanks for your help

TOPICS
PDF forms
3.0K
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 ,
Oct 03, 2021 Oct 03, 2021

Paste this script in the JS Console, select all, and hit the Enter key:

 

for (var i = 0 ; i < this.numFields ; i++) {
var oFld = this.getField(this.getNthFieldName(i));
if ((oFld.type == "text") && (/total/.test(oFld.name))) {
oFld.setAction("Format","event.value = util.printf(\"%,2.0f\",event.value) ;");
}
}

 

 

It will change the Format of all text fields whose name contains total like this:

 

Capture_113.png

 

You can assign the format you want by setting printf:

https://opensource.adobe.com/dc-acrobat-sdk-docs/acrobatsdk/html2015/Acro12_MasterBook/JS_API_AcroJS...


Acrobate du PDF, InDesigner et Photoshopographe
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 Beginner ,
Mar 18, 2024 Mar 18, 2024

I am trying to use the code above to change multiple text boxes at the same time from integer to 1 decimal point but it doesn't seem to work.  The code I am using is below and all I have changed is for the file name to contain 'TestCPL' and the format from '%,2.0f' to %,2.1f (1 decimal point).

Daphne5C23_0-1710781034016.png

I have then selected all the fields on the page and hit enter but nothing seems to change in the field names that contain TestCPL.  Can you please tell me what I am doing wrong?

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

You don't need to select the fields. You need to select the code in the JS Console and press Ctrl+Enter to execute it.

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

Thank you for responding. 

 

I now seem to get the message 'undefined' under the code after I execute it

Daphne5C23_0-1710867613309.png

You can see the field names in the screenshot below and there are definitely a number of them in the first two rows that contain 'TestCPL' as part of the name.

Daphne5C23_1-1710867964437.png

 

  

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

"undefined" is not an error, the opposite, actually.

Check if the fields were actually edited.

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

They didn't change.  My fields are currently set to zero decimal points and I am trying to set them to 1 decimal point.  After executing that code they still remain at zero decimal points.

Daphne5C23_0-1710925485926.png

 

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

Daphne5C23 since it's a grid of fields you don't need to use a script.

Remove all fields except the first one, set the Format, then right-clic on the field and use "Create multiple copies".

This should take about 30 seconds.


Acrobate du PDF, InDesigner et Photoshopographe
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 Beginner ,
Mar 20, 2024 Mar 20, 2024

Thanks for your suggestion.  I do use the 'Create Multiple Copies' function when starting a form from scratch but sadly that is not an option for me on this occassion. 

 

I actually have several existing forms to amend, each one with many fields in a grid pattern.  Almost all of those fields contain individual validation scripts checking target values against actual values and formatting the text colour accordingly so if I used the create multiple copies method I would then have to amend all those validation scripts individually instead so it would actually take several hours.

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

That's because the code you got will not set the built-in Number option of the Format tab.

Try this:

 

oFld.setAction("Format", "AFNumber_Format(1, 0, 0, 0, \"\", false)");
oFld.setAction("Keystroke", "AFNumber_Keystroke(1, 0, 0, 0, \"\", false)");

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

That works perfectly!  Thank you so much for all your help. 

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 ,
Dec 30, 2025 Dec 30, 2025

I have used the script below to edit multiple formats at once, but specified them with the name of the fields it needs to change, but sadly, it doesn't work. Am I doing something wrong? I have the newest version, but I did make the form in Indesign (also newest version) before exporting it to an interactive pdf. In adobe Acrobat i added the script, because otherwise I needed to edit the format of all the fields by hand.

 

var names = this.getFieldNames();
var kostenCount = 0;
var datumCount = 0;

for(var i=0; i<names.length; i++){
var f = this.getField(names[i]);
if(!f || f.type !== "text") continue;

// ===== KOSTENVELDEN =====
if(f.name.indexOf("Kosten_") === 0 ||
f.name === "Totaal-kosten-alles" ||
f.name.indexOf("Prijs-stuk_") === 0){
if(f.value === "") f.value = 0; // dummy waarde
f.setAction("Format", "AFNumber_Format(2,1,0,0,'€');");
f.setAction("Keystroke", "AFNumber_Keystroke(2,1,0,0,'€');");
f.value = f.value; // trigger format
kostenCount++;
}

// ===== DATUMVELDEN =====
if(f.name.indexOf("Deadline_") === 0 ||
f.name.indexOf("Start-datum_") === 0 ||
f.name.indexOf("Crew_Geboortedatum_") === 0){
if(f.value === "") f.value = "01/01/2000"; // dummy datum
f.setAction("Format", "AFDate_FormatEx('dd/mm/yyyy');");
f.setAction("Keystroke", "AFDate_KeystrokeEx('dd/mm/yyyy');");
f.value = f.value; // trigger format
datumCount++;
}
}

// Bevestigingspopup
app.alert("Formatting applied!\nKostenvelden: " + kostenCount + "\nDatumvelden: " + datumCount);

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 ,
Dec 30, 2025 Dec 30, 2025

Check the JavaScript console for errors . 

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 ,
Dec 30, 2025 Dec 30, 2025

I do not get an error, just the undefined, but it still doesn't work as it should i guess

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 ,
Dec 30, 2025 Dec 30, 2025

What are the names of the fields you're trying to edit?

Can you share the file?

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 ,
Dec 30, 2025 Dec 30, 2025

I have attached the file, please note that it is in Dutch. I want to do the following:

 

  • The fields with names that match-up with one of the names below to be formatted like this example: €1.234,56
    • Fields starting with: Kosten_
    • Fields starting with: Prijs-stuk_
    • Field with name: Totaal-kosten-alles
  • I want the fields that match up to at least one of the names below to format like a date like this: dd/mm/yyyy
    • Fields that start with: Deadline_
    • Fields that start with: Start-datum_
    • Fields that start with: Crew_Geboortedatum_
  • Furthermore (if possible) I want the fields that start with the text below to be formatted as a phonenumber
    • Crew_Telefoonnummer_

 

Is this possible?

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 ,
Dec 30, 2025 Dec 30, 2025

Ja, dat is geen probleem! 🙂

 

There's no such method as getFieldNames, though. You probably got it off some kind of AI model that invented it. To access all the fields in a file use the following:

 

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

// do something with this field
}

 

I did that, and it worked fine. It edited 192 "Kosten" fields and 140 "Datum" fields. I would recommend adding a command to switch off calculations before running it, though.

Add this before your code:

this.calculate = false;

And this at the end of it:

this.calculate = true;

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 ,
Dec 30, 2025 Dec 30, 2025

You should have, because (as I wrote below above) getFieldNames doesn't exist, so the first line of code would generate this error:

 

TypeError: this.getFieldNames is not a function
1:Console:Exec

 

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 ,
Dec 30, 2025 Dec 30, 2025

I added the fnction as you said it and for me it didn't change anything it seems. Although it did give the note "True" after i did ctrl + enter. Am I doing something wrong? The full script should be as below, right? I only needed to change the top part of my original script.

 

this.calculate = false;

 

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

// do something with this field
}

// ===== KOSTENVELDEN =====
if(f.name.indexOf("Kosten_") === 0 ||
f.name === "Totaal-kosten-alles" ||
f.name.indexOf("Prijs-stuk_") === 0){
if(f.value === "") f.value = 0; // dummy waarde
f.setAction("Format", "AFNumber_Format(2,1,0,0,'€');");
f.setAction("Keystroke", "AFNumber_Keystroke(2,1,0,0,'€');");
f.value = f.value; // trigger format
kostenCount++;
}

// ===== DATUMVELDEN =====
if(f.name.indexOf("Deadline_") === 0 ||
f.name.indexOf("Start-datum_") === 0 ||
f.name.indexOf("Crew_Geboortedatum_") === 0){
if(f.value === "") f.value = "01/01/2000"; // dummy datum
f.setAction("Format", "AFDate_FormatEx('dd/mm/yyyy');");
f.setAction("Keystroke", "AFDate_KeystrokeEx('dd/mm/yyyy');");
f.value = f.value; // trigger format
datumCount++;
}
}

// Bevestigingspopup
app.alert("Formatting applied!\nKostenvelden: " + kostenCount + "\nDatumvelden: " + datumCount);

 

this.calculate = true;

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 ,
Dec 30, 2025 Dec 30, 2025

Select the whole script before you execute it. 

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 ,
Dec 30, 2025 Dec 30, 2025

I used the code below, selected it al before I "Ctrl+enter"ed, but now I got a "SyntaxError: syntax error

31:Console:Exec
undefined"

 

What am I not seeing and/or doing wrong?

 

this.calculate = false;

for (var i=0; i<this.numFields;i++){
var fname = this.getNthFieldName(i);
var f = this.getField(fname);
if (f=nul) continue;
// Do something with this field
}

// Kostenvelden
if(fname.indexOf("Kosten_") === 0||
fname === "Totaal-kosten-alles" ||
fname.indexOf("Prijs-stuk_") === 0){
if(f.value === "")f.value=0;
f.setAction("Format","AFNumber_Format(2,1,0,0,'€');");
f.setAction("Keystroke", "AFNumber_Keystroke(2,1,0,0,'€');");
f.value = f.value
kostenCount++;
}

// Datumvelden
if(fname.indexOf("Deadline_") === 0||
fname.indexOf("Start-datum_") === 0||
fname.indexOf("Crew_Geboortedatum_") === 0){
if(f.value === "") f.value = "01/01/2000";
f.setAction("Format","AFDate_FormatEx('dd/mm/yyyy');");
f.setAction("Keystroke","AFDate_KeystrokeEx('dd/mm/yyyy');");
f.value = f.value
datumCount++;
}
}

//Bevestigings pop-up
app.alert("Formatting applied!\Kostenvelden: " + kostenCount + "\nDatumvelden: " + datumCount);

this.calculate = true

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 ,
Dec 30, 2025 Dec 30, 2025

You have one curly bracket too many, after this line:

// Do something with this field

Use the code I provided below.

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 ,
Dec 30, 2025 Dec 30, 2025

No, it needs to be this:

 

this.calculate = false;
var kostenCount = 0;
var datumCount = 0;

for (var i=0; i<this.numFields; i++) {
	var fname = this.getNthFieldName(i);
	var f = this.getField(fname);
	if (f==null) continue;
	if(!f || f.type !== "text") continue;

	// ===== KOSTENVELDEN =====
	if(f.name.indexOf("Kosten_") === 0 ||
	f.name === "Totaal-kosten-alles" ||
	f.name.indexOf("Prijs-stuk_") === 0){
	if(f.value === "") f.value = 0; // dummy waarde
	f.setAction("Format", "AFNumber_Format(2,1,0,0,'€');");
	f.setAction("Keystroke", "AFNumber_Keystroke(2,1,0,0,'€');");
	f.value = f.value; // trigger format
	kostenCount++;
	}

	// ===== DATUMVELDEN =====
	if(f.name.indexOf("Deadline_") === 0 ||
	f.name.indexOf("Start-datum_") === 0 ||
	f.name.indexOf("Crew_Geboortedatum_") === 0){
	if(f.value === "") f.value = "01/01/2000"; // dummy datum
	f.setAction("Format", "AFDate_FormatEx('dd/mm/yyyy');");
	f.setAction("Keystroke", "AFDate_KeystrokeEx('dd/mm/yyyy');");
	f.value = f.value; // trigger format
	datumCount++;
	}
}

// Bevestigingspopup
app.alert("Formatting applied!\nKostenvelden: " + kostenCount + "\nDatumvelden: " + datumCount);
this.calculate = true;
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 ,
Dec 30, 2025 Dec 30, 2025

Thank you! It works now! Last two questions

  1.  Is it possible to leave the date field empty instead of adding the "dummy"value?
  2. Is it possible to add the "€" i front of the number?

 

I am new at making these scripts, really want to become better at it and I really appriciate the help! Thanks a lot

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 ,
Dec 30, 2025 Dec 30, 2025

1. Yes, there's no need to do that.

2. Yes. After '€' in both function calls, add a "true" parameter (without the quotes).

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