Skip to main content
Inspiring
February 21, 2017
Answered

Save layer state on close

  • February 21, 2017
  • 1 reply
  • 7212 views

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?

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

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.

1 reply

Karl Heinz  Kremer
Community Expert
Karl Heinz KremerCommunity ExpertCorrect answer
Community Expert
February 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.

Joel Geraci
Community Expert
Community Expert
February 22, 2017

Elaborating on Karl's answer...

The initiState property of the OCG object is Read/Write in Acrobat but Read-only in Reader. If you need Reader to store the state of the layer, then storing it in a hidden field is the only viable approach.

If you anticipate that all of your users will have Acrobat or you only need Acrobat users to save, then the initState can be used to change the default state of the layer.

Bernd Alheit
Community Expert
Community Expert
March 19, 2019

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


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!

        }

    }

}