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

Java script to show/hide layers and checkboxes

New Here ,
Feb 18, 2025 Feb 18, 2025

I'm trying to control the visibility of certain checkboxes and layers on my PDF form according to what is selected in a drop down menu. I've found 2 scripts; one to show/hide the layers and one to show/hide the checkboxes. I'm a novice when it comes to java scripting. How do I combine them?

 

If "Option1" is selected from the dropdown, the layer of the same name along with Box3 and Box4 should show.

If "Option2" is selected, the layer of the same name along with Box1 and Box2 should show.

If "-Select-" is selected (default), none of the options above should show.

 

Layers script:

var layers = this.getOCGs();

var v = event.value;

for (var i = 0; i < this.layers.length; i++) {

    if (layers[i].name == “Option1” && v == "Option1") {

        layers[i].state = true;

    }

    else if (layers[i].name == “Option2” && v == "Option2") {

        layers[i].state = true;

    }

    if (layers[i].name == "Option1" && v == "Option2") {

        layers[i].state = false;

    }

    else if (layers[i].name == "Option2" && v == "Option1") {

        layers[i].state = false;

    }

    if (layers[i].name == "Option1" && v == "-Select-") {

        layers[i].state = false;

    }

    if (layers[i].name == “Option2” && v == "-Select-") {

        layers[i].state = false;

    }

}

 

Checkbox script:

var v = event.value;

if(v == "-Select-"){

this.getField(“Box1”).display = display.hidden;

this.getField("Box2").display = display.hidden;

this.getField("Box3”).display = display.hidden;

this.getField("Box4").display = display.hidden;}

if(v == "Option2"){

this.getField("Box1").display = display.visible;

this.getField("Box2").display = display.visible;

this.getField("Box3”).display = display.hidden;

this.getField("Box4").display = display.hidden;}

if(v == "Option1"){

this.getField("Box1").display = display.hidden;

this.getField("Box2").display = display.hidden;

this.getField("Box3”).display = display.visible;

this.getField("Box4").display = display.visible;}

 

Any help or advice would be greatly appreciated!

Thanks,

Trisha

TOPICS
How to , JavaScript , PDF , PDF forms
800
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 ,
Feb 19, 2025 Feb 19, 2025

It means the line above the one highlighted has a curly end quote after "Box3 instead of a straight quote.  I can see a lot of other curly quotes as well.  Correct those and make sure you are using a plain text editor to write your scripts so this does not happen.  I believe console in Mac is Cmd + j, or you can search for "Debugger" in the Tools tab search.

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

Removed by author.

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

Not quite what I'm trying to do. The link you provided is about using a checkbox to control layer visibility.

I'm want to use a drop down to control the visibility of both layers and checkboxes.

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

Just put them one after another. They should work independently of each other.

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

That's what I thought but it didn't work at all when I combined them.

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

Which part doesn't work?  Do both scripts work independently?  If so they should work together.  If not, check the 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 ,
Feb 19, 2025 Feb 19, 2025

Both scripts work independently. When I put them together, no matter what order they are in. I'm on a Mac so your Console instructions don't work for me. Screen shot below shows error in Acrobat. I have no idea what it means.

2025-02-19_06-58-59.jpgexpand image

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

It means the line above the one highlighted has a curly end quote after "Box3 instead of a straight quote.  I can see a lot of other curly quotes as well.  Correct those and make sure you are using a plain text editor to write your scripts so this does not happen.  I believe console in Mac is Cmd + j, or you can search for "Debugger" in the Tools tab search.

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

THANK YOU!!!

 

I did alter the script in a Text Edit program but had no idea about 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
New Here ,
Mar 03, 2025 Mar 03, 2025

Everything is working, I appreicate your help!!

 

One more issue though...

I didn't realize that the PDF doesn't save with the layers visible that were chosen by the dropdown. Any idea how to resolve this? I've read that it can be done but I know next to nothing about java.

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 ,
Mar 03, 2025 Mar 03, 2025

I figured it out. Have to add 

layers[i].initState = layers[i].state;

after each layer state.

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 03, 2025 Mar 03, 2025

That won't work if the file will be viewed in Reader.

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 ,
Mar 03, 2025 Mar 03, 2025

I don't know what program each customer will use to view the form.

 

Is there something that will make it wwork in both Acrobat and Reader?

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 03, 2025 Mar 03, 2025

To make it work in Reader you will need to use a script to save the state of the layers when the file is closed (or saved) to something like a field or the file's metadata, and another script to read the info and then use it when the file is opened.

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 03, 2025 Mar 03, 2025

Leave your original script and enter the following document level script (change all instances of "Dropdown" to the actual field name):

var v = this.getField("Dropdown").value;
this.getField("Dropdown").value="-Select-";
this.getField("Dropdown").value="Option1";
this.getField("Dropdown").value=v;

 

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 ,
Mar 07, 2025 Mar 07, 2025

Thank you, PDF Automation Station. But it's not working for me.

 

I changed "Dropdown" to the field name and created a line for each dropdown selection. Placed the script in Document JavaScripts. Saved & opened in Reader and nada.

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 07, 2025 Mar 07, 2025

The script captures the value of the Dropdown then changes the value to 2 posbilities so your visibility script in the dropdown will run, then changes it back to the original value so the visibility script will run again.  Can you post your form?

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 ,
Mar 07, 2025 Mar 07, 2025

PDF attached. Hopefully I've removed/changed all company info. Note: there are 2 layers not being used currently. They can be ignored.

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 07, 2025 Mar 07, 2025

Your document level script has a different field in the first line than the field names in the rest of the scripts.  There is an error in the script.  There is no field named TaylorFacility.  You have another problem.  You are using the variable v elsewhere in a calculation and it is being changed to the other value during the doc level script.  Change the document level script to this:

 

var vlu = this.getField("Facility").value;
this.getField("Facility").value="- Select -";
this.getField("Facility").value="BLM (OSP)";
this.getField("Facility").value=vlu;

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 ,
Mar 07, 2025 Mar 07, 2025

That was my mistake in trying to hide the company info. I've gotten permission to share the real form now. We want the layer to default to "- Select -". Then the customer would select the correct facility and save the form. When we get it back, it should show the layer the customer selected.

 

With the way I have the code, it's changing the layer to LSV upon open.

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 07, 2025 Mar 07, 2025

The script I provided does what you ask.  Here it is modified for the new form presented:

var vlu = this.getField("Taylor Facility").value;
this.getField("Taylor Facility").value="- Select -";
this.getField("Taylor Facility").value="Taylor Print Impressions - BLM (OSP)";
this.getField("Taylor Facility").value=vlu;

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 ,
Mar 07, 2025 Mar 07, 2025

Sorry, you're right. It's just not what I was expecting. As I've said, I'm a newbie at complicated forms and java.

 

When the facility is selected in Reader, it's doesn't make the layers and items for that facility visible until the document is saved and reopened. Is there a way to change that?

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 07, 2025 Mar 07, 2025

It works in Reader but you have errors in the calculation script when used with Reader.  layers[i].initState can't be used in Reader.  Remove all instances and it should work.

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 ,
Mar 10, 2025 Mar 10, 2025
LATEST

Thank you, thank you, thank you!!!!! I really appreciate 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