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

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

Participant ,
Feb 10, 2025 Feb 10, 2025

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
TOPICS
JavaScript , Modern Acrobat , PDF forms
684
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Feb 10, 2025 Feb 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.

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

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.

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 ,
Feb 10, 2025 Feb 10, 2025

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

 

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


Acrobate du PDF, InDesigner et Photoshoptographe
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 ,
Feb 10, 2025 Feb 10, 2025

Correct. It needs to be:

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

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 ,
Feb 10, 2025 Feb 10, 2025

The if is not correct.

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 ,
Feb 10, 2025 Feb 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.

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
Participant ,
Feb 10, 2025 Feb 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. 

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 ,
Feb 10, 2025 Feb 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.

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
Participant ,
Feb 11, 2025 Feb 11, 2025

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

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

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.

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
Participant ,
Feb 11, 2025 Feb 11, 2025

Thanks Try67,

 

You're a genius.

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 ,
Feb 10, 2025 Feb 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 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 ,
Feb 10, 2025 Feb 10, 2025

I did mention it, actually... 😊

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 ,
Feb 10, 2025 Feb 10, 2025

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

 

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 ,
Feb 11, 2025 Feb 11, 2025

Just noticed another error in your code, namely here:

nPage: v+2,

There's no variable called "v" defined anywhere in 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