Copy link to clipboard
Copied
I am looking to change the layer's set Default state from "off" to "on" so when saved it will remain "on".
Copy link to clipboard
Copied
Sure, let's take a simple example. You have a layer called "Layer1" and you want to set its default state using a field.
Create a (hidden) text field called "Layer1State" and add the following code as a doc-level script:
var ocgArray = this.getOCGs();
if (ocgArray!=null) {
for (var i=0; i<ocgArray.length; i++) {
if (ocgArray.name=="Layer1") {
ocgArray.state=this.getField("Layer1State").valueAsString=="1";
break;
}
}
}
Now you just need to set the value of the field as "1" if you want the layer to be visible when the file is opened, or to anything else if you want it to be hidden.
For example, if you want to save the current state of the layer when the file is saved, you can place this code under the document's Will Save event:
var ocgArray = this.getOCGs();
if (ocgArray!=null) {
for (var i=0; i<ocgArray.length; i++) {
if (ocgArray.name=="Layer1") {
this.getField("Layer1State").value = (ocgArray.state) ? "1" : "0";
break;
}
}
}
Copy link to clipboard
Copied
It can be done with a script, but it won't work if the file is used in Reader.
A better option is to keep the desired state of the layer in something like a hidden text field and then use a script to apply it when the file is opened.
Copy link to clipboard
Copied
OK, could you expound on "keep the desired state of the layer in something like a hidden text field and then use a script to apply it when the file is opened", please?
Copy link to clipboard
Copied
Sure, let's take a simple example. You have a layer called "Layer1" and you want to set its default state using a field.
Create a (hidden) text field called "Layer1State" and add the following code as a doc-level script:
var ocgArray = this.getOCGs();
if (ocgArray!=null) {
for (var i=0; i<ocgArray.length; i++) {
if (ocgArray.name=="Layer1") {
ocgArray.state=this.getField("Layer1State").valueAsString=="1";
break;
}
}
}
Now you just need to set the value of the field as "1" if you want the layer to be visible when the file is opened, or to anything else if you want it to be hidden.
For example, if you want to save the current state of the layer when the file is saved, you can place this code under the document's Will Save event:
var ocgArray = this.getOCGs();
if (ocgArray!=null) {
for (var i=0; i<ocgArray.length; i++) {
if (ocgArray.name=="Layer1") {
this.getField("Layer1State").value = (ocgArray.state) ? "1" : "0";
break;
}
}
}
Copy link to clipboard
Copied
OK, so in order to use this solution I would need the button associated to the layer to change the value of the Layer1State field and have the doc reference the Layer1State field on load for the value to determine if the layer is visible or not?
The general scope of the project is to have a PDF map of our facilities where the PDF user can click on an area of the map to highlight it and email the saved PDF to maintenance for a service call. Am I going about this the wrong way?
Here is the current javascript on one of the buttons, activated on Mouse Down, for reference:
var layerName = "Col1 row1";
var layers = this.getOCGs();
if (layers!=null && layers.length!=0) {
for (var i in layers) {
if (layers.name==layerName) {
layers.state = !layers.state;
break;
}
}
}
Copy link to clipboard
Copied
You can use the button to change the field's value, but you don't have to, as the Will Save event is enough. If the user doesn't save the file after making the changes then it doesn't matter anyway... So the code I provided should be sufficient.
Copy link to clipboard
Copied
OK, I'm having trouble implementing this solution. I have 54 layers to check the visibility on when the doc loads. I've tried adjusting the initial doc-level script but I'm running into an error. I'm trying to concatenate the layer's name with State to get the name of the hidden text field connected to each layer on line 4. I'm sure I've made a grammatical error here but I could really use some help finding it.
1 var ocgArray = this.getOCGs();
2 if (ocgArray!=null) {
3 for (var i=0; i<ocgArray.length; i++) {
4 ocgArray.state=this.getField(ocgArray.name + "State").value;
5 break;
6 }
7 }
TypeError: this.getField(...) is null
4:Console:Exec
undefined
Copy link to clipboard
Copied
Check the names of the fields:
console.println("name: " + ocgArray[i].name + "State");
Why did you add the break?
Copy link to clipboard
Copied
The break was in the original answer provided in the 3rd reply.
Running this code I get an "undefined" result.
var ocgArray = this.getOCGs();
if (ocgArray!=null) {
for (var i=0; i<ocgArray.length; i++) {
console.show("name: " + ocgArray.name + "State");
}
}
undefined
Copy link to clipboard
Copied
The break is correct for the other code.
Did you select the whole code before you execute it?
Copy link to clipboard
Copied
Yes. I'm using the Debugger tool, selecting the entire set of code, and using Ctrl+Enter to run it.
Copy link to clipboard
Copied
Did you open a document with layers?
Copy link to clipboard
Copied
Yes, 55 layers
Copy link to clipboard
Copied
It's console.println(), not console.show()...
Copy link to clipboard
Copied
That worked!
All of the hidden text filed names are as I excepted them.
Copy link to clipboard
Copied
Any other ideas on my code problem, other then removing the break?
Copy link to clipboard
Copied
Did you get already the error?
Copy link to clipboard
Copied
Remove the break command and use the output in the console to figure out which field name is causing the error...
Copy link to clipboard
Copied
I worked out the issue. Thank you for all of the help!
Copy link to clipboard
Copied
Correct, my error.
Copy link to clipboard
Copied
Hello Try67,
I have a single-page document with seven layers, and six layers are hidden until a specific answer is selected for each layer (three combo boxes, three Yes/No radio buttons.) I already have the scripts that trigger the hidden layer to show for each question. I want the user to be able to save a copy of the document to PDF so that when they open it, the triggered responses will still be showing. How can I apply the script you show to each layer by retrieving their current state and storing it in the hidden fields (Layer2State, Layer3State, etc.)?
Copy link to clipboard
Copied
If you have multiple layers I would suggest using a single text field for all of them. It can be a multiline field with the name (and state) of each layer on a separate line. You write to it when the file is saved (using the Will Save event of the document) and read from it when the file is loaded, and set the layers accordingly.
Copy link to clipboard
Copied
Hello Try67,
I may be misunderstanding but isn't the text field name to be the name of the state (Layer1State) and the field is just to store the value (1 or 0)? Your reply seems to indicate that I would use one multiline text field with the name of each layer and layer state on separate lines. Example: Layer1 = Layer1State, Layer2 = Layer2State, etc. And by looping the code by the number of layers, it would check the current state of the layer and store it there?
Copy link to clipboard
Copied
Yes, that's correct. The script will be a bit more complicated, though.
Copy link to clipboard
Copied
I tested the script below on a PDF where I have a hidden field called "Layer2State" with the value set to "1". The script is in a Will Save action. I have triggered Layer2 to show. I saved the file, closed it, then reopened it, and it appeared to work the first time. I reset it and tried it again, but it did not work the second time. I've checked the debugger, but there are no errors. What could I be missing? I have attached the PDF file.
Note: If I can get this to work for one layer, I intend to set up the additional five layers as "else if" following the first "if" statement. I am still a greenhorn but doing all that I can make this work. I appreciate your help.
var ocgArray = this.getOCGs();
if (ocgArray!=null) {
for (var i=0; i<ocgArray.length; i++) {
if (ocgArray.name=="Layer2") {
this.getField("Layer2State").value = (ocgArray.state) ? "1" : "0";
break;
}
}
}


-
- 1
- 2