Copy link to clipboard
Copied
I'm trying to control the visibility of certain checkboxes and layers on my PDF form according to what is selected in a drop down menu. I've found 2 scripts; one to show/hide the layers and one to show/hide the checkboxes. I'm a novice when it comes to java scripting. How do I combine them?
If "Option1" is selected from the dropdown, the layer of the same name along with Box3 and Box4 should show.
If "Option2" is selected, the layer of the same name along with Box1 and Box2 should show.
If "-Select-" is selected (default), none of the options above should show.
Layers script:
var layers = this.getOCGs();
var v = event.value;
for (var i = 0; i < this.layers.length; i++) {
if (layers[i].name == “Option1” && v == "Option1") {
layers[i].state = true;
}
else if (layers[i].name == “Option2” && v == "Option2") {
layers[i].state = true;
}
if (layers[i].name == "Option1" && v == "Option2") {
layers[i].state = false;
}
else if (layers[i].name == "Option2" && v == "Option1") {
layers[i].state = false;
}
if (layers[i].name == "Option1" && v == "-Select-") {
layers[i].state = false;
}
if (layers[i].name == “Option2” && v == "-Select-") {
layers[i].state = false;
}
}
Checkbox script:
var v = event.value;
if(v == "-Select-"){
this.getField(“Box1”).display = display.hidden;
this.getField("Box2").display = display.hidden;
this.getField("Box3”).display = display.hidden;
this.getField("Box4").display = display.hidden;}
if(v == "Option2"){
this.getField("Box1").display = display.visible;
this.getField("Box2").display = display.visible;
this.getField("Box3”).display = display.hidden;
this.getField("Box4").display = display.hidden;}
if(v == "Option1"){
this.getField("Box1").display = display.hidden;
this.getField("Box2").display = display.hidden;
this.getField("Box3”).display = display.visible;
this.getField("Box4").display = display.visible;}
Any help or advice would be greatly appreciated!
Thanks,
Trisha
Copy link to clipboard
Copied
It means the line above the one highlighted has a curly end quote after "Box3 instead of a straight quote. I can see a lot of other curly quotes as well. Correct those and make sure you are using a plain text editor to write your scripts so this does not happen. I believe console in Mac is Cmd + j, or you can search for "Debugger" in the Tools tab search.
Copy link to clipboard
Copied
Removed by author.
Copy link to clipboard
Copied
Not quite what I'm trying to do. The link you provided is about using a checkbox to control layer visibility.
I'm want to use a drop down to control the visibility of both layers and checkboxes.
Copy link to clipboard
Copied
Just put them one after another. They should work independently of each other.
Copy link to clipboard
Copied
That's what I thought but it didn't work at all when I combined them.
Copy link to clipboard
Copied
Which part doesn't work? Do both scripts work independently? If so they should work together. If not, check the console for errors.
Copy link to clipboard
Copied
Both scripts work independently. When I put them together, no matter what order they are in. I'm on a Mac so your Console instructions don't work for me. Screen shot below shows error in Acrobat. I have no idea what it means.
Copy link to clipboard
Copied
It means the line above the one highlighted has a curly end quote after "Box3 instead of a straight quote. I can see a lot of other curly quotes as well. Correct those and make sure you are using a plain text editor to write your scripts so this does not happen. I believe console in Mac is Cmd + j, or you can search for "Debugger" in the Tools tab search.
Copy link to clipboard
Copied
THANK YOU!!!
I did alter the script in a Text Edit program but had no idea about the quotes.
Copy link to clipboard
Copied
Everything is working, I appreicate your help!!
One more issue though...
I didn't realize that the PDF doesn't save with the layers visible that were chosen by the dropdown. Any idea how to resolve this? I've read that it can be done but I know next to nothing about java.
Copy link to clipboard
Copied
I figured it out. Have to add
layers[i].initState = layers[i].state;
after each layer state.
Copy link to clipboard
Copied
That won't work if the file will be viewed in Reader.
Copy link to clipboard
Copied
I don't know what program each customer will use to view the form.
Is there something that will make it wwork in both Acrobat and Reader?
Copy link to clipboard
Copied
To make it work in Reader you will need to use a script to save the state of the layers when the file is closed (or saved) to something like a field or the file's metadata, and another script to read the info and then use it when the file is opened.
Copy link to clipboard
Copied
Leave your original script and enter the following document level script (change all instances of "Dropdown" to the actual field name):
var v = this.getField("Dropdown").value;
this.getField("Dropdown").value="-Select-";
this.getField("Dropdown").value="Option1";
this.getField("Dropdown").value=v;
Copy link to clipboard
Copied
Thank you, PDF Automation Station. But it's not working for me.
I changed "Dropdown" to the field name and created a line for each dropdown selection. Placed the script in Document JavaScripts. Saved & opened in Reader and nada.
Copy link to clipboard
Copied
The script captures the value of the Dropdown then changes the value to 2 posbilities so your visibility script in the dropdown will run, then changes it back to the original value so the visibility script will run again. Can you post your form?
Copy link to clipboard
Copied
PDF attached. Hopefully I've removed/changed all company info. Note: there are 2 layers not being used currently. They can be ignored.
Copy link to clipboard
Copied
Your document level script has a different field in the first line than the field names in the rest of the scripts. There is an error in the script. There is no field named TaylorFacility. You have another problem. You are using the variable v elsewhere in a calculation and it is being changed to the other value during the doc level script. Change the document level script to this:
var vlu = this.getField("Facility").value;
this.getField("Facility").value="- Select -";
this.getField("Facility").value="BLM (OSP)";
this.getField("Facility").value=vlu;
Copy link to clipboard
Copied
That was my mistake in trying to hide the company info. I've gotten permission to share the real form now. We want the layer to default to "- Select -". Then the customer would select the correct facility and save the form. When we get it back, it should show the layer the customer selected.
With the way I have the code, it's changing the layer to LSV upon open.
Copy link to clipboard
Copied
The script I provided does what you ask. Here it is modified for the new form presented:
var vlu = this.getField("Taylor Facility").value;
this.getField("Taylor Facility").value="- Select -";
this.getField("Taylor Facility").value="Taylor Print Impressions - BLM (OSP)";
this.getField("Taylor Facility").value=vlu;
Copy link to clipboard
Copied
Sorry, you're right. It's just not what I was expecting. As I've said, I'm a newbie at complicated forms and java.
When the facility is selected in Reader, it's doesn't make the layers and items for that facility visible until the document is saved and reopened. Is there a way to change that?
Copy link to clipboard
Copied
It works in Reader but you have errors in the calculation script when used with Reader. layers[i].initState can't be used in Reader. Remove all instances and it should work.
Copy link to clipboard
Copied
Thank you, thank you, thank you!!!!! I really appreciate your help.

