This is a bit involved...
Create a new drop down control named LayerConfig in your document and set it to be hidden and read-only. We are only using this to store our layer configuration, the user will not interact with it.
Then create a document level JavaScript and name it e.g. LayerConfig using the following script:
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[i].state == true) {
activeLayers.push(ocgArray[i].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[i] == 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[i].state = isInArray(ocgArray[i].name, activeLayers);
activeLayers.push(ocgArray[i].name);
}
}
}
restoreLayerConfig(this);
This defines the functions we are using and calls the restoreLayerConfig() function when the document gets loaded. Now create a "WillSave" document action using this one line:
saveLayerConfig(this);
This should do the trick.
Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com