Skip to main content
Inspiring
February 10, 2025
Answered

Setting up a form field array to check if a form field is visible.

  • February 10, 2025
  • 5 replies
  • 1682 views

Hi there,

 

I need to set up a form field name array to check if the field names "8 base 2" to "8 base 8" are visible. If they are visible then I will need to show then next one to show along with the Name, Sig and Date boxes. Also when it shows "8 base 5" Page 2 needs to be spawned. I have asked this previously last week but when it came to spawning a new page it stopped working. 

 
var visibleFields = ["8 base 2", "8 base 3", "8 base 4"];
for (var i in visibleFields) {
    var f = this.getField(visibleFields);
    if (f) f.display = display.visible {
        this.spawnPageFromTemplate({
cTemplate: "Page 5",nPage: v+2,bOverlay: false,bRename: false}); this.getField("Date5").display = display.visible; this.getField("Sig5").display = display.visible; this.getField("Name5").display = display.visible;
    }
}
Something along these lines. 
 
As always any help would be greatly appreciated,
 
Steve
Correct answer try67

I have now changed the code to the below but the spawn page is repeating 3 times?

 

Anything I am doing wrong with it. 

 

var visibleFields = ["8 base 2", "8 base 3", "8 base 4"];
for (var i in visibleFields) {
var f = this.getField(visibleFields[i]);
if (f.display==display.visible) {
this.getTemplate("Page 2").spawn(1, false, false); this.getField("Date5").display = display.visible; this.getField("Sig5").display = display.visible; this.getField("Name5").display = display.visible; this.getField("8 base 5").display = display.visible;
}
}


Probably because all three fields are visible... Either check only one of them, or break the loop after spawning a new page, so the rest don't get checked.

5 replies

try67
Community Expert
Community Expert
February 11, 2025

Just noticed another error in your code, namely here:

nPage: v+2,

There's no variable called "v" defined anywhere in it.

Thom Parker
Community Expert
Community Expert
February 10, 2025

Another way to implement this functionality is to use group naming for the fields. This would make the process more easily expandable, robust, and simplify it.

For example, use these field names: "8base.2", "8base.3", "8base.4"

 

Then the code can be written like this:

 

this.getField("8base").getArray().forEach(function(oFld){
    if (oFld.display == display.visible) {
        template.spawn(...);
      ... other code ...
    }
});

 

And there is another issue that the other posters didn't mention.   Don't use the spawnPageFromTemplate function.  Use the "template.spawn()" function. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
February 10, 2025

I did mention it, actually... 😊

Thom Parker
Community Expert
Community Expert
February 10, 2025

I see that now. It's aways in the details 🙂

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
February 10, 2025

This line is invalid:

var f = this.getField(visibleFields);

It needs to be:

var f = this.getField(visibleFields[i]);

Also, do not use the spawnPageFromTemplate method. It's been replaced by the spawn method of the Template object itself.

Inspiring
February 10, 2025

Thanks for your help but getting error| TypeError: this.getField(...) is null 7:AcroForm:Button 5:Annot1:MouseUp:Action1.

Can't see the naming convention wrong in any way. 

try67
Community Expert
Community Expert
February 10, 2025

Print out the field name in each iteration so you can see which one is causing it.

You have to make sure the field names in your array match the actual ones PERFECTLY. A single character off (including spaces, upper/lower-case letters, etc.) will cause it to fail.

Bernd Alheit
Community Expert
Community Expert
February 10, 2025

The if is not correct.

JR Boulay
Community Expert
Community Expert
February 10, 2025

Try to correct this first (the equal sign must be doubled):

 

if (f) f.display == display.visible {

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
February 10, 2025

Correct. It needs to be:

if (f.display==display.visible) {