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
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
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?
Copy link to clipboard
Copied
Take a look here for some inspiration: Turn layer on/off AND save layer state (Edit PDF)
Copy link to clipboard
Copied
Much appreciated. Thanks for your help Karl.
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
Copy link to clipboard
Copied
Where does you save and restore the visibility of the layers?
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.
Copy link to clipboard
Copied
Plus, a field called "LayerConfig" needs to be created.
Copy link to clipboard
Copied
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!
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.
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
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.
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.
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?
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?
Copy link to clipboard
Copied
Did you create the form field that is used to store the layer configuration?
Copy link to clipboard
Copied
I created a 'dropdown list' field and named it 'LayerConfig'.
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?
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.
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);
Copy link to clipboard
Copied
Thank you in advance!
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?
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
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!