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

Show/Hide Layers Using a Dropdown Menu

Community Beginner ,
May 07, 2024 May 07, 2024

I have a base layer that I want to remain visible, but I would like to switch between the rest of my layers by using a dropdown menu. I'm trying to write it such that when one layer is selected, the rest are hidden. This is what I have so far, but when I select anything from the dropdown, nothing changes.

 

// Define the layer names
var layerNames = ["Emergency Repair", "CSA Visit", "Installation", "Other"];

// Function to toggle layers based on dropdown selection
function toggleLayers() {
var selectedOption = event.target.value;
for (var i = 0; i < layerNames.length; i++) {
var layer = this.getOCGs()[layerNames[i]];
if (layer) {
if (layerNames[i].replace(/\s+/g, '') === selectedOption) {
layer.state = !layer.state;
} else {
layer.state = false;
}
}
}
}

// Add an event listener to the dropdown
this.getField("Dropdown2").setAction("Keystroke", "toggleLayers()");


Apologies if this code looks weird. I've resorted to asking ChatGPT for help.

TOPICS
How to , JavaScript , Modern Acrobat , PDF forms
1.5K
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 ,
May 08, 2024 May 08, 2024

Yeah, I was going to ask you about that... Just use this:

 

var layers = this.getOCGs();
for (var i in layers) {
	layers[i].state = layers[i].name=="Guides and Grids" || (layers[i].name==event.value);
}

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 ,
May 07, 2024 May 07, 2024

> I've resorted to asking ChatGPT for help.

That's your main issue. This code contains a lot of 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
Community Expert ,
May 07, 2024 May 07, 2024

Get rid of all the other code, and use this as the drop-down's custom Validation script:

 

var layers = this.getOCGs();
for (var i in layers) {
	layers[i].state = (layers[i].name==event.value);
}
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 ,
May 08, 2024 May 08, 2024

Good grief. I knew the code should be short, but I didn't think it would be quite that short. That's taken care of a large portion of things, but the background layer (Named "Background and Title") disappears when I use this. Any thoughts?

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 ,
May 08, 2024 May 08, 2024

Yeah, I was going to ask you about that... Just use this:

 

var layers = this.getOCGs();
for (var i in layers) {
	layers[i].state = layers[i].name=="Guides and Grids" || (layers[i].name==event.value);
}
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 ,
May 08, 2024 May 08, 2024
LATEST

It works! Thanks a bunch for the 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
Adobe Employee ,
May 07, 2024 May 07, 2024

Hi there

Hope you are doing well and thanks for reaching out.

The workflow you are trying to achieve might be possible using JavaScript. For more information, please check the help pages listed below:
https://acrobatusers.com/tutorials/javascript_console/
https://helpx.adobe.com/acrobat/using/applying-actions-scripts-pdfs.html

Hope it will help

Regards
Amal

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