turn multiple layers off and turn one layer on

Feb 01, 2018

Copy link to clipboard

Copied

I am working on a pdf file and have gotten to work with hover states (mouse enter, mouse out) and mouse up/down

The one issue I'm running into is when I keep the state on (showing a layer) on click and making sure if I have any other layer I have previously selected is turned off.

I did do this by going through each layer and turning it off on mouse up, but I have a lot of layers and that can be a lot to do for each button.  Is there an easier way?

I tried using on mouse up

var docOCGs = this.getOCGs();

for (var x=0; x < docOCGs.length; x++)

{

    docOCGs.state = false;

}

---- and then another action

var docOCGs = this.getOCGs();

for (var x=0; x < docOCGs.length; x++)

{

    if(docOCGs.name == "DBC: National")

    {

        docOCGs.state = !docOCGs.state;

    }

}

but it turned off all layers except DBC Natoinal.  and there are some layers that I still want to show.  I tried locking them, but they still become invisible.  I even check "Always Visible" on the properties.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

263

Likes

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

turn multiple layers off and turn one layer on

Feb 01, 2018

Copy link to clipboard

Copied

I am working on a pdf file and have gotten to work with hover states (mouse enter, mouse out) and mouse up/down

The one issue I'm running into is when I keep the state on (showing a layer) on click and making sure if I have any other layer I have previously selected is turned off.

I did do this by going through each layer and turning it off on mouse up, but I have a lot of layers and that can be a lot to do for each button.  Is there an easier way?

I tried using on mouse up

var docOCGs = this.getOCGs();

for (var x=0; x < docOCGs.length; x++)

{

    docOCGs.state = false;

}

---- and then another action

var docOCGs = this.getOCGs();

for (var x=0; x < docOCGs.length; x++)

{

    if(docOCGs.name == "DBC: National")

    {

        docOCGs.state = !docOCGs.state;

    }

}

but it turned off all layers except DBC Natoinal.  and there are some layers that I still want to show.  I tried locking them, but they still become invisible.  I even check "Always Visible" on the properties.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

264

Likes

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
Feb 01, 2018 0
Feb 01, 2018

Copy link to clipboard

Copied

The "state" setting for an OCG layer is the final word, until Acrobat reprocesses the conditions (usually on a page change), then it changes. This is tricky stuff.

There is a way to group OCGs to make them all go at once. But even with grouping your loops are the correct and only way to control OCGs with JS. If you want some layers to remain visible, then you need to exclude them in the loop.  The best way to do this is to prefix the names so the script can easily sort out the ones it needs to change and the ones it doesn't.

There are alternative ways to control OCGs. If you look them up in the PDF spec you'll see there are all kinds of visibility triggers, zoom level, language, etc. there is even a way to associate a script with a layer. None of this is controllable from the Acrobat UI.  So I wrote a plug-in that provides this access. If you join the site, you can download it from pdfscripting.com

Likes

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
Reply
Loading...
Feb 01, 2018 1
Feb 01, 2018

Copy link to clipboard

Copied

so the only way to make sure I don't have one layer open after clicking another is to turn them all off in separate actions?

Can I use the same script, turn off all the layers I want (including the one for that button) and then add another action to turn that layer on but put it below it? 

I was wondering how to turn off the layers I want on one script.  right now I do them individually like this

var docOCGs = this.getOCGs();

for (var x=0; x < docOCGs.length; x++)

{

    docOCGs.state = false;

}

Likes

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
Reply
Loading...
Feb 01, 2018 1
Feb 01, 2018

Copy link to clipboard

Copied

Have you read this article?

https://acrobatusers.com/tutorials/create_use_layers

You don't need separate loops. You can set all the states in just one loop that uses conditionals to set them appropriately.  One of the problems though, as explained in the article, is that often JS is fighting the OCG handler, which is also evaluating conditions to set visibility. So you need to make sure the OCG parameters are set appropriately, and something else that can stabilize the state is to set the initial state value. "OCG.initState".  Set this to the same value as the state.

Likes

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
Reply
Loading...
Feb 01, 2018 1
Feb 01, 2018

Copy link to clipboard

Copied

yes, I looked at that.  I think I'm close.  I'm more of a dabbler when it comes to coding so I'm not sure I have all the syntax correct.  if I want to turn off all of the layers before I turn on the one I select, how do I do that loop.  and does it help if layers start with the same title such as "DBC:"

and if I turn them off, then make another action to turn the one I want on after that.  Could that work?

Likes

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
Reply
Loading...
Feb 01, 2018 0
Feb 01, 2018

Copy link to clipboard

Copied

The code can turn the layers on and off in any way you want. I thought that the issue was that you wanted some to not turn off? In this case you need to filter for those layers in the loop. I suggesting the name prefix because it's easy to recognize in the loop.

For example:

var docOCGs = this.getOCGs();

for (var x=0; x < docOCGs.length; x++)

{

    if(docOCGs.name.substr(0,4) != "Grp1")

          docOCGs.state = false;

}

In this case all OCGs names that are prefixed with "Grp1" are not affected by the loop that turns off everything else.

Likes

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
Reply
Loading...
Feb 01, 2018 0
Feb 01, 2018

Copy link to clipboard

Copied

I think that works.  I put this script first

var docOCGs = this.getOCGs(); 

for (var x=0; x < docOCGs.length; x++) 

    if(docOCGs.name.substr(0,4) == "DBC:") 

          docOCGs.state = false; 

and put it first and it turns off everything with that prefix

but then I put

var docOCGs = this.getOCGs();

for (var x=0; x < docOCGs.length; x++)

{

    if(docOCGs.name == "DBC: National")

    {

        docOCGs.state = !docOCGs.state;

    }

}

to toggle that layer.

Likes

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
Reply
Loading...
Feb 01, 2018 0