Hide all layers

New Here ,
May 21, 2022 May 21, 2022

Copy link to clipboard

Copied

I have made a multi-page document with forms. Each page has a background image in its own layer. Is it possible to hide them all at the touch of a button? I want the user to be able to choose not to print the background and turn off over 30 layers take time.

TOPICS
Edit and convert PDFs , JavaScript , PDF forms

Views

139

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
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , May 22, 2022 May 22, 2022

Try following script:

var layerName = "Bakgrunn";
var layers = this.getOCGs();
if (layers!=null && layers.length!=0) {
  for (var i in layers) {
    if (layers[i].name==layerName)
      layers[i].state = !layers[i].state;
  }
}

Likes

Translate

Translate
Adobe Community Professional ,
May 21, 2022 May 21, 2022

Copy link to clipboard

Copied

You may be able to achieve this by employing the use of JavaScript, or simply using Acrobat's built-in features, such as radio buttons or checkboxes  to set layer visibility with show or hide state.

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
community guidelines
New Here ,
May 21, 2022 May 21, 2022

Copy link to clipboard

Copied

I'm not so familiar with JavaScript but after a lot of googling I tried this code

 

var layerName = "Bakgrunn";
var layers = this.getOCGs();
if (layers!=null && layers.length!=0) {
for (var i in layers) {
if (layers[i].name==layerName) {
layers[i].state = !layers[i].state;
break;
}
}
}

 

All the layers have the same name, but this did only worked on the page of the check box. The same happend for the built-in feature for hiding layers. I want it to hide all the layers on every page with one click. 

I don't know if it maters but I only have this background layer on every page, no other layers.

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
community guidelines
Adobe Community Professional ,
May 22, 2022 May 22, 2022

Copy link to clipboard

Copied

Try following script:

var layerName = "Bakgrunn";
var layers = this.getOCGs();
if (layers!=null && layers.length!=0) {
  for (var i in layers) {
    if (layers[i].name==layerName)
      layers[i].state = !layers[i].state;
  }
}

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
community guidelines
New Here ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

That works exactly like I want it to. Thank you.

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
community guidelines
Adobe Community Professional ,
May 22, 2022 May 22, 2022

Copy link to clipboard

Copied

This code doesn't hide a layer, it toggles its visibility. If it was hidden it will make it visible, and vice versa.

To actually hide it change this line:

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

To:

layers[i].state = false;

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
community guidelines
New Here ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

Sorry I probably wrote this a little wrong. I wanted a way to toggle the layers visibility on and off.

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
community guidelines
Adobe Community Professional ,
May 23, 2022 May 23, 2022

Copy link to clipboard

Copied

LATEST

To toggle layers visible or not visible is just easier to employ a pair of radio buttons or checkboxes.

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
community guidelines
Adobe Community Professional ,
May 22, 2022 May 22, 2022

Copy link to clipboard

Copied

What are the names of the layers?

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
community guidelines
New Here ,
May 22, 2022 May 22, 2022

Copy link to clipboard

Copied

The name for all layers are "Bakgrunn" 

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
community guidelines