Skip to main content
Inspiring
May 6, 2024
Answered

Script to automate the function of buttons

  • May 6, 2024
  • 1 reply
  • 1897 views

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);
    }
}

 

This topic has been closed for replies.
Correct answer Samuel22307458169h

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.


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!

1 reply

try67
Community Expert
Community Expert
May 6, 2024

The setAction command shouldn't be a part of your code. You only need to run it once, from the JS Console.

Inspiring
May 6, 2024

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.

try67
Community Expert
Community Expert
May 6, 2024

Your code does that, but the inclusion of the setAction command is unnecessary and will cause it not to work outside of Acrobat.