Show/hide layer conditional to checkbox and Text Field
Copy link to clipboard
Copied
Hi, I'm currently using that code to show a layer named PA40. If the box is checked, it will show the layer, if it's not, the layer will close.
var layerName = "PA40";
var layers = this.getOCGs(0&&1);
if (layers!=null && layers.length!=0) {
for (var i in layers) {
if (layers.name==layerName) {
layers.state = !layers.state;
break;
}
}
}
I now wish to use that checkbox to make another thing: if the checkbox PA40 and the Text Field named PIED is 18 or higer, it will show the layer named PA4, and if the Text Field named PIED is lower then 18, it will show the layer PA4L.
I'm a total noob in Javascript.
Thanks a lot
Copy link to clipboard
Copied
First of all, what's this line supposed to be?
var layers = this.getOCGs(0&&1);
If you're trying to get the OCGs from pages 0 and 1 like that, that's not how it's done.
Change it to:
var layers = this.getOCGs(0).concat(this.getOCGs(1));
Copy link to clipboard
Copied
Thanks, I changed it to this below and it still doesn't work.
var layerName = "PA40";
var layers = this.getOCGs(0).concat(this.getOCGs(1));
if (layers!=null && layers.length!=0) {
for (var i in layers) {
if (layers.name==layerName) {
layers.state = !layers.state;
break;
}
}
}
Been able to make it work by writing the code two times, one with ) and one with 1.
For my questions I was wondering if it would be easier to make a textfield appear instead of a layer, since I only have one item on it.
Thanks a lot
Copy link to clipboard
Copied
Yes, fields are much easier to work with than layers.
Copy link to clipboard
Copied
So is it possible to make a field written 123456 if PA40 is checked and PIED is 18 or over?
Copy link to clipboard
Copied
Sure. What should it be otherwise, though?
Copy link to clipboard
Copied
Sorry I might not be clear enough (or my English is terrible). I'm working on a PDF that will make a drawing of a pole and a certain piece number has to be different depending on the height of the pole.
If I check the box PA40, and the number in the field PIED is lower then 18 (event.value < 18), a text field written 123456 will appear.
If I check the box PA40, and the number in the field PIED is 18 or higher (event.value >= 18), a text field written 789456 will appear.
Copy link to clipboard
Copied
My question was: What should be the value of this text field when PA40 is not checked?
Copy link to clipboard
Copied
If the box is not checked the box will be empty or not visible. They have to check the box and enter a dimension.
Copy link to clipboard
Copied
I now wish to use that checkbox to make another thing: if the checkbox PA40 and the Text Field named PIED is 18 or higer, it will show the layer named PA4, and if the Text Field named PIED is lower then 18, it will show the layer PA4L.
Place this script in the checkbox:
if (event.target.value != "Off") && (this.getField("PIED").value >= 18) {
var ocgArray1 = this.getOCGs();
for (var i=0; i < ocgArray1.length; i++) {
if (ocgArray1.name == "PA4") {ocgArray1.state = true;}
if (ocgArray1.name == "PA4L") {ocgArray1.state = false;}
}
}
else if (event.target.value != "Off") && (this.getField("PIED").value < 18) {
var ocgArray1 = this.getOCGs();
for (var i=0; i < ocgArray1.length; i++) {
if (ocgArray1.name == "PA4") {ocgArray1.state = false;}
if (ocgArray1.name == "PA4L") {ocgArray1.state = true;}
}
}
else {
var ocgArray1 = this.getOCGs();
for (var i=0; i < ocgArray1.length; i++) {
if (ocgArray1.name == "PA4") {ocgArray1.state = false;
if (ocgArray1.name == "PA4L") {ocgArray1.state = false;
}
}
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
I enter the code JR_Boulay and it doesn't work. I put it in a JavaScript tester and there are errors (something about identifier and operators).
Copy link to clipboard
Copied
You need to decide, are you using a layer or a field?
Copy link to clipboard
Copied
I think a field would be easier to handle since I already have like 20 layers (and would need to create like 10 more).
Copy link to clipboard
Copied
OK, I think that's a wish choice. You can use this code as the custom calculation script of your text field, then:
var v1 = this.getField("PA40").valueAsString;
var v2 = Number(this.getField("PIED").valueAsString);
if (v1=="Off") event.value = "";
else {
if (v2<18) event.value = "123456";
else event.value = "789456";
}
Copy link to clipboard
Copied
This works perfectly!! Thanks a lot guys, you deserve a box of cookies!!
Copy link to clipboard
Copied
Beware that an OCG state is not saved with the document and revert to default each time the document is closed.
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
Why is that when I try this code nothing works. I need to show or hade a layer I created using a check box.

