Highlighted

Toggle Image Field Visibility with List Box

Community Beginner ,
Sep 10, 2020

Copy link to clipboard

Copied

Hello Adobe Community. Long time reader, first time posting here.

 

I have a List Box that I am using to change information in a large number of text fields via a function containing an array in a Document Script. These fields pertain to the spec values for different products (voltage, amperage, weight, etc), so whenever we stock a new product to the list, this has been extremely easy to add the info to the array and as an option in the List Box. 

 

Here's an abbreviated version of the script as an example:

var modList = {
LG420: { module: "LG LG420N2W-V5", wattage: "420", voc: "49.7", vmp: "42.1", isc: "10.63", imp: "9.98", ptc: "386.9", length: "79.7", width: "40.3", weight: "47.81", POmod: "505", MaxinDC: "17.5", MaxinV: "83", MaxoutA: "15", Maxoutv: "85" },

LG400: { module: "LG LG400N2T-J5", wattage: "400", voc: "49.7", vmp: "41.5", isc: "10.22", imp: "9.65", ptc: "366", length: "79.7", width: "40.3", weight: "47.81", POmod: "505", MaxinDC: "17.5", MaxinV: "83", MaxoutA: "15", Maxoutv: "85" }, 
}; 

function Inheritance(modProps) { 

    this.getField("Module").value = modList[modProps].module;
    this.getField("ModW").value = modList[modProps].wattage; 

    this.getField("sld.Str1.ModW").value = modList[modProps].wattage; 
    this.getField("sld.Str1.VOC").value = modList[modProps].voc; 
    this.getField("sld.Str1.VMP").value = modList[modProps].vmp; 
    this.getField("sld.Str1.ISC").value = modList[modProps].isc; 
    this.getField("sld.Str1.IMP").value = modList[modProps].imp;

    this.getField("sld.Str1.POmod").value = modList[modProps].POmod;        this.getField("sld.Str1.MaxinDC").value = modList[modProps].MaxinDC;    this.getField("sld.Str1.MaxinV").value = modList[modProps].MaxinV;    this.getField("sld.Str1.MaxoutA").value = modList[modProps].MaxoutA;    this.getField("sld.Str1.Maxoutv").value = modList[modProps].Maxoutv;

    this.getField("PTC").value = modList[modProps].ptc;
    this.getField("ModLength").value = modList[modProps].length;
    this.getField("ModWidth").value = modList[modProps].width;
    this.getField("ModWeight").value = modList[modProps].weight;
} 

 

This script is run on Selection Change;

if( event.willCommit ) { 
    if(event.value == "") this.resetForm(["sld.Str1.Module","sld.Str2.Module","sld.Str3.Module"]); else Inheritance(event.value)
}

 

 

I have also been using this same List Box to control Layer Visibility for the product specification sheets. This is programmed with a MouseUp action in the List Box properties, which calls another Document Script using the getOCGs function:

function ctrlLayers()
{
    // get the radio button state
    var sel = this.getField("modList").value;

    var ocgs = this.getOCGs();
    for (i=0; i < ocgs.length; i++) {
        ocgs[i].state = (ocgs[i].name == sel);
    }
}

  

I have recently encountered issues with the Visibility script, namely that it has stopped working. I cannot fathom why it stopped, seemingly out of the blue. Changing the selection in the List Box still flawlessly changes all of the spec values, but the visibility of the layers seems to be dependent on whichever option is last in the List Box. 

 

My previous method to achieve the same results involved using Buttons to change all of the field values and control the visibility of images containing the spec sheet pages. However, as my form became more complex and many more text fields were added, it became increasingly difficult to add new products, as every line of the code needed to be changed individually.

 

I would like to continue to use the List Box method to control the contents of the text fields, but also return to using Images instead of Layers for the spec sheets. Unfortunatley, I am at a loss to figure out how to control the visibility of Image fields depending on the Selection Change in the List Box.

Ideally, the output value of the List Box would be passed to a document script which recognizes the value and sets the visibility of the approprate image to Visible, which otherwise would be Hidden. Maybe an if/else conditional?

 

Alternatively, if it is possible to add a script to the Image field to receive the output value of the List Box and set itself to Visible as a conditional, that would work too, I suppose.

 

I imagine that the answer to this may be extremely simple, to which I admit that the majority of my (extremely limited) knowledge on this subject has been due in large part to this forum and Thom Parker's website (SHOUTOUT!).

 

Any help would be very much appreciated!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

There are no layers in the file you shared... getOCGs returns null.

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

78

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Toggle Image Field Visibility with List Box

Community Beginner ,
Sep 10, 2020

Copy link to clipboard

Copied

Hello Adobe Community. Long time reader, first time posting here.

 

