Saving layer states in a document with radio buttons and lots of layers

Oct 26, 2017

Copy link to clipboard

Copied

Hello,

I am a visual designer with very limited js skills but am wondering if anyone could help me with an interactive PDF I've made in which I want to save whichever layer is open. The layers are set to display as radio buttons are clicked and there are 60 different layers. (I know.)

I found one piece of code just googling on line, which appears to be saving which radio button was selected but not displaying the corresponding layer.

function SetLayer() {     // get the radio button status     var layerName = this.getField("Layer").value;      this.getField("LayerName").value = layerName;      var ocgArray = this.getOCGs();      for (var i=0; i < ocgArray.length; i++)      {         if (layerName == ocgArray.name)               ocgArray.state = true;         else             ocgArray.state = false;     } }  if (this.getField("LayerName") != "") {     SetLayer(); } else {     getActiveLayer(); }  function getActiveLayer() {     for (var i=0; i < ocgArray.length; i++)      {         if (ocgArray.state == true) {             this.getField("LayerName").value = ocgArray.name;             this.getField("Layer").value = ocgArray.name;         }     } } 

Does anyone know what I'm missing or how I could have it save the layer view as well? Here is my file:

https://learning.optum.com/content/ss/4583079e9dec4bd8ad0465ca060ff7cd/LearningDev/anneke/FitnessMap...

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

The script uses two fields that are not on the form.  However, this script only allows one layer to be visible at a time, and your setup is more complex.

My first thought is to use the saved radio button state. Basically to re activate the selected button to cause it's action to run when the document is opened. But this won't work because the Layer State is set on a MouseUp, which can't be forced through JS

The other thing to do is use the "Will Save" event to set the "true" Layer states to "initState", which is the initial value of the layer when the PDF is opened.

Add this code to the "Will Save" "Document Action"

var aOCGs = this.getOCGs();

for(var i=0;i<aOCGs.length;i++)

   aOCGs.initState = aOCGs.state;

You'll find the document actions in this tool

TOPICS
Acrobat SDK and JavaScript

Views

227

Likes

Translate

Translate

Report

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

Saving layer states in a document with radio buttons and lots of layers

Oct 26, 2017

Copy link to clipboard

Copied

Hello,

I am a visual designer with very limited js skills but am wondering if anyone could help me with an interactive PDF I've made in which I want to save whichever layer is open. The layers are set to display as radio buttons are clicked and there are 60 different layers. (I know.)

I found one piece of code just googling on line, which appears to be saving which radio button was selected but not displaying the corresponding layer.

function SetLayer() {     // get the radio button status     var layerName = this.getField("Layer").value;      this.getField("LayerName").value = layerName;      var ocgArray = this.getOCGs();      for (var i=0; i < ocgArray.length; i++)      {         if (layerName == ocgArray.name)               ocgArray.state = true;         else             ocgArray.state = false;     } }  if (this.getField("LayerName") != "") {     SetLayer(); } else {     getActiveLayer(); }  function getActiveLayer() {     for (var i=0; i < ocgArray.length; i++)      {         if (ocgArray.state == true) {             this.getField("LayerName").value = ocgArray.name;             this.getField("Layer").value = ocgArray.name;         }     } } 

Does anyone know what I'm missing or how I could have it save the layer view as well? Here is my file:

https://learning.optum.com/content/ss/4583079e9dec4bd8ad0465ca060ff7cd/LearningDev/anneke/FitnessMap...

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

The script uses two fields that are not on the form.  However, this script only allows one layer to be visible at a time, and your setup is more complex.

My first thought is to use the saved radio button state. Basically to re activate the selected button to cause it's action to run when the document is opened. But this won't work because the Layer State is set on a MouseUp, which can't be forced through JS

The other thing to do is use the "Will Save" event to set the "true" Layer states to "initState", which is the initial value of the layer when the PDF is opened.

Add this code to the "Will Save" "Document Action"

var aOCGs = this.getOCGs();

for(var i=0;i<aOCGs.length;i++)

   aOCGs.initState = aOCGs.state;

You'll find the document actions in this tool

TOPICS
Acrobat SDK and JavaScript

Views

228

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 26, 2017 0
Adobe Community Professional ,
Dec 22, 2017

Copy link to clipboard

Copied

The script uses two fields that are not on the form.  However, this script only allows one layer to be visible at a time, and your setup is more complex.

My first thought is to use the saved radio button state. Basically to re activate the selected button to cause it's action to run when the document is opened. But this won't work because the Layer State is set on a MouseUp, which can't be forced through JS

The other thing to do is use the "Will Save" event to set the "true" Layer states to "initState", which is the initial value of the layer when the PDF is opened.

Add this code to the "Will Save" "Document Action"

var aOCGs = this.getOCGs();

for(var i=0;i<aOCGs.length;i++)

   aOCGs.initState = aOCGs.state;

You'll find the document actions in this tool

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 22, 2017 0