Copy link to clipboard
Copied
Does anyone know if its possible to control (turn on/off) layers with java script?
Currently I am using this JS:
var layers = this.getOCGs();
for(var i=0;i<this.layers.length;i++)
{
if(layers.name == "LAYER NAME")
layers.state = !(layers.state);
}
It displays how I want it too, however when I save the PDF and close it. When the PDF is reopened, The layers reset to the original state
Help please
1 Correct answer
As Try67 explained in the other thread, there are issues with manipulating Layers in Reader. So, if you can't change the OCG state so that it maintains its value, you save the desired state a different way and restore it on Document Open. One easy way to do this is with "doc.viewState".
So you need hidden text field to save the Layer state, lets call it "LayerState". Then on document close, or anytime you change Layer visibility, call this code:
this.getField("LayerState").value = eval(viewStat
...Copy link to clipboard
Copied
You'll need to change the layer's "initialView" property to fix that.
However, this property can't be edited in Reader, so you'll need to use a workaround, like a doc-level script that changes the states of all the layers when the file is opened, based on the value of a (hidden) text field, for example.
Copy link to clipboard
Copied
Here's an article on scripting OCG layers
It includes a sample file
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
can you reattach the link.
Copy link to clipboard
Copied
Opps, must have missed that.
Here it is:
https://acrobatusers.com/tutorials/create_use_layers
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Hey Thom,
So I read the article but still a little lost on how to build the code. Sorry, I don't have a strong background in javascripting.
Still looking for a way to reset the layers initial view based on a check boxed in the PDF. >If check box is on when I save > open the file with that layer and check box on.
Copy link to clipboard
Copied
As Try67 explained in the other thread, there are issues with manipulating Layers in Reader. So, if you can't change the OCG state so that it maintains its value, you save the desired state a different way and restore it on Document Open. One easy way to do this is with "doc.viewState".
So you need hidden text field to save the Layer state, lets call it "LayerState". Then on document close, or anytime you change Layer visibility, call this code:
this.getField("LayerState").value = eval(viewState.toSource()).ocgStates.toSource();
Next, add this code to a Document Script:
viewState = {ocgStates:eval(this.getField("LayerState").value)};
This is not as good as setting the initialState for the layer, but it works for Acrobat/Reader. Probably won't work for any other viewer.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thank you so Much!!! this worked perfectly
Copy link to clipboard
Copied
Hi Thom.
I am doing something similar and hoping you mind elaborate a little more for me. I am quite new to Javascript. I have read the link above. I have a base layer, then 3 other layers. Depending on the check box ticked, it will show a layer and its form fields. I also have a a button set up with javascript so that when the form is filled, the full pDF will attach to a new email in Outlook. However, everything works until the recipient opens the email. They have to click on the checkbox again for the layer to appear. I am using Adobe Pro. My layers were created in InDesign.
1. What script goes with each checkbox? Right now I used an action, Mouse Up, set layer visibility. But as mentioned, it doesn't maintain upon reopen.
2. Does an additional script need to go with my button re: visibility of layer?
As mentioned, I am quite new to JS. This is a work project so would love to have it operate as needed 🙂
Copy link to clipboard
Copied
I think my previous post explains saving and restoring layer visibility pretty well. The general idea is to save the current layer state to a form field any time the layer state changes. Then restore the layer state from the form field when the document is opened. The Acrobat scripting model includes a document property called the "viewState", which includes an array of layer states. To save the current layer state this property is converted to JSON (with the ".toSource()" function). This code should bge placed in the mouseUp action for each checkbox.
Then to restore the state the saved JSON text is converted back and reapplied to the document viewstate in a document script. The code for doing this is provided in the previous post.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
I have 3 radio buttons so I am not sure if this is possible. If they were a regular text field, I would input
viewState = {ocgStates:eval(this.getField("LayerState").value)}; ? Obviously LayerState is the name of the field.
Then set an action to call the code
this.getField("LayerState").value = eval(viewState.toSource()).ocgStates.toSource(); ?
Apologies. Not trying to be simple and I appreciate your additional explanation.
Copy link to clipboard
Copied
The Code to save the layer state goes on the MouseUp action on each of the radio buttons
This code
this.getField("LayerState").value = this.viewState.toSource();
Then, the code to restore the layerstate goes into a document script, which is called when the document is opened.
this.viewState = {ocgStates:eval(this.getField("LayerState").value)};
Here's an article on document scripts:
https://www.pdfscripting.com/public/Document-Level-Scripts.cfm
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
One very important note. This scheme only works when the PDF is viewed in Acrobat Pro or Reader. It won't work on mobile devices, browsers, or most any other PDF viewer.
Use the Acrobat JavaScript Reference early and often

