Turn layer on/off AND save layer state

I need to include parentheses in PDF file for legal review, then delete them after legal approval. Info inside the parentheses is constant, so parentheses are always in same position. I have parentheses on separate layer in InDesign and exported to PDF with layers. Now, clients need to determine layer state and save the file with that layer state. I'm unable to get that last part — saving the PDF file with the preferred layer state. Thanks for your help.


Brad Gifford


2 Answers

Do your clients use Adobe Acrobat or the free Adobe Reader? With Acrobat, they should be able to change the layer state on the Layers navigation pane on the left side.

Adobe Reader cannot do that. For Reader, you would need to have a hidden form field that stores layer configuration data and restores the layer configuration from that field when the file is opened. Do you have any background in JavaScript programming for Acrobat?

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   

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


Karl Heinz Kremer   


Please specify a reason: