Skip to main content
Known Participant
January 27, 2025
해결됨

How to troubleshoot Acrobat? Code worked last week but fails today.

  • January 27, 2025
  • 2 답변들
  • 978 조회

This is infuriating. I had functional JavaScript code last Friday that doesn't work today on the same file. See "How to show/hide single layers in browser with JavaScript?" (https://community.adobe.com/t5/acrobat-sdk-discussions/how-to-show-hide-single-layers-in-browser-with-javascript/m-p/15103708#M93769).

 

The PDF file has no duplicates, yet this code that worked no longer shows the layer:

function find(ocgname){
    var layers = this.getOCGs();
    for(var i=0; i < layers.length; i++) { 
        if(layers[i].name == ocgname) return layers[i];
    }
    return null;
}

bld = find("F1")
layer = bld.name;
layer.state = true;

true

I also tried this single step code:

var layers = this.getOCGs();
var cnt = layers.length;
layer = layers[9].name;
layer.state = true;

undefined
undefined
F1
true

and that failed to show the layer. I have verified that there are no duplcate layers with:

setOCGOrder(getOCGs())

Setting the state to true is supposed to show the layer. It did but now it doesn't!

 

1. A power event occurred this weekend that that caused a hard shut down and so everything, including Acrobat, shut down ungracefully. Could that have something to do with its state now?

2. What tools are provided that would help me troubleshoot my PDF to see why the code that worked last week will not work today?

최고의 답변: Thom Parker
quote

Something very odd appears to be going on with layers. To test show/hide I added the following J...

......After running it, I see that the last command added triple layers for each of the watermarks that were just added as shown in the attached screenshot. 

 

What is up with layers in the API?


By @jeff_biss


I think that maybe you have been working with this PDF for a bit and those extra layers are leftovers. 

 

2 답변

JR Boulay
Community Expert
Community Expert
February 12, 2025

[MOVED TO THE ACROBAT DISCUSSIONS]

Acrobate du PDF, InDesigner et Photoshopographe
jeff_biss작성자
Known Participant
January 27, 2025

Something very odd appears to be going on with layers. To test show/hide I added the following JavaScript to the console from Thom's "Creating and using layers (OCGs) with Acrobat JavaScript" (https://acrobatusers.com/tutorials/create_use_layers/) article:

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

// First rename any previous layers named Watermark 
var ocg = null; 
while( (ocg = FindOCG("Watermark")) != null) ocg.name = "PreviousWatermark"; 

// Add three OCG layers to current PDF 
this.addWatermarkFromText({cText:"One",aColor:color.red,nVertValue:18}); 
FindOCG("Watermark").name = "LayerOne"; 
this.addWatermarkFromText({cText:"Two",aColor:color.blue,nVertValue:36}); 
FindOCG("Watermark").name = "LayerTwo"; 
this.addWatermarkFromText({cText:"Three",aColor:color.green,nVertValue:54}); 
FindOCG("Watermark").name = "LayerThree";
this.setOCGOrder(this.getOCGs());

After running it, I see that the last command added triple layers for each of the watermarks that were just added as shown in the attached screenshot. 

 

What is up with layers in the API?

Thom Parker
Community Expert
Community Expert
January 28, 2025

Your code has a simple mistake. 

 

This code returns the OCG/layer object. 

bld = find("F1")

 

This code returns the name of a layer, which is a string

layer = bld.name;

 

So, this code makes no sense, and never, ever, worked to show a layer.

layer.state = true;

 

This is the code you want, which uses the "state" property of the OCG (Layer) Object

bld.state = true;

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
jeff_biss작성자
Known Participant
January 28, 2025

Yep, that's it as I just discovered. The first thing I did was look at that code and that .name just didn't make sense so I deleted it and the code works. This post should be deleted as it was adding .name that was the problem and this will just get in the way of people looking for valid answers.

 

Thanks for your time, Jeff