Retrieving and manipulating interactive PDF document with JS
<p>I'm working for a friend that needs to manipulate an interactive PDF with the following needs:
there are 2 main "layers" and 2 buttons. When I press button1 only the layer1 is visible, when I press button2, the layer2 is the only visible.</p>

<p>He can upload a javascript for each button with the action that must start on the click, and this is the code I wrote for him.</p>

<pre><code>var layerToHideLabel = "testlayer1";
var layerToShowLabel = "testlayer2";

showField(layerToShowLabel);
hideField(layerToHideLabel);

function showField(fieldName){
    var field = null;
    field = FindOCG(fieldName);
    if(field != null){
        setVisibility(field,true);
    }
}

function hideField(fieldName){
    var field = null;
    field = FindOCG(fieldName);
    if(field != null){
        setVisibility(field,false);
    }
}

function setVisibility(field, status){
    field.state=status;
}

function FindOCG(cName) { 
    var aOCGs = this.getOCGs(); 
    for(var i=0; aOCGs &amp;&amp; i&lt;aOCGs.length;i++) { 
        if(aOCGs[i].name == cName) return aOCGs[i]; 
    } 
    return null; 
}
</code></pre>

<p>The <code>getOCGs</code> function is a built-in functionality for the document, and of course, when I launch the method I get the following error:</p>

<pre><code>this.getOCGs is not a function
</code></pre>

<p>I think the issue is context related, as the script is launched on the click of the button, so maybe <code>this</code> refers to the button and not to the document.</p>

<p>I tried lot of combinations to retrieve the document from the OCG, with no luck. I tried document, $doc, doc, Document, but nothing. I'm afraid it's not even possible, maybe I should try to upload some code at document level, but I don't know if I can add some <code>onchange</code> functionality.</p>

<p>This is the guide I've been following so far:
<a href="https://acrobatusers.com/tutorials/create_use_layers" rel="nofollow">https://acrobatusers.com/tutorials/create_use_layers</a></p>

<p>Thanks in advance</p>