11 Replies Latest reply on Nov 25, 2011 10:56 AM by James A L.

    Button to toggle through layers

    jay fresno Level 1

      Does anyone know if it is possible to create a button in Acrobat 9 that will toggle through the visibility of each layer, from one layer to the next, each time it is clicked? Or perhaps there is some other way to toggle through the visibility of multiple images?

        • 1. Re: Button to toggle through layers
          Dave Merchant MVP & Adobe Community Professional

          Yes it is. You can use JavaScript on a button, and the doc.getOCGs() array. See the example in the Acrobat SDK. Simplest solution for your purposes would be to set a counter variable in a doc-level script, and use a function similar to the example to hide all but that OCG, increment the counter, etc. You may also want to hide the OCGs from the side pane so the user can't manually play with combinations, by sending doc.setOCGOrder() a null array.

           

          It's also possible (though not in Acrobat itself) to define a logical relationship between OCGs, making them exclusive-or. That way if the user turns one on using the side pane, the others automatically turn off. Defining these rules is something only possible with plugins.

          • 2. Re: Button to toggle through layers
            jay fresno Level 1

            Dave,

            I opened a layered PDF, inserted a button, copied the sample code into the button, but when clicking the button nothing happens. When clicking the button, I would like the PDF to display one layer at a time. Is that possible?

            • 3. Re: Button to toggle through layers
              gkaiseril MVP & Adobe Community Professional

              Try the following script to see the state of your layers on page 1.

               

              function ListOCG(oDoc, nPageNum) {

                 var ocgArray = new Array();  // array of layers or OCG
                 if(typeof nPageNum == 'undefined') {
                    ocgArray = oDoc.getOCGs(); // OCG array for whole PDF
                 } else { 
                    ocgArray = oDoc.getOCGs({nPage: nPageNum}); //  OCG array for page specific page in PDF
                 }
                 for (var i=0; i < ocgArray.length; i++) {
                    console.println(ocgArray[i].name + ": " + ocgArray[i].state); // list OCG entry and state
                 } // end ocg array
              return true;
              }  // end function

               

              console.show(); console.clear();

              ListOCG(this, 0);

               

              Use to see all of the layers in the PDF.:

               

              ListOCG(this);

               

              Note that if you add a watermark,then there will be a layer called "Watermark' in the OCG array.

              • 4. Re: Button to toggle through layers
                jay fresno Level 1

                GKaisersil,

                 

                Thank you for your suggestions.

                 

                I probably didn't make clear what I want to accomplish. When anyone viewing the PDF clicks a button, I would like the button to show one layer at a time, kind of like a slideshow, but with layers. Show layer 1 and hide all other layers, then show layer 2 and hide all other layers, and so forth.

                 

                I put two buttons in the PDF. One with the first code you mentioned, and one with the second code (ListOCG(this);).

                 

                The button with the first code brings up the JavaScript Debugger window (as below) and the second button does nothing.

                Untitled-1.gif

                • 5. Re: Button to toggle through layers
                  Dave Merchant MVP & Adobe Community Professional

                  The scripts we've linked you to are examples of how the objects work, not matched answers to your cycle-through-every-layer question. Please bear in mind we're here to offer general assistance and not to build people final solutions.

                   

                   

                  Having said that, cycling through layers (or anything else in an array) is very easy in JS. For example put this into a doc-level script (NOT inside a function):

                   

                  var ocgCounter = 0;

                   

                  function toggleOCGs() {
                    var ocgArray = this.getOCGs(this.pageNum);
                    var ocgNum = ocgArray.length - 1;
                    for (var i=0; i <= ocgNum; i++) {
                      ocgArray[i].state = (i==ocgCounter);
                    }
                    ocgCounter++;
                    if (ocgCounter==ocgNum) ocgCounter = 0;
                  }

                   

                  and add a button to the page with a JavaScript action that calls

                   

                  toggleOCGs();
                  • 6. Re: Button to toggle through layers
                    gkaiseril MVP & Adobe Community Professional

                    I have provided a function that list the layers for a page or the entire PDF. Like many functions, how it works is dependent upon what values you pass to it.

                     

                    If the line "ListOCG(this);" is used to replace 'ListOCG(this, 0);' and displays all the layers in a multi page PDF file. You may need to open the JavaScirpt debugger console.

                     

                    Note that you have found another hidden system layer, 'Guides and Grids'. You may need to adjust your script for toggling the layers for this code or you will need to specify the layers you want to toggle.

                     

                    You will see that you need to obtain the index number of layer you want to toggle and not just the name, although you could loop through all of the names each time and find the one that is 'true' and then change that one to false and set the next indexed layer to true. And for the case where you no layer toggled, then set the 'state' for the '0' indexed element of the OCG array.

                    • 7. Re: Button to toggle through layers
                      jay fresno Level 1

                      Dave,

                       

                      I ask for your understanding that I am not proficient at JavaScript. I do very much appreciate all the help you have given me.

                       

                      I followed your instructions to put the JavaScript into a doc level script, and added a button on the page. This is working as expected, except that I have 4 layers (not including the Guides and Grids layer that George mentioned). On each click of the button, the layer visibility goes from layer 1, to layer 2, to layer 3, then it skips layer 4, and goes back to showing layer 1. The layers are listed in reverse order in the Layers panel with Layer 1 at the bottom directly above Guides and Grides, and Layer 4 at the top.

                       

                      Although I don't understand JavaScript, I looked through the script and didn't see anything obvious that would cause that behavior. Is there a way to fix this glitch?

                      • 8. Re: Button to toggle through layers
                        jay fresno Level 1

                        I tried the script, that Dave suggested, with 4 layers and it toggles through the visibility of the bottom 3 layers. I tried adding a 5th layer, and it toggles through the visibility of bottom 4 layers.

                         

                        Is the fix for this to add an extra 'dummy' layer so that all the layers that all the necessary layers will be shown -- or does someone know a more elegant solution that won't make the file unnecessarily large?

                        • 9. Re: Button to toggle through layers
                          Dave Merchant MVP & Adobe Community Professional

                          Just change this line

                           

                          if (ocgCounter==ocgNum) ocgCounter = 0;

                           

                          to this:

                           

                          if (ocgCounter>ocgNum) ocgCounter = 0;

                           

                          The first version I posted accounted for the null layer in your file.

                          • 10. Re: Button to toggle through layers
                            jay fresno Level 1

                            Dave,

                             

                            Thank you! This works perfectly to toggle through all layers.

                             

                            However, it's a good suggestion to not let the user manually adjust the layers themselves. To use doc.setOCGOrder(), where would I put this code -- in a layer, a document JavaScript?

                            • 11. Re: Button to toggle through layers
                              James A L. Level 1

                              I would like to do something similar, but I think even more simple. I would like the visibility of just one specific layer to be toggled on and off every time someone clicks a button. How would I do that? And is that something I would have to do in Acrobat X, or could I do that in InDesign CS5.5? Thanks.