Copy link to clipboard
Copied
Hello, I have written a script that assigns each button the visibility of 2 levels. The script works fine but when I reopen the PDF the function no longer works.
I have many languages in the pdfs I create and I usually use the set visibility level action in the button properties, but I wanted to automate this. How can I do this?
In the attached code I have only inserted 2 layers
var livBase = "Base";
var livIT = "Italiano";
var livEN = "Inglese";
var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");
var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");
function livelloInglese() {
var ocgs = this.getOCGs();
for (var i in ocgs) {
ocgs[i].state = (ocgs[i].name === livEN || ocgs[i].name === livBase);
}
}
function livelloItaliano() {
var ocgs = this.getOCGs();
for (var i in ocgs) {
ocgs[i].state = (ocgs[i].name === livIT || ocgs[i].name === livBase);
}
}
Copy link to clipboard
Copied
Yes is possible, like this:
var cScriptCode = "\nvar livBase = 'Base';\nvar livIT = 'Italiano';\nvar livEN = 'Inglese';\nfunction livelloInglese(){\nvar ocgs = this.getOCGs();\nfor (var i in ocgs) {\nocgs[i].state = (ocgs[i].name === livEN || ocgs[i].name === livBase);\n}\n}\nfunction livelloItaliano(){\nvar ocgs = this.getOCGs();\nfor (var i in ocgs) {\nocgs[i].state = (ocgs[i].name === livIT || ocgs[i].name === livBase);\n}\n}";
this.addScript("scriptPulsanti", cScriptCode);
var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");
var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");
I did various tests and also found suggestions in Adobe's API reference manual.
I created a "Run Javascript" action with this code and it works great
Copy link to clipboard
Copied
Ok thanks, I investigated and actually the levels were set as never visible, with preflight I set an action that makes them visible if activated, now everything works, thanks again!
Copy link to clipboard
Copied
The setAction command shouldn't be a part of your code. You only need to run it once, from the JS Console.
Copy link to clipboard
Copied
Thanks for reply, what I would like is to have a command that automatically does what I could do with the Set visibility layer action.
That is, for each button I associate the visibility of the layer, each button has its own specific name.
Copy link to clipboard
Copied
Your code does that, but the inclusion of the setAction command is unnecessary and will cause it not to work outside of Acrobat.
Copy link to clipboard
Copied
var livBase = "Base";
var livIT = "Italiano";
var livEN = "Inglese";
var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");
var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");
function livelloInglese() {
var ocgs = this.getOCGs();
for (var i in ocgs) {
ocgs[i].state = (ocgs[i].name === livEN || ocgs[i].name === livBase);
}
}
function livelloItaliano() {
var ocgs = this.getOCGs();
for (var i in ocgs) {
ocgs[i].state = (ocgs[i].name === livIT || ocgs[i].name === livBase);
}
}
SyntaxError: syntax error
1:Console:Exec
undefined
Copy link to clipboard
Copied
Are you running it from the Console? You need to select the full code if you do. But that's not how you should use it.
Put this code under a doc-level script:
// Doc-level code
var livBase = "Base";
var livIT = "Italiano";
var livEN = "Inglese";
function livelloInglese() {
var ocgs = this.getOCGs();
for (var i in ocgs) {
ocgs[i].state = (ocgs[i].name === livEN || ocgs[i].name === livBase);
}
}
function livelloItaliano() {
var ocgs = this.getOCGs();
for (var i in ocgs) {
ocgs[i].state = (ocgs[i].name === livIT || ocgs[i].name === livBase);
}
}
And then run this code from the JS Console, making sure to select all of it before executing it:
var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");
var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");
Copy link to clipboard
Copied
It works! but I have a question, do I have to do this for every document? or is there a way to have it by default in every pdf?
Copy link to clipboard
Copied
You have to add this to every document where you want this to work. You can do it as an Action and run it on multiple files, but you can't do it automatically for any file you open, no.
Copy link to clipboard
Copied
Thanks for your help
I added an action that executes this code:
var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");
var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");
is it possible to insert the code that goes into the javascript of the document with an action?
Copy link to clipboard
Copied
Yes is possible, like this:
var cScriptCode = "\nvar livBase = 'Base';\nvar livIT = 'Italiano';\nvar livEN = 'Inglese';\nfunction livelloInglese(){\nvar ocgs = this.getOCGs();\nfor (var i in ocgs) {\nocgs[i].state = (ocgs[i].name === livEN || ocgs[i].name === livBase);\n}\n}\nfunction livelloItaliano(){\nvar ocgs = this.getOCGs();\nfor (var i in ocgs) {\nocgs[i].state = (ocgs[i].name === livIT || ocgs[i].name === livBase);\n}\n}";
this.addScript("scriptPulsanti", cScriptCode);
var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");
var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");
I did various tests and also found suggestions in Adobe's API reference manual.
I created a "Run Javascript" action with this code and it works great
Copy link to clipboard
Copied
I have an issue, when scroll the doc after press the button for language selection, the layer disappeared.
Copy link to clipboard
Copied
Are you sure it's set up correctly, spanning all the pages in your file?
Copy link to clipboard
Copied
How do I know if it spans all pages?
Copy link to clipboard
Copied
There's no clear indication. You need to set it up that way when you created it. If you didn't, it will only be on one page.
Copy link to clipboard
Copied
Ok thanks, I investigated and actually the levels were set as never visible, with preflight I set an action that makes them visible if activated, now everything works, thanks again!