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
If the script is in the WillSave event, then it is only triggered when the document will save. Which will only happen if the PDF has changed. I would suggest putting "console.println" statements in the code so you know when it has been run, and how it is working.
you'll find a tutorial on the console window that shows this technique (late in the video) here.
https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thank you, Thom! I will try using "console.println" to check when the code is run. I hope to figure this out by the end of the week to complete this form, or else the user will have to print to PDF to save the information before closing. I am waiting for my manager to purchase a membership to your PDFScripting to advance my understanding of JavaScript in Acrobat. Thank you very much for your help.
Copy link to clipboard
Copied
You are welcome. We also offer training, development, and consulting around Acrobat and PDF. Members get a steep discount.
Good Luck!
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Hello Thom,
When you say, "which will only happen if the PDF has changed," does that change include revealing the layers? The PDF will have had to change for it to show the hidden layers, like specific selections from a combo box or radio buttons. And, if the script needs to be moved from a document action Will Save to document level, what would trigger the script to store the layer states if changed? Thank you for your help.
Copy link to clipboard
Copied
No. Changing the state of a layer does not "dirty" the file.
Copy link to clipboard
Copied
Maybe I should back up. The initial state of an OCG on document open is controlled with the OCG initState property. The OCG initState property can only be saved with the document in Acrobat Pro, not Reader. Setting the state of the OCG does not change the initState, so the document opens with the same layer visibility as it did the first time.
Since it seems you are using form fields on the document to determine the OCG state, then the best way to make sure it's all setup correctly when the document is opened is with a document level function that checks the field values and sets the OCG state property. This same function should also be called on the relevant form field events so the OCG state stays in sync with the field values. The idea is do drive the action from the fields that are controling layer visibility
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
- Dear, Reidzilla!
- I'm trying to solve absulutely the same qust as described in your question. Doing the same as described in the topic and it doesn't work 😞
- I have a layer with name "0_Status" and the radio button with name "3_YES_2.1"
- Initially layer has staus OFF (by default).
- When I press radio button, the layer becomes visible. After press Save, This layer should change default layer from OFF to ON. But it not working 😞
- Using the following script (put it at the end of the script line of the button)
- var ocgArray1 = this.getOCGs();
- if (ocgArray!=null) {
- for (var i=0; i<ocgArray.length; i++) {
- if (ocgArray1.name=="0_Status")
- {this.getField("3_YES_2.1").value = (occArray.state) ? "1" : "0"; }}}
Please help me to find the solution, appretiate in advance for assistance of the Best community!
Copy link to clipboard
Copied
Does you use ocgArray1 or ocgArray ?
Copy link to clipboard
Copied
Does you use ocgArray1 or ocgArray ?
By Bernd Alheit
In the script I see, that ocgArray1
Copy link to clipboard
Copied
In the script I see ocgArray1 and ocgArray.
Copy link to clipboard
Copied
I have tried to add 1 without success
Copy link to clipboard
Copied
Check the Javascript console for errors.
Copy link to clipboard
Copied
What Bernd is trying to tell you is that your code contains errors. There is no "ocgArray" variable, and further, the ocgArray1 variable is an array, so you have to use the array notation to use it. I would suggest you look back over the scripts in the previous posts.
For better information, read this article:
https://acrobatusers.com/tutorials/create_use_layers/
The answer to the original question is a qualified NO. The OCG object has a defaultState property, but this property cannot be set from Reader because of access permissions, so it is useless in a gereral purpose form script. The answer was already given in the early posts. To use Layer states on a form, the state value must be saved in a form field, and restored when the document is opened. On your form the layer state is already saved in the radio button. So all you need to do is use a document level script to restore the state when the document is opened. Note however, that this scheme will only work when the document is opened in either Acrobat Pro or Reader, or a very small set of compliant 3rd party PDF viewers. It will not work on any other PDF viewer.
Of course you also need to use the correct script to set the state in the first place.
Read the article. The answers are there.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thank you, Thom.
I'll read it - it looks much harder, tnan i thought, but I hope to understand it and learn to how to use it properly.
Not sure how to use "document level" script. Time to fill up gaps In knowledge
Copy link to clipboard
Copied
Here's an article on document level script.
https://www.pdfscripting.com/public/Document-Level-Scripts.cfm
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Dear, Thom.
Day by Day, I'm tying to solve This problem, but still don't understand, where I making mistake. Debugger shows no errors.
I have created appropriate script on Document level (see attached file), but after save and reopen file, appropriate layers do Not change their status from OFF to ON.
Names of the layers, that have to change default status from OFF to ON, when radio button wth name "Yes2. 0" is activated are:
Layer with name "2_Status_NTO_Not_Required" and layer with name "3_YES_2.1".
Probably I can Not solve This issue, because of radio button?
So, the final code on document level is below:
var ocgArray1 = this.getOCGs();
if (ocgArray!=null)
{ for (var i=0; i<ocgArray1.length; i++)
{ if (ocgArray1.name=="2_Status_NTO_Not_Required") ;
ocgArray1.name=="3_YES_2.1") ;
{ocgArray1.state=this.getField("Yes2.0").valueAsString == "1";
break;
}}}
P. S. When I understand how to make This work, I could adjust all document (with more, then 70 layers) and it will help to engineers In their Hard work.
Here's an article on document level script.
https://www.pdfscripting.com/public/Document-Level-Scripts.cfm
By Thom Parker
Here's an article on document level script.
https://www.pdfscripting.com/public/Document-Level-Scripts.cfm
By Thom Parker
Copy link to clipboard
Copied
 
