15 Replies Latest reply on Mar 22, 2017 7:47 AM by shazias99501584

    Hide/Show layers with Javascript

    jajawheels Level 1

      Hello,

       

      I am trying to find a Javascript method for hiding/showing layers in a pdf document. I would like to be able to use an if/else statement to set the visibility of multiple layers.

       

      Let's say I have 5 layers on a single page, all set to hidden by default. This page also has a field with 5 options relating to each of the 5 layers. Once an option is selected, an if/else will use this method to set the appropriate layer to visible.

       

      Thank you!

        • 1. Re: Hide/Show layers with Javascript
          try67 MVP & Adobe Community Professional

          A layer is an OCG (Optional Content Group), so you would use the getOCGs method of the Document object to get all the layers of a certain page, and then iterate over the array that it returns to find the specific one you want to edit. To show/hide a layer you set its state value to true or false, resp.

          See the documentation of getOCGs for an example of how to do it.

          • 3. Re: Hide/Show layers with Javascript
            jajawheels Level 1

            Thank you sirs.

            • 4. Re: Hide/Show layers with Javascript
              John Frederick Chionglo Level 1

              Here is another way to satisfy your layer requirements in PDF.

              1. Create a Field (type: button) for each layer. Suppose the five “layers” are called “LayerButton1”, “LayerButton2”, “LayerButton3”, “LayerButton4”, and “LayerButton5” (using Acrobat Professional or using the doc.addField API).

              2. Store an image for each layer (using Acrobat Professional or using the buttonSetIcon API).

              3. Position each button on the page anywhere you want (using the rect property)

              4. Each option that controls the visibility of a respective “layer” may also be a button. Create a Field (type: button) for each control.  Suppose the five “control buttons” are called “ToggleLayerButton1”, “ToggleLayerButton2”, “ToggleLayerButton3”, “ToggleLayerButton4”, and “ToggleLayerButton5”.

              5. Position each control button on the page anywhere you want.

              6. For ToggleLayerButton1, add the following MouseUp action (using Acrobat Professional or using the Field.setAction API):
                var fld = this.getField("Button1");
                if (fld.display == display.hidden)
                  fld.display = display.visible;
                else
                  fld.display = display.hidden;

               

               

              7. Repeat the MouseUp action for the other control buttons.

              I hope this helps. - john

              • 5. Re: Hide/Show layers with Javascript
                shazias99501584 Level 1

                Hi,

                I'm trying to hide/show a layer (depending on a checkbox) which consists of checkboxes and text-field to allow a user to add their information. When I add in check-boxes and text fields to a layer (let's say layer A), once I hide it, the checkboxes remain on the default page but the text is hidden. I tried to create a separate document with the checkboxes and text-fields in Adobe and import the layer in my merged document, but the checkboxes and text fields are not interactive instead are displayed as squares. How can I hide everything including the interactive checkboxes and text fields on a specific layer?

                Furthermore, is there a way to hide a whole section on a PDF page based on a checkbox?

                 

                Thanks!

                • 6. Re: Hide/Show layers with Javascript
                  try67 MVP & Adobe Community Professional

                  Fields don't (normally) belong to a layer. They exist at a different level of the PDF file than layers.

                  You would need to hide (or show) the layers and then hide (or show) the fields that belong to them, separately.

                   

                  Not sure what you mean about hiding a whole section of a PDF... You can hide a layer and you can hide form fields or cover parts of a page using a field.

                  • 7. Re: Hide/Show layers with Javascript
                    shazias99501584 Level 1

                    Okay that's what I thought. I tried to hide a textbox using JavaScript that was in Layer A (which was linked to a check-box for visibility), but this froze my entire PDF document multiple times.

                     

                    I probably didn't explain this well... basically, I have two checkboxes, CheckBox1 and CheckBox2. If someone checks off Checkbox 1, this opens up Layer A on a default blank page which lists a series of question and interactive features such as checkboxes and field-text. The same is done with Checkbox 2. When someone checks off CheckBox2, this opens up Layer B with a list of different questions and interactive features. Since I know it's not possible to hide the interactive features along with the selection of a layer, is it possible to hide an entire section (let's say section A) if CheckBox1 is not checked and show if it is checked? The same would be with Checkbox2 and Section B.

                    • 8. Re: Hide/Show layers with Javascript
                      try67 MVP & Adobe Community Professional

                      It is possible to hide fields alongside a layer, but it needs to be done using a script.

                      • 9. Re: Hide/Show layers with Javascript
                        shazias99501584 Level 1

                        Can you give me the script please?

                        • 10. Re: Hide/Show layers with Javascript
                          try67 MVP & Adobe Community Professional

                          Here's a basic example that hides a layer and some fields at the same time:

                           

                          var ocgArray = doc.getOCGs();
                          for (var i=0; i < ocgArray.length; i++) {
                              if (ocgArray[i].name == "Layer 1") {
                                  ocgArray[i].state = false;
                              }
                          }
                          this.getField("Field1").display = display.hidden;
                          this.getField("Field2").display = display.hidden;
                          this.getField("Field3").display = display.hidden;
                          
                          • 11. Re: Hide/Show layers with Javascript
                            shazias99501584 Level 1

                            Hi Try67,

                             

                            Your code worked- when I uncheck the boxes (that's linked to the layer), the fields (Text2 and Check Box2) hide, however, when I check the box, Text2 and Check Box2 remains hidden.

                             

                            This is the code I used:

                             

                            var layer = this.getOCGs();

                            for(var i=0; i<layer.length; i++)
                            {
                            if(event.target.value=="Yes")
                            {
                            if(layer[i].name=="External Layer")
                            {layer[i].state=true};
                            }
                            else
                            {
                            if(layer[i].name=="External Layer")
                            {layer[i].state=false};
                            }
                            }

                            this.getField("Text2").display = display.hidden;
                            this.getField("Check Box2").display = display.hidden;

                             

                             

                            How can I fix this? Appreciate your help!

                            • 12. Re: Hide/Show layers with Javascript
                              try67 MVP & Adobe Community Professional

                              You need to make the last two lines of the code conditional as well. Something like this:

                               

                              if (event.target.value=="Yes")

                                  this.getField("Text2").display = display.visible;

                                  this.getField("Check Box2").display = display.visible;

                              } else {

                                  this.getField("Text2").display = display.hidden;

                                  this.getField("Check Box2").display = display.hidden;

                              }

                              • 13. Re: Hide/Show layers with Javascript
                                shazias99501584 Level 1

                                Still getting a Syntax error. Is this correct:

                                 

                                var layer = this.getOCGs();

                                for(var i=0; i<layer.length; i++)
                                {
                                if(event.target.value=="Yes")
                                {
                                if(layer[i].name=="External Layer")
                                {layer[i].state=true};
                                }
                                else
                                {
                                if(layer[i].name=="External Layer")
                                {layer[i].state=false};
                                }
                                }

                                this.getField("Text2").display = display.hidden;
                                this.getField("Check Box2").display = display.hidden;
                                }

                                {
                                if (event.target.value=="Yes")

                                    this.getField("Text2").display = display.visible;

                                    this.getField("Check Box2").display = display.visible;

                                } else {

                                    this.getField("Text2").display = display.hidden;

                                    this.getField("Check Box2").display = display.hidden;

                                }

                                • 14. Re: Hide/Show layers with Javascript
                                  try67 MVP & Adobe Community Professional

                                  What syntax error?

                                   

                                  On Mon, Mar 13, 2017 at 6:10 PM, shazias99501584 <forums_noreply@adobe.com>

                                  • 15. Re: Hide/Show layers with Javascript
                                    shazias99501584 Level 1

                                    Hi,

                                    The issue was sorted out.

                                     

                                    I have another question- in hopes that you can help again.

                                     

                                    I've been creating reset buttons for my PDF fillable form and it's been working perfectly. For example, I have two buttons- one button resets the first page and the second button resets the second page. However, when I create a copy of the PDF form and test out the reset buttons, I notice that the boxes under Reset button is not functioning properly. When I click the first button, the second page erases some items. Can you tell me what the issue is?

                                     

                                    Thanks,