I have a List Box that I am using to change information in a large number of text fields via a function containing an array in a Document Script. These fields pertain to the spec values for different products (voltage, amperage, weight, etc), so whenever we stock a new product to the list, this has been extremely easy to add the info to the array and as an option in the List Box. 

 

Here's an abbreviated version of the script as an example:

var modList = {
LG420: { module: "LG LG420N2W-V5", wattage: "420", voc: "49.7", vmp: "42.1", isc: "10.63", imp: "9.98", ptc: "386.9", length: "79.7", width: "40.3", weight: "47.81", POmod: "505", MaxinDC: "17.5", MaxinV: "83", MaxoutA: "15", Maxoutv: "85" },

LG400: { module: "LG LG400N2T-J5", wattage: "400", voc: "49.7", vmp: "41.5", isc: "10.22", imp: "9.65", ptc: "366", length: "79.7", width: "40.3", weight: "47.81", POmod: "505", MaxinDC: "17.5", MaxinV: "83", MaxoutA: "15", Maxoutv: "85" }, 
}; 

function Inheritance(modProps) { 

    this.getField("Module").value = modList[modProps].module;
    this.getField("ModW").value = modList[modProps].wattage; 

    this.getField("sld.Str1.ModW").value = modList[modProps].wattage; 
    this.getField("sld.Str1.VOC").value = modList[modProps].voc; 
    this.getField("sld.Str1.VMP").value = modList[modProps].vmp; 
    this.getField("sld.Str1.ISC").value = modList[modProps].isc; 
    this.getField("sld.Str1.IMP").value = modList[modProps].imp;

    this.getField("sld.Str1.POmod").value = modList[modProps].POmod;        this.getField("sld.Str1.MaxinDC").value = modList[modProps].MaxinDC;    this.getField("sld.Str1.MaxinV").value = modList[modProps].MaxinV;    this.getField("sld.Str1.MaxoutA").value = modList[modProps].MaxoutA;    this.getField("sld.Str1.Maxoutv").value = modList[modProps].Maxoutv;

    this.getField("PTC").value = modList[modProps].ptc;
    this.getField("ModLength").value = modList[modProps].length;
    this.getField("ModWidth").value = modList[modProps].width;
    this.getField("ModWeight").value = modList[modProps].weight;
} 

 

This script is run on Selection Change;

if( event.willCommit ) { 
    if(event.value == "") this.resetForm(["sld.Str1.Module","sld.Str2.Module","sld.Str3.Module"]); else Inheritance(event.value)
}

 

 

I have also been using this same List Box to control Layer Visibility for the product specification sheets. This is programmed with a MouseUp action in the List Box properties, which calls another Document Script using the getOCGs function:

function ctrlLayers()
{
    // get the radio button state
    var sel = this.getField("modList").value;

    var ocgs = this.getOCGs();
    for (i=0; i < ocgs.length; i++) {
        ocgs[i].state = (ocgs[i].name == sel);
    }
}

  

I have recently encountered issues with the Visibility script, namely that it has stopped working. I cannot fathom why it stopped, seemingly out of the blue. Changing the selection in the List Box still flawlessly changes all of the spec values, but the visibility of the layers seems to be dependent on whichever option is last in the List Box. 

 

My previous method to achieve the same results involved using Buttons to change all of the field values and control the visibility of images containing the spec sheet pages. However, as my form became more complex and many more text fields were added, it became increasingly difficult to add new products, as every line of the code needed to be changed individually.

 

I would like to continue to use the List Box method to control the contents of the text fields, but also return to using Images instead of Layers for the spec sheets. Unfortunatley, I am at a loss to figure out how to control the visibility of Image fields depending on the Selection Change in the List Box.

Ideally, the output value of the List Box would be passed to a document script which recognizes the value and sets the visibility of the approprate image to Visible, which otherwise would be Hidden. Maybe an if/else conditional?

 

Alternatively, if it is possible to add a script to the Image field to receive the output value of the List Box and set itself to Visible as a conditional, that would work too, I suppose.

 

I imagine that the answer to this may be extremely simple, to which I admit that the majority of my (extremely limited) knowledge on this subject has been due in large part to this forum and Thom Parker's website (SHOUTOUT!).

 

Any help would be very much appreciated!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

There are no layers in the file you shared... getOCGs returns null.

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

79

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 10, 2020 0
Most Valuable Participant ,
Sep 10, 2020

Copy link to clipboard

Copied

Check the JS Console after making a selection in the list-box...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 10, 2020 0
Community Beginner ,
Sep 10, 2020

Copy link to clipboard

Copied

When I select a different option in the List Box, the Console shows:

TypeError: getField(...) is null
1:Field:Calculate

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 10, 2020 0
Adobe Community Professional ,
Sep 10, 2020

