Skip to main content
reidzilla
Known Participant
September 25, 2018
Answered

Can you change the default state of a layer with javascript from a button?

  • September 25, 2018
  • 2 replies
  • 8231 views

I am looking to change the layer's set Default state from "off" to "on" so when saved it will remain "on".

This topic has been closed for replies.
Correct answer try67

Sure, let's take a simple example. You have a layer called "Layer1" and you want to set its default state using a field.

Create a (hidden) text field called "Layer1State" and add the following code as a doc-level script:

var ocgArray = this.getOCGs();

if (ocgArray!=null) {

    for (var i=0; i<ocgArray.length; i++)  {

        if (ocgArray.name=="Layer1") {

            ocgArray.state=this.getField("Layer1State").valueAsString=="1";

            break;

        }

    }

}

Now you just need to set the value of the field as "1" if you want the layer to be visible when the file is opened, or to anything else if you want it to be hidden.

For example, if you want to save the current state of the layer when the file is saved, you can place this code under the document's Will Save event:

var ocgArray = this.getOCGs();

if (ocgArray!=null) {

    for (var i=0; i<ocgArray.length; i++)  {

        if (ocgArray.name=="Layer1") {

            this.getField("Layer1State").value = (ocgArray.state) ? "1" : "0";

            break;

        }

    }

}

2 replies

overnau22418802i2xn
Participating Frequently
January 13, 2022
  • Dear, Reidzilla!
  • I'm trying to solve absulutely the same qust as described in your question. Doing the same as described in the topic and it doesn't work 😞
  • I have a layer with name "0_Status" and the radio button with name "3_YES_2.1"
  • Initially layer has staus OFF (by default).
  • When I press radio button, the layer becomes visible. After press Save, This layer should change default layer from OFF to ON. But it not working 😞
  • Using the following script (put it at the end of the script line of the button) 
  • var ocgArray1 = this.getOCGs();
  • if (ocgArray!=null) {
  • for (var i=0; i<ocgArray.length; i++) {
  • if (ocgArray1.name=="0_Status")
  • {this.getField("3_YES_2.1").value = (occArray.state) ? "1" : "0"; }}}

Please help me to find the solution, appretiate in advance for assistance of the Best community! 

Bernd Alheit
Community Expert
January 13, 2022

Does you use ocgArray1 or ocgArray ?

overnau22418802i2xn
Participating Frequently
January 13, 2022
 

Does you use ocgArray1 or ocgArray ?


By @Bernd Alheit

In the script I see, that ocgArray1

try67
Community Expert
September 25, 2018

It can be done with a script, but it won't work if the file is used in Reader.

A better option is to keep the desired state of the layer in something like a hidden text field and then use a script to apply it when the file is opened.

reidzilla
reidzillaAuthor
Known Participant
September 26, 2018

OK, could you expound on "keep the desired state of the layer in something like a hidden text field and then use a script to apply it when the file is opened", please?

Known Participant
August 16, 2022

If the script is in the WillSave event, then it is only triggered when the document will save.  Which will only happen if the PDF has changed.  I would suggest putting "console.println" statements in the code so you know when it has been run, and how it is working. 

 

you'll find a tutorial on the console window that shows this technique (late in the video) here. 

https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm

 

 

 


Hello Thom,

When you say, "which will only happen if the PDF has changed," does that change include revealing the layers? The PDF will have had to change for it to show the hidden layers, like specific selections from a combo box or radio buttons. And, if the script needs to be moved from a document action Will Save to document level, what would trigger the script to store the layer states if changed? Thank you for your help.