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

Using Javascript Action in Dropdown to hide/show a layer

Community Beginner ,
Nov 02, 2017 Nov 02, 2017

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

TOPICS
Acrobat SDK and JavaScript
3.1K
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

correct answers 1 Correct answer

Community Expert , Nov 02, 2017 Nov 02, 2017

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

...
Translate
Community Expert ,
Nov 02, 2017 Nov 02, 2017

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.

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 ,
Nov 02, 2017 Nov 02, 2017

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.

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

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.

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

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.

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