Skip to main content
irinAP
Participant
August 13, 2018
Question

Hide/show layer and fields (interactive pdf)

  • August 13, 2018
  • 2 replies
  • 3004 views

Hope someone is able to help with my question: How can I make layer visible (a pop up message) with a button click and hide all the other fields on a page at the same time, and revert back (hide layer and make all the fields visible) on another click of the same button. Thanks!

This topic has been closed for replies.

2 replies

Inspiring
August 13, 2018

Here's a good tutorial on working with layers (OCGs) using JavaScript: https://acrobatusers.com/tutorials/creating-and-using-layers-ocgs-with-acrobat-javascript

To control the visibility or form fields, use the "display" field property, for example:

// Show this field

getField("text1").display = display.visible;

If you use hierarchical field naming for the group of fields that you want to control (e.g., group1.text1, group1.text2, group1.checkbox4, etc), you can show/hide all of the fields in the group with a single statement:

// Hide all of the group1 fields

getField("group1").display = display.hidden;

irinAP
irinAPAuthor
Participant
August 13, 2018

Thank you for your insight, George. But I feel like I need a conditional statement for desired functionality to work. I was able to find this js code:

var layerName = "Chart1";

var layers = this.getOCGs();

if (layers!=null && layers.length!=0) {

for (var i in layers) {

if (layers.name==layerName) {

layers.state = !layers.state;

break;

}

}

}

This works - shows my hidden layer but I need to hide the rest of the fields on this page so that they don't show on top of my pop-up layer. Next I need to hide my pop-up layer on another click and show my fields back again.

try67
Community Expert
Community Expert
August 13, 2018

A layer is not defined on a specific page. You will need to hard-code the page number into the code, if you want to do it.

Lukas Engqvist
Community Expert
Community Expert
August 13, 2018

You can do this with the show/hide field function.

Your alert/message is one button, easiest is just to have it cover all other fields in stack order you can add as many show/hide functions as you like to show/hide additional form items.

Clicking somewhere on the alert button will hide itself (and if necessary show other buttons/fields).

This can be done in Acrobat or in InDesign. Giving good names is the key to keep you from going crazy.