These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Lock a layer automatically

Bonurbe
Registered: Mar 3 2008
Posts: 10
Answered

Hi,
 
I'm wondering if there would be a way to lock a layer automatically (with a custom button for example)?
 
Usually, I need to select the layer, right-click on "properties" and click on "Locked" in the Default State line. It's a bit long since I'm doing that repetitively.
 
Thanks,
Bruno

My Product Information:
Acrobat Pro 10.1, Macintosh
UVSAR
Expert
Registered: Oct 29 2008
Posts: 1357
Accepted Answer
In Acrobat you can set the .locked property of the OCG (but not in Reader).

For example here's a script that finds a layer called "skyline" and locks it - you could run the script from a menu item, but you'll need to pass it a reference to the current document in the cDoc variable:

var ocgArray = cDoc.getOCGs();
for (var i=0; i < ocgArray.length; i++) {
if (ocgArray[i].name == "skyline") ocgArray[i].locked = true;
}

To run it on the debug console, change "cDoc" to "this". Read the Acrobat SDK documentation if you need to know about adding menu items or tool buttons.
Bonurbe
Registered: Mar 3 2008
Posts: 10
Thanks UVSAR. I've just checked and nothing happened. I've replaced "cDoc" with "this" but no success. I've tried to add "pageNum" after "this.getOCGs(pageNum);" but no success.

Do you know what could be wrong? Is it working on a yours with a layered PDF?

Thanks,
Bruno
UVSAR
Expert
Registered: Oct 29 2008
Posts: 1357
Yes, it works for me in the Acrobat X Pro JS console, using "this.getOCGs()".

OCG names are case-sensitive and you can't refer to the "group" parents if there are any, only the OCGs themselves.

Check your layer names by adding the line

console.println(ocgArray[i].name);

above the "if..." line, so the names appear on the console.
Bonurbe
Registered: Mar 3 2008
Posts: 10
Actually, I have Acrobat Pro 9 not X, so it might the problem.

Otherwise, here's the message I receive everytime:

SyntaxError: syntax error
1:Console:Exec
undefined
UVSAR
Expert
Registered: Oct 29 2008
Posts: 1357
That means you've entered it wrong, or you're not selecting the entire script and running it as one operation - in the console you must highlight all four lines and press CMD+ENTER, or it'll only execute the current line.

We have a console tutorial here.
Bonurbe
Registered: Mar 3 2008
Posts: 10
OK, got it to work thanks. I didn't know indeed I had to select all four lines in order to execute the script.

Thanks for your help and patience.
UVSAR
Expert
Registered: Oct 29 2008
Posts: 1357
No problem - happy to help!
AgustinRobles
Registered: Oct 30 2011
Posts: 1
Hello:

I am new to pdf and java scripting.
I am looking for something simmilar: I have a PDF with a layer called "Logo" for the user to decide whether it displays its contents (a logo) or not using a button and then being able to distribute it locked, for the recipient not being able to make that layer visible again.

I tried your script, but I still can make layers visible or invisible and when accessing to that layer's properties the "Locked" checkbox is unchecked both on Adobe Acrobat Pro and Reader.

Thank you in advance.

Greetings from Mexico.