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

JavaScript Formula Help - Merging Fields

New Here ,
May 12, 2025 May 12, 2025

I would like Text1 to read:

NAME

ADDRESS

CITY, STATE, ZIP

 

 

 

var fields = ["NAME", "ADDRESS", "CITY", "STATE", "ZIP"];
var values = [];
for (var i in fields) {
var f = this.getField(fields[i]);
if (f.valueAsString!="") values.push(f.valueAsString);
}
event.value = values.join("\n");

 

2025-05-12_131531.png

TOPICS
Create PDFs , How to , JavaScript , PDF , PDF forms
332
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 ,
May 13, 2025 May 13, 2025

Then the fields values need to be joined differently. 

For example:

var fields = ["NAME", "ADDRESS", "CITY", "STATE", "ZIP"];
var aFldVals = fields.map(cNm=>this.getField(cNm).value);
event.value = aFldVals[0] + "\n" + aFldVals[1] + "\n" + aFldVals[2] + ", " + aFldVals[3] + ", " + aFldVals[4]; 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
May 12, 2025 May 12, 2025

Have you a question?

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 ,
May 12, 2025 May 12, 2025

Is your code a calculation script on the "Text1" field?  If it is, then this is the correct way to do it. 

 

Did you check the console window for errors?  If you had you would probably see a type error being reported. This is because of the incorrect usage of the "for/in" loop. Use a regular for loop and it'll work. 

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
May 12, 2025 May 12, 2025

Here is another way to write the code:

var fields = ["NAME", "ADDRESS", "CITY", "STATE", "ZIP"];
event.value = fields.map(cNm=>this.getField(cNm).value).join("\n");

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
May 13, 2025 May 13, 2025

Text1 is showing:

NAME

ADDRESS

CITY

STATE

ZIP

 

But I would like the City, State, & Zip to be on the same line instead of stacked. 

 

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 ,
May 13, 2025 May 13, 2025

Then the fields values need to be joined differently. 

For example:

var fields = ["NAME", "ADDRESS", "CITY", "STATE", "ZIP"];
var aFldVals = fields.map(cNm=>this.getField(cNm).value);
event.value = aFldVals[0] + "\n" + aFldVals[1] + "\n" + aFldVals[2] + ", " + aFldVals[3] + ", " + aFldVals[4]; 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
May 13, 2025 May 13, 2025
LATEST

That worked! 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