Skip to main content
Participating Frequently
December 10, 2019
Answered

Javascript toggle/show layers Acrobat DC

  • December 10, 2019
  • 2 replies
  • 1327 views

Hello folks,

I need some assistance by toggling the visibility of layers. I am not to familiar with JS yet but i managed at least to show single layers depending on what is selected from a list. The layers hold descriptions in English, German and French language. This layers are shown if i put in the following code into the validation field:

 

var layers = this.getOCGs();
var v = event.value;
for (var i = 0; i < this.layers.length; i++) {
if (layers[i].name == "Deutsch" && v == "Deutsch") {
layers[i].state = true
}
else if (layers[i].name == "English" && v == "English") {
layers[i].state = true;
}
else if (layers[i].name == "Francais" && v == "Francais") {
layers[i].state = true;
}
else {
layers[i].state = false;
}
}

 

How do I manage to show another layer named "Hintergrund" (German for Background) all the time, no matter which of the language selections has been made?

 

Thank you in advance for your kind support!

 

Matthias

This topic has been closed for replies.
Correct answer try67

Add this to the top of the if-condition:

 

if (layers[i].name == "Hintergrund") {
layers[i].state = true

} else if (layers[i].name == "Deutsch" && v == "Deutsch") {

// etc.

2 replies

JR Boulay
Community Expert
Community Expert
December 10, 2019

Beware, the state of the layers is not saved at the closing of the document.

Acrobate du PDF, InDesigner et Photoshopographe
Thom Parker
Community Expert
Community Expert
December 10, 2019

Use this to save the OCG state with the PDF

 

layers[i].state = layers[i].initState = true;

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
December 10, 2019

I think you mean:

layers[i].initState = true;

And it's important to note that won't work in Reader, only in Acrobat.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
December 10, 2019

Add this to the top of the if-condition:

 

if (layers[i].name == "Hintergrund") {
layers[i].state = true

} else if (layers[i].name == "Deutsch" && v == "Deutsch") {

// etc.

M.PeppelAuthor
Participating Frequently
December 10, 2019

Dear try67,

thank you very much! It worked, did not think it was so simple...

 

Cheers

 

Matthias

ex nihil nihilo fit