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

Save layer state on close

Engaged ,
Feb 21, 2017 Feb 21, 2017

Hi

I have a relatively complicated form that is using buttons to show/hide multiple layers.

Everything is working well, but I am trying to find a way for the layer visibility state to be saved when the file is saved, so that when the PDF is next opened the layers are still in the same state.

Is this possible?

TOPICS
Acrobat SDK and JavaScript
7.3K
Translate
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 , Feb 21, 2017 Feb 21, 2017

You will have to save the layer state in e.g. a hidden form field and then restore it again from that hidden field the next time the document is opened. There is nothing built into Acrobat that would do this for you.

Translate
Community Expert ,
Dec 17, 2018 Dec 17, 2018

Glad that it worked out. It's always a good idea to start from scratch with a script that does not want to behave correctly. By making changes to something again and again, you may have modified something that is not obvious to spot.

Translate
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 ,
Mar 19, 2019 Mar 19, 2019

Hi,

When I save the document using this script it turns on all layers when I reopen?

Do I need to put layer names in the hidden drop down?

Thanks

Translate
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 ,
Mar 19, 2019 Mar 19, 2019

The state of the layers is not saved when you save the file. They will revert to their Init State when you open it.

The solution is either to change the initState property (note, that's not possible in Reader), or to save the states to something like a text field and then use a doc-level script to read the contents of that field and show/hide the layers based on it each time the file is opened.

Translate
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 ,
Mar 19, 2019 Mar 19, 2019

Hi,

That's what I've done. I've added the doc level script and a hidden drop down but as you can see from the pdf form attached, the layer states are not saving.

Any ideas?

WeTransfer

Thanks for your help

Translate
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 ,
Mar 19, 2019 Mar 19, 2019

Where does you show/hide layers?

Translate
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 ,
Mar 19, 2019 Mar 19, 2019

The way you did it doesn't make sense to me. It seems you're just setting all the layers that appear under LayerConfig as visible.

As that what you intended to do?

Translate
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 ,
Mar 19, 2019 Mar 19, 2019

Hi, I just followed the link here Turn layer on/off AND save layer state (Edit PDF)

I just want to retain the layer states when saving.

I'm still fairly new to Javascript so I apologies if I'm not making myself clear.

Translate
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 ,
Mar 19, 2019 Mar 19, 2019

Where does you change the layer states?

Translate
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 ,
Mar 19, 2019 Mar 19, 2019

Screenshot 2019-03-19 at 12.09.38.png

There are various buttons on the form that change layer states. Here are the ones on Page1 highlighted in GREEN

Translate
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 ,
Mar 19, 2019 Mar 19, 2019

function restoreLayerConfig(doc) {

    var activeLayers = [];

    // get the items from the layerConfig dropdown control

    var f = this.getField("LayerConfig");

    if (f != null) {

        for (var i = 0; i < f.numItems; i++) {

            activeLayers.push(f.getItemAt(i));

        }

        var ocgArray = doc.getOCGs();

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

            ocgArray.state = isInArray(ocgArray.name, activeLayers);

            activeLayers.push(ocgArray.name);                                        // <= Remove this line!

        }

    }

}

Translate
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 ,
Mar 19, 2019 Mar 19, 2019

It's ok, I've managed to make it work. Thanks for everyone's help!

Translate
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