javascriptscopeacrobatacrobat-sdk

toggle visibility of a layer (the state property) in Acrobat not working


I'm trying to toggle layer visibility in an Acrobat pdf by setting the state property to either true or false. It works fine in a proof of concept pdf, but it's not working here. I feel like this is a scope issue, because I'm getting into the switch statement but the states aren't toggling. I feel like I'm not referencing STARS in displayRating correctly with respect to the document. Any ideas? It seems like the result of the splice suddenly isn't the same as what that item is in the LAYERS array once it's pushed into STARS. Thanks!

// document javascript    
var STARS = [];
function init(){
    var LAYERS = this.getOCGs();
    var l = LAYERS.length;   
    for(var i = 0;i<l;i++){
        if(/^(STAR_)/i.test(LAYERS[i].name)==true){
            STARS.push(LAYERS.splice(i,1,null));
        };
    }
}
function displayRating(r){
    // called from a button in the pdf
    switch(parseInt(r)){
        case 3:
            STARS[0].state = STARS[1].state = STARS[2].state = true;
            break;
        case 2:
            STARS[0].state = STARS[1].state = true;
            STARS[2].state = false;
            break;
        case 1:
            STARS[0].state = true;
            STARS[1].state = STARS[2].state = false;
            break;
        default:
            STARS[0].state = STARS[1].state = STARS[2].state = false;
    }
}
init();

Note: In the Acrobat SDK pdf, the state property is described on page 520.


Solution

  • Solved: It turns out the splice statement was causing the problems. It was recording the null value into the LAYERS array and then pushing null into the STARS array. I thought it would return the value from the splice first, and then fill it with null after, but that wasn't the case. Solution was to just push the matched value without the splice.