Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Control Layers with Javascript

Community Beginner ,
Apr 11, 2018 Apr 11, 2018

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

TOPICS
Acrobat SDK and JavaScript
6.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 19, 2018 Apr 19, 2018

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

...
Translate
Community Expert ,
Apr 11, 2018 Apr 11, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 11, 2018 Apr 11, 2018

Here's an article on scripting OCG layers

It includes a sample file

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 12, 2018 Apr 12, 2018

can you reattach the link. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 12, 2018 Apr 12, 2018

Opps, must have missed that.

Here it is:

https://acrobatusers.com/tutorials/create_use_layers

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 19, 2018 Apr 19, 2018

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2018 Apr 19, 2018

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.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 19, 2018 Apr 19, 2018

Thank you so Much!!! this worked perfectly

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 20, 2023 Mar 20, 2023

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 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 20, 2023 Mar 20, 2023

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. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 21, 2023 Mar 21, 2023

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 23, 2023 Mar 23, 2023
LATEST

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

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 20, 2023 Mar 20, 2023

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. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines