Check Boxes That Show and Hide PDF Layers
In my last article I outlined how to show and hide PDF pages with check boxes by spawning hidden template pages. In this article I'll describe how to show and hide PDF layers using check boxes as triggers.Â
Take the Acrobat Pro/JavaScript eLearning Course Acrobat Like a Pro
Challenges
The layer visibility must be set to Visible When On for a script to turn a layer on and off.Â
Layers have a default state, either on or off. When the PDF is opened the layer will revert to the default state.
Because the layer will revert to its default state when the document is opened, changing the state of the layer does not "dirty" the PDF, meaning it is not a change that will cause the save dialog ("Do you want to save changes to xx.pdf before closing?") to activate when the PDF is closed.
Watch
Steps:
1)Â Â Open the cover page.
2)Â Â Open the layers panel in the left navigation panel.
3)Â Â In the Acrobat tools tab type Watermark, then select Watermark > Add.
4)Â Â Select File as the source and browse the hard drive for the Black.pdf file.
5)Â Â Set the Scale relative to target page field to 100% and click OK.
6)Â Â Run the following script in the console:
this.setOCGOrder(this.getOCGs());
Note: Layers are called OCGs (Optional Content Groups)
7)  You should see a layer called Watermark appear in the layers panel. Right-click the layer, and select Properties.
8)Â Â Make the following changes to the layer in the dialog window and click OK.
9)Â Â Repeat steps three through eight for the Blue, Green, and Red files.
10) Press Ctrl + Shift + 7 to enter form editing mode.
11) Create a check box field named Black.
12) In the Actions tab, select the following dropdown items and click Add.
13) Copy and paste the following script and save the changes:
var ocg=this.getOCGs();
for(var i=0;i<ocg.length;i++)
{
if(ocg[i].name==event.target.name && event.target.value=="Off")
{ocg[i].state=false}
if(ocg[i].name==event.target.name && event.target.value!="Off")
{ocg[i].state=true}
}
Naming Tips
If you've read some of my other articles you'll know why I name fields and other objects the way I do. I named the layers the same as the check box fields, so the field name is called to identify the corresponding layer. This allows the same script to be used in different locations with minimum tweaks, or in this case, no tweaks. In other words, I can use the exact same script in all four check boxes and it will work without modification. I do not have to copy and paste the script into every field. I will only enter the script into the first field, and instead copy the field three times, and then rename those three fields to match the corresponding layers. This should make more sense as break down the script line by line:
/* Define the variable ocg as an array of the layers in the document*/
var ocg=this.getOCGs();
/* Create a loop that loops through all layers*/
for(var i=0;i<ocg.length;i++)
{
/* If the OCG name is equal to the field name, AND the field value is equal to "Off" */
if(ocg[i].name==event.target.name && event.target.value=="Off")
/* Set the OCG state to false (off) */
{ocg[i].state=false}
/* If the OCG name is equal to the field name, AND the field value is not equal to "Off" */
if(ocg[i].name==event.target.name && event.target.value!="Off")
/* Set the OCG state to true (on) */
{ocg[i].state=true}
}
14) Copy the check box field and paste it three times.
15) Rename the check box fields Blue, Green, and Red.
16) Test the check boxes to ensure they show and hide the layers.
Set the initial state when the PDF opens
17) Copy and paste the following script as a document level script:
var ocg=this.getOCGs();
for(var i=0;i<ocg.length;i++)
{
ocg[i].state=(this.getField(ocg[i].name).value!="Off")
}
The script above is short and sweet. Because I named the check box fields the same as the layers, there is no need for multiple variables and if statements. One statement inside a loop covers all check boxes and layer settings. I'll break it down:
/* Define the variable ocg as an array of layers in the document */
var ocg=this.getOCGs();
/* Create a loop that loops through all layers*/
for(var i=0;i<ocg.length;i++)
{
/* See below for an explanation of the following line*/
ocg[i].state=(this.getField(ocg[i].name).value!="Off")
}
/*The first part the line above, 'ocg[i].state=', sets the state of the each layer (OCG). The state is set to true of false. The next part of the line in parentheses '(this.getField(ocg[i].name).value!="Off")', returns either true or false. It states that the check box field that matches the OCG name does not equal "Off". If this statement is true, it returns true. If this statement is false, it returns false. False turns the layer on and true turns the layer off.*/
Video of the entire project
Help Me Improve, Grow, and Continue
If you see something that doesn’t make sense here, please let me know in the comments and I’ll clarify it for you. If you have any questions about this post, ask away. Help me keep the queue filled up by requesting ideas for future articles in the comments section, and if you know someone who would benefit from this post, please share it. Thank you and see you next time…