Copy link to clipboard
Copied
You must access the layers with ocgArray1[i]
e.g. ocgArray1[i].name
Copy link to clipboard
Copied
You're code still contains many errors. As Bernd points out, the OCG object must be accessed with array notation, and the script still has instances of the wrong ocgArray variable name, and there are several more. Programming is all about the fine details.
Also the code is formatted badly. While this doesn't affect how the code works, it makes it very difficult for the people who you are asking for free help. In the furture, please format your code so we can read it!!
Here is the fixed code.
var ocgArray1 = this.getOCGs();
if (ocgArray1!=null)
{
for (var i=0; i<ocgArray1.length; i++)
{
if((ocgArray1[i].name=="2_Status_NTO_Not_Required") || (ocgArray1[i].name=="3_YES_2.1"))
{
ocgArray1[i].state=this.getField("Yes2.0").valueAsString == "1";
break;
}
}
}
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Dear, Thom and Bernd.
Thank you for helping!
(pdf form still inoperative, reading manuals at This moment to understand, what is wrong).
Do you, know if Adobe Acrobat has full training with Javascript and other benifits of Acrobat Professional? I Want to discuss possibility to buy appropriate training with my managers.
Copy link to clipboard
Copied
You'll find lots of training materials on this site:
I also provide custom training/development sessions for site members. Contact me through this forum or the pdfscripting.com site.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
It works! I'm happy!!!
Thank you for your help!
It was Not easy for me, but the resault is so great!
If it is requered, I can describe in details, how I made it 🙂
Copy link to clipboard
Copied
Thank you, Thom! This variation with adjustments worked. Utilizing the values of the radio buttons or combo boxes helped significantly. I am sharing the script used in one of the two forms.
var ocgArray1 = this.getOCGs();
if (ocgArray1!=null)
{
for (var i=0; i<ocgArray1.length; i++)
{
if((ocgArray1[i].name=="Layer2") || (ocgArray1[i].name=="Layer2State"))
{
ocgArray1[i].state=this.getField("rdoMeetExpt").valueAsString == "1"||this.getField("rdoMeetExpt").valueAsString == "2"||this.getField("rdoMeetExpt").valueAsString == "3";
}
else if((ocgArray1[i].name=="Layer3") || (ocgArray1[i].name=="Layer3State"))
{
ocgArray1[i].state=this.getField("ddlRateValue").valueAsString == "Acceptable"||this.getField("ddlRateValue").valueAsString == "Needs Improvement"||this.getField("ddlRateValue").valueAsString == "Poor";
}
else if((ocgArray1[i].name=="Layer4") || (ocgArray1[i].name=="Layer4State"))
{
ocgArray1[i].state=this.getField("ddlRSRMetExpt").valueAsString == "Acceptable"||this.getField("ddlRSRMetExpt").valueAsString == "Needs Improvement"||this.getField("ddlRSRMetExpt").valueAsString == "Poor";
}
else if((ocgArray1[i].name=="Layer5") || (ocgArray1[i].name=="Layer5State"))
{
ocgArray1[i].state=this.getField("ddlRateImage").valueAsString == "Could Be Better"||this.getField("ddlRateImage").valueAsString == "Below Expectations"||this.getField("ddlRateImage").valueAsString == "Poor Quality";
}
else if((ocgArray1[i].name=="Layer6") || (ocgArray1[i].name=="Layer6State"))
{
ocgArray1[i].state=this.getField("rdoSpecSvc").valueAsString == "Yes";
}
else if((ocgArray1[i].name=="Layer7") || (ocgArray1[i].name=="Layer7State"))
{
ocgArray1[i].state=this.getField("rdoResign").valueAsString == "No";
}
else if((ocgArray1[i].name=="Layer8") || (ocgArray1[i].name=="Layer8State"))
{
ocgArray1[i].state=this.getField("rdoPassInsp").valueAsString == "No";
break;
}
}
}


-
- 1
- 2