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

Save layer state on close

Engaged ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

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

Views

4.6K

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 , 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.

Votes

Translate

Translate
Community Expert ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

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.

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
Engaged ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

Thanks Karl

I suspected that would be the case, but I'm not sure where to look for an example of how to script this.

Are you able to point me to any examples?

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 ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

Take a look here for some inspiration: Turn layer on/off AND save layer state (Edit PDF)

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
Engaged ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

Much appreciated. Thanks for your help Karl.

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
New Here ,
Oct 17, 2022 Oct 17, 2022

Copy link to clipboard

Copied

Hi Karl, I've followed the instructions you provided previously and for a newbie like me, they have been a massive help. However, I think I've gone awry with my code on my dropdown that dictates which layer is visible. I have two layers, layer.SGCDN should only be visible if SGCDN is selected in the dropdown, else Layer.Normal should be visible. It works fine (probably by fluke) until I save the file with SGCDN selected - when it's reopened both layers are visible. Could you give me a hint on where I've gone wrong?

 

The validation code on my dropdown is:

var ocgArray = this.getOCGs();
for (var i=0; i < ocgArray.length; i++) {
if (ocgArray[i].name=="Layer.SGCDN") ocgArray[i].state = (event.value=="SGCDN");
else if (ocgArray[i].name=="Layer.Normal") ocgArray[i].state = (event.value!="SGCDN");
}

 Thanks in advance

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 17, 2022 Oct 17, 2022

Copy link to clipboard

Copied

Where does you save and restore the visibility of the 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 17, 2022 Oct 17, 2022

Copy link to clipboard

Copied

There also has to be a document level script that resets the layer states when the PDF is opened. 

This script is basically the same as the validation script in the dropdown, but  event.value is replace by the value of the dropdown. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 17, 2022 Oct 17, 2022

Copy link to clipboard

Copied

Plus, a field called "LayerConfig" needs to be created.

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
New Here ,
Oct 27, 2022 Oct 27, 2022

Copy link to clipboard

Copied

LATEST

Hi All, Actually I realised my mistake. I had done all of the above as per the instructions, but forgot the most basic part - the default visibility settings of the layers themselves *doh* Thanks for your swift replies!

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 ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

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.

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
Engaged ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

Thanks for all your help guys, but I think I've bitten off more than I can chew here. I don't really have enough knowledge of javascript to understand how Karl's code works, or to troubleshoot it with my PDF.

I've tried using the script you pointed me to Karl and while it seems to gather the layer states information in the drop down field, it doesn't keep the layer states on reopening.

I may have to try a cruder method, without layers, and just rely on using show/hide for all the content. It's quite messy and labour-intensive, but I should be able to get it to work within my abilities.

I do appreciate you trying to help me.

Thanks

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 ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

If it's not restoring the layer state, chances are you are not calling the function to restore the state when the document is opened. This is done via the one line in a document level script.

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
Engaged ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

I have put the one line

saveLayerConfig(this);

into a 'Document Actions' 'Document Will Save' script. The main LayerConfig script is set as a 'Document Javascript'.

This is new territory for me so I could well be getting this completely wrong still.

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 ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

The document level script needs to call restoreLayerConfig(this) - is that happening? Are you getting any errors in the JavaScript console?

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
Engaged ,
Feb 21, 2017 Feb 21, 2017

Copy link to clipboard

Copied

No, I am not getting any errors in the Javascript debugger console.

I have pasted your script directly into a document level javascript (from the link you gave me earlier). This script included restoreLayerConfig(this) as the last line of the script?

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 ,
Feb 22, 2017 Feb 22, 2017

Copy link to clipboard

Copied

Did you create the form field that is used to store the layer configuration?

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
Engaged ,
Feb 22, 2017 Feb 22, 2017

Copy link to clipboard

Copied

I created a 'dropdown list' field and named it 'LayerConfig'.

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 ,
Feb 22, 2017 Feb 22, 2017

Copy link to clipboard

Copied

One potential problem is that a change in the layer visibility is not something that marks the document as "dirty", which means that Acrobat does not prompt to save it. And, when you try to save, it may not actually perform the save. Are you sure that the file gets saved? You may want to try a "Save As". You can also unhide the LayerConfig field to see how it updates when you save the file. If it's still not working, would you be able to share your file?

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
Engaged ,
Feb 22, 2017 Feb 22, 2017

Copy link to clipboard

Copied

Thanks for persisting with trying to help me Karl.

I've tried using 'save as', but still with no improvement, so I have shared the file with you.

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 ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Karl hi!

have you solved this problem? I have the same situation, but my Layer Config isn't saving the changes.

I use this code:

function saveLayerConfig(doc) {

    // get the current layer configuration and save the names of all active layers in an array

    var activeLayers = [];

    var ocgArray = doc.getOCGs();

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

        if (ocgArray.state == true) {

            activeLayers.push(ocgArray.name);

        }

    }

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

    if (f != null) {

        f.clearItems();

        f.setItems(activeLayers);

    }

}

function isInArray(val, arr) {

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

        if (arr == val)

            return true;

    }

    return false;

}

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);

        }

    }

}

and then:

saveLayerConfig(this);

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 ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Thank you in advance!

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 ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

Are you getting any errors in the JavaScript console? Do you have a field named "LayerConfig" (which is what your script writes to and reads from)? Can you share the file?

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 ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

I have the LayerConfig file, yes... this is the pdf: https://www.dropbox.com/s/l6fshszo6d8fwb8/KD-Locusta-Zahtjev%20za%20prodaju%20udjela%20u%20otvorenom...

thank you so much, I am trying to solve this for the past 24h

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 ,
Dec 17, 2018 Dec 17, 2018

Copy link to clipboard

Copied

I've made the document from scratch and now it works just fine. Don't understand how, but I am happy!

Thank you for your 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