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?
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Thanks for your help
Copy link to clipboard
Copied
Where does you show/hide layers?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Where does you change the layer states?
Copy link to clipboard
Copied

There are various buttons on the form that change layer states. Here are the ones on Page1 highlighted in GREEN
Copy link to clipboard
Copied
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!
}
}
}
Copy link to clipboard
Copied
It's ok, I've managed to make it work. Thanks for everyone's help!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now