• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Sep 25, 2018 Sep 25, 2018

Copy link to clipboard

Copied

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

TOPICS
PDF forms

Views

4.1K

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

Community Expert , Sep 26, 2018 Sep 26, 2018

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 th

...

Votes

Translate

Translate
Community Expert ,
Sep 25, 2018 Sep 25, 2018

Copy link to clipboard

Copied

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.

Votes

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
Community Beginner ,
Sep 26, 2018 Sep 26, 2018

Copy link to clipboard

Copied

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?

Votes

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
Community Expert ,
Sep 26, 2018 Sep 26, 2018

Copy link to clipboard

Copied

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;

        }

    }

}

Votes

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
Community Beginner ,
Sep 26, 2018 Sep 26, 2018

Copy link to clipboard

Copied

OK, so in order to use this solution I would need the button associated to the layer to change the value of the Layer1State field and have the doc reference the Layer1State field on load for the value to determine if the layer is visible or not?

The general scope of the project is to have a PDF map of our facilities where the PDF user can click on an area of the map to highlight it and email the saved PDF to maintenance for a service call. Am I going about this the wrong way?

Here is the current javascript on one of the buttons, activated on Mouse Down, for reference:

var layerName = "Col1 row1";

var layers = this.getOCGs();

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

for (var i in layers) {

if (layers.name==layerName) {

layers.state = !layers.state;

break;

}

}

}

Votes

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
Community Expert ,
Sep 26, 2018 Sep 26, 2018

Copy link to clipboard

Copied

You can use the button to change the field's value, but you don't have to, as the Will Save event is enough. If the user doesn't save the file after making the changes then it doesn't matter anyway... So the code I provided should be sufficient.

Votes

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
Community Beginner ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

OK, I'm having trouble implementing this solution. I have 54 layers to check the visibility on when the doc loads. I've tried adjusting the initial doc-level script but I'm running into an error. I'm trying to concatenate the layer's name with State to get the name of the hidden text field connected to each layer on line 4. I'm sure I've made a grammatical error here but I could really use some help finding it.

1   var ocgArray = this.getOCGs(); 

2   if (ocgArray!=null) { 

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

4   ocgArray.state=this.getField(ocgArray.name + "State").value; 

5   break; 

6   } 

7   } 

TypeError: this.getField(...) is null

4:Console:Exec

undefined

Votes

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
Community Expert ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

Check the names of the fields:

 

console.println("name: " + ocgArray[i].name + "State");

 

Why did you add the break?

Votes

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
Community Beginner ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

The break was in the original answer provided in the 3rd reply.

Running this code I get an "undefined" result.

var ocgArray = this.getOCGs(); 

if (ocgArray!=null) { 

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

console.show("name: " + ocgArray.name + "State");

  } 

undefined

Votes

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
Community Expert ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

The break is correct for the other code.

Did you select the whole code before you execute it?

Votes

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
Community Beginner ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

Yes. I'm using the Debugger tool, selecting the entire set of code, and using Ctrl+Enter to run it.

Votes

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
Community Expert ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

Did you open a document with layers?

Votes

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
Community Beginner ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

Yes, 55 layers

Votes

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
Community Expert ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

It's console.println(), not console.show()...

Votes

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
Community Beginner ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

That worked!

All of the hidden text filed names are as I excepted them.

Votes

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
Community Beginner ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

Any other ideas on my code problem, other then removing the break?

Votes

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
Community Expert ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

Did you get already the error?

Votes

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
Community Expert ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

Remove the break command and use the output in the console to figure out which field name is causing the error...

Votes

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
Community Beginner ,
Oct 04, 2018 Oct 04, 2018

Copy link to clipboard

Copied

I worked out the issue. Thank you for all of the help!

Votes

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
Community Expert ,
Oct 03, 2018 Oct 03, 2018

Copy link to clipboard

Copied

Correct, my error.

Votes

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
Explorer ,
Aug 11, 2022 Aug 11, 2022

Copy link to clipboard

Copied

Hello Try67,

 

I have a single-page document with seven layers, and six layers are hidden until a specific answer is selected for each layer (three combo boxes, three Yes/No radio buttons.) I already have the scripts that trigger the hidden layer to show for each question. I want the user to be able to save a copy of the document to PDF so that when they open it, the triggered responses will still be showing. How can I apply the script you show to each layer by retrieving their current state and storing it in the hidden fields (Layer2State, Layer3State, etc.)?

 

Votes

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
Community Expert ,
Aug 11, 2022 Aug 11, 2022

Copy link to clipboard

Copied

If you have multiple layers I would suggest using a single text field for all of them. It can be a multiline field with the name (and state) of each layer on a separate line. You write to it when the file is saved (using the Will Save event of the document) and read from it when the file is loaded, and set the layers accordingly.

Votes

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
Explorer ,
Aug 12, 2022 Aug 12, 2022

Copy link to clipboard

Copied

Hello Try67,

I may be misunderstanding but isn't the text field name to be the name of the state (Layer1State) and the field is just to store the value (1 or 0)? Your reply seems to indicate that I would use one multiline text field with the name of each layer and layer state on separate lines. Example: Layer1 = Layer1State, Layer2 = Layer2State, etc. And by looping the code by the number of layers, it would check the current state of the layer and store it there? 

Votes

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
Community Expert ,
Aug 12, 2022 Aug 12, 2022

Copy link to clipboard

Copied

Yes, that's correct. The script will be a bit more complicated, though.

Votes

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
Explorer ,
Aug 15, 2022 Aug 15, 2022

Copy link to clipboard

Copied

I tested the script below on a PDF where I have a hidden field called "Layer2State" with the value set to "1". The script is in a Will Save action. I have triggered Layer2 to show. I saved the file, closed it, then reopened it, and it appeared to work the first time. I reset it and tried it again, but it did not work the second time. I've checked the debugger, but there are no errors. What could I be missing? I have attached the PDF file.
Note: If I can get this to work for one layer, I intend to set up the additional five layers as "else if" following the first "if" statement. I am still a greenhorn but doing all that I can make this work. I appreciate your help.


var ocgArray = this.getOCGs();
if (ocgArray!=null) {
for (var i=0; i<ocgArray.length; i++) {
if (ocgArray.name=="Layer2") {
this.getField("Layer2State").value = (ocgArray.state) ? "1" : "0";
break;
}
}
}


Votes

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