Using Javascript Action in Dropdown to hide/show a layer

Community Beginner ,
Nov 02, 2017

Copy link to clipboard

Copied

I have a PDF set up with a dropdown. The dropdown named "Country" allows users to select their Country/Region. I have 27 other layers in my PDF, each having an image of a flag. They are stacked on top one another. My hopes is to have the layer toggle from hidden to visible based on the selected country in the dropdown. I found many topics about buttons or checkboxes, but could not find anything based on dropdowns.

Please find my PDF through the link below

Shared Files - Acrobat.com

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

Your best option is any of the field scripts that trigger when an option is selected from the drop down. My favorite is the Keystroke script on the "Format" tab, when the "Custom" option is selected. 

For scripting purposes it is best to have a direct relationship between the options in the dropdown and the OCGs (Layers). I noticed that the OCGs on the PDF are named "Layer 1", etc. These names make the scripting more complicated, because you need to somehow relate "Layer 2" to "USA".

For example, have the OCGs named for the country, so the OCG name and the dropdown name are the same, i.e. the layer for USA is named "USA". Or have them in the same order so the index if the dropdown selection is the same as the position of the the OCG in the returned array. I don't like this one cause it's hard to manage.  Here's an example that assumes the names are the same.

In Format Keystroke script

if(event.willCommit)

  this.getOCGs().some(function(a){return a.state = (a.name==event.value);});

To use the code you'll need to do two things,

1. Rename all layers to their country names

2. Flatten out "Layer 1", since it is the main background for the document.

TOPICS
Acrobat SDK and JavaScript, Macintosh

Views

905

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

Using Javascript Action in Dropdown to hide/show a layer

Community Beginner ,
Nov 02, 2017

Copy link to clipboard

Copied

I have a PDF set up with a dropdown. The dropdown named "Country" allows users to select their Country/Region. I have 27 other layers in my PDF, each having an image of a flag. They are stacked on top one another. My hopes is to have the layer toggle from hidden to visible based on the selected country in the dropdown. I found many topics about buttons or checkboxes, but could not find anything based on dropdowns.

Please find my PDF through the link below

Shared Files - Acrobat.com

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

Your best option is any of the field scripts that trigger when an option is selected from the drop down. My favorite is the Keystroke script on the "Format" tab, when the "Custom" option is selected. 

For scripting purposes it is best to have a direct relationship between the options in the dropdown and the OCGs (Layers). I noticed that the OCGs on the PDF are named "Layer 1", etc. These names make the scripting more complicated, because you need to somehow relate "Layer 2" to "USA".

For example, have the OCGs named for the country, so the OCG name and the dropdown name are the same, i.e. the layer for USA is named "USA". Or have them in the same order so the index if the dropdown selection is the same as the position of the the OCG in the returned array. I don't like this one cause it's hard to manage.  Here's an example that assumes the names are the same.

In Format Keystroke script

if(event.willCommit)

  this.getOCGs().some(function(a){return a.state = (a.name==event.value);});

To use the code you'll need to do two things,

1. Rename all layers to their country names

2. Flatten out "Layer 1", since it is the main background for the document.

TOPICS
Acrobat SDK and JavaScript, Macintosh

Views

906

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
Nov 02, 2017 0
Most Valuable Participant ,
Nov 02, 2017

Copy link to clipboard

Copied

Use the code you found. You can use the drop-down's validation script, and access its new value using event.value.

Make sure to tick the option to commit the selected value immediately, for it to work smoothly. You'll find it under the field's Properties, Options tab.

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...
Nov 02, 2017 0
Adobe Community Professional ,
Nov 02, 2017

Copy link to clipboard

Copied

Your best option is any of the field scripts that trigger when an option is selected from the drop down. My favorite is the Keystroke script on the "Format" tab, when the "Custom" option is selected. 

For scripting purposes it is best to have a direct relationship between the options in the dropdown and the OCGs (Layers). I noticed that the OCGs on the PDF are named "Layer 1", etc. These names make the scripting more complicated, because you need to somehow relate "Layer 2" to "USA".

For example, have the OCGs named for the country, so the OCG name and the dropdown name are the same, i.e. the layer for USA is named "USA". Or have them in the same order so the index if the dropdown selection is the same as the position of the the OCG in the returned array. I don't like this one cause it's hard to manage.  Here's an example that assumes the names are the same.

In Format Keystroke script

if(event.willCommit)

  this.getOCGs().some(function(a){return a.state = (a.name==event.value);});

To use the code you'll need to do two things,

1. Rename all layers to their country names

2. Flatten out "Layer 1", since it is the main background for the document.

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...
Nov 02, 2017 0
Community Beginner ,
Nov 09, 2017

Copy link to clipboard

Copied

Thank you Thom. Great explanation and easy to follow. Simple to set up and it all worked out nicely. There were times where if you clicked on one Country and then clicked on another without going to another field both layers would be visible but I can just prompt people to click on another field first if they need to chose another Country from the dropdown.

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...
Nov 09, 2017 0
Adobe Community Professional ,
Nov 09, 2017

Copy link to clipboard

Copied

Thank you. I'm glad it worked out so well.  To solve the issue with having to click on another field, set the "Commit Selected Value Immediately" check box on the "Options" tab of the "Properties" dialog for the dropdown.

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...
Nov 09, 2017 0