Copy link to clipboard

Copied

Looks like that you use a name of a field which doesn't exists.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 10, 2020 0
Most Valuable Participant ,
Sep 11, 2020

Copy link to clipboard

Copied

There are no layers in the file you shared... getOCGs returns null.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
Community Beginner ,
Sep 11, 2020

Copy link to clipboard

Copied

Thank you for your replies!

 

You're right. Strangely, it looks like I deleted the OCG's but didn't recognize it because the images are still present. After replacing the layers, the original script works again. Thank you for that!

 

To clarify though, my question is whether it is possible to control the visibility of image fields using the list box?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
Adobe Community Professional ,
Sep 11, 2020

Copy link to clipboard

Copied

Change the property "display" of the image fields.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
Most Valuable Participant ,
Sep 11, 2020

Copy link to clipboard

Copied

Sure, it's possible, by setting the display property, which you were already doing...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
Community Beginner ,
Sep 10, 2020

Copy link to clipboard

Copied

I created another Document Script that references my List Box, but I am having trouble getting it to control the visibility.

So far I have tried it two ways:

if (this.getField("modList").value !== "CAN320"){  
    this.getField("CAN320").display = display.hidden;
} else if (this.getField("modList").value == "CAN320"){
    this.getField("CAN320").display = display.visible;
}

and

if (this.getField("modList").value == "PAN330"){  
    this.getField("PAN330").display = display.visible;
} else {
    this.getField("PAN330").display = display.hidden;
}


if (this.getField("modList").value == "QCELL345"){  
    this.getField("QCELL345").display = display.visible;
} else {
    this.getField("QCELL345").display = display.hidden;
}

but neither are working.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 10, 2020 0
Adobe Community Professional ,
Sep 11, 2020

Copy link to clipboard

Copied

Hi,

 

The above code works when run on its own,

 

if (this.getField("modList").value == "PAN330"){  
    this.getField("PAN330").display = display.visible;
} else {
    this.getField("PAN330").display = display.hidden;
}


if (this.getField("modList").value == "QCELL345"){  
    this.getField("QCELL345").display = display.visible;
} else {
    this.getField("QCELL345").display = display.hidden;
}

 

I think the problem is that another script is failing which might be stopping this script from being run.

I will try and get time to investigate further.

 

Regards

 

Malcolm

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
Adobe Community Professional ,
Sep 11, 2020

Copy link to clipboard

Copied

Hi,

 

Spotted a couple of things that look incorrect to me.

 

There not being any OCG's which I believe is covered above, but you could just add a null check to get around that issue

if (ocgs != null) {

// do code here

}

 

and you are using simplified notation to do a sum in the eSysSize field which is

eModnum * eModW / 1000

 

Only I can't find fields called that, I can only see fields called

eSys.eModnum

eSys.eModW

 

And both of those fields have duplicates else where in the form, (off to look up how to access a specific instance of a field in simplified field notation), although it might be worth moving this straight to JavaScript.

 

Regards

 

Malcolm

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
Community Beginner ,
Sep 11, 2020

Copy link to clipboard

Copied

Thank you for your reply!

To clarify, I am trying to move away from using OCG's and am asking how to control the visibility of image fields using a list box.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
Adobe Community Professional ,
Sep 11, 2020

Copy link to clipboard

Copied

Can you share your PDF file? It's always easier to figure out what's wrong by actually looking at a file, seeing where and how a script is used, and how it interacts with other elements in the form. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
Adobe Community Professional ,
Sep 11, 2020

Copy link to clipboard

Copied

The OP have shared the PDF file.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
Most Valuable Participant ,
Sep 11, 2020

Copy link to clipboard

Copied

You have to adjust your code accordingly, then. Either remove the call to that function or make it handle the situation in which no OCGs are found. Currently it's not handled and that's triggering an error that prevents the code from completing.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 1
Community Beginner ,
Sep 11, 2020

Copy link to clipboard

Copied

Thank you!

  1. Where would I put the code for the null ocgs? In the document script?
  2. Thank you for the eSysSize correction! In regards to this, I've noticed recently that simplified notation calculations do not seem to work well with grouped fields (ones with the "."). Am I doing something wrong in this regard? Even after changing the field names to correspond with the fields they are meant to reference, the calculation does not work. The only way I've found to resolve this is to change the field names I'm referencing by taking out the '.', using the new field names in the calc. Only then will it do the math.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0
try67 LATEST
Most Valuable Participant ,
Sep 11, 2020

Copy link to clipboard

Copied

1. Add this line to your function:

if (ocgs==null) return;

2. I recommend you use a script for these calculations. The Simple Field Notation option is very limited.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 11, 2020 0