Copy link to clipboard
Copied
I've successfully create a javasript (see below) to set specified layer's initial states to OFF, which works great except I have to close and reopen the file to confirm. Setting the State (visible or not visible) doesn't seem to work when the script is run.
I'd like to essentialy redraw the document with the changed intial states as set, similar to if I'd done this via a Droplet which automatically behaves like the document saves, closes, and reopens.
Is there a javascript command that will do this?
My current script (segment) is shown below.
function setLayerVisProperty(){
var ocgArray = this.getOCGs();
for (var i = 0; i < ocgArray.length; i++){
if (ocgArray[i].name.match (/imprint|inkjet/i)) { //if named contains...
//ocgArray[i].State = false; //then make not-visible. Currently off until learn how to force save & redraw.
ocgArray[i].initState = false //then initial state to not-visible
}
checkPoint = "After"
msg = ("ocgArray[" + i + "] " + ocgArray[i].name + " | " + ocgArray[i].state + " " + ocgArray[i].initState)
report(checkPoint, msg);
}
}
Thanks!
Copy link to clipboard
Copied
Hi,
Based on the code above it is working exactly as I would expect, you are setting the initState, which is the state is it initialised to when the document is opened, and are note setting the State ( that line is commented out), so this means that it will not do anything until the document is closed and opened again.
as stated in the documentation -
If you uncomment the state line, then the code should work with out closing and opening.
Copy link to clipboard
Copied
Thanks BarlaeDC,
I had thought that, too -- but even when including this line (below), the visibility of the open PDFs layers are not changed. I know it should work, but it doesn't, which lead to my wondering if there were a method to force the redraw based on the PDFs new initial state settings.
Video added shows what happens when this line is active, and it should work but doesn't.
I appreciate your input, and ideas from anyone on this topic.
ocgArray[i].State = false
Copy link to clipboard
Copied
Hi,
I am not sure what is happening in your code, attached is a sample that shows the following
- how to toggle all layers
- how to toggle specific layers
- how to show a specific layer
- how to hide a specific layer
Hopefully you can use this to solve your code, I will try and recreate your problem but it would be easier if you could share your file (or a file that shows the problem).
Copy link to clipboard
Copied
Hello everyone and particularly BarlaeDC for the great example.
I have a strange phenomenon here.
I use a similar JavaScript button to toggle the visibility of my comments layer in diverse documents. In some, but not all documents, I have the phenomenon that once I set the visibility of the comments layer to invisible using this JavaScript button, and then simply change the zoom factor of the PDF document, the invisible layer turns visible again.
See the attached sample document that shows this phenomenon using your toogle button, Barlae.
It is even stranger that this problem disappears once you change the visibility of the comments layer one single time using the layers pane and the eye symbol. The problem successively completely disappears until the document is closed and reopened.
It also does not happen with all documents of mine that have a comments layer and this layer visibility toggle JavaScript button, only with some documents.
Can we conclude that this is a bug in Adobe Acrobat and Reader? I have tested it both in the latest versions of Reader and Acrobat DC Professional.
Copy link to clipboard
Copied
The function updateCommentsButtonDocLevelScript is not defined.
Look at the error in the Javascript console.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Works just fine for me. Where are you opening the file, exactly? In what application, including the exact version number?
Copy link to clipboard
Copied
Did you change the zoom level after pressing the button that hides the comments? When I do that here, the comments reappear immediately.
(Adobe Acrobat Reader 32bit Continuous Release Version 2023.006.20380)
Copy link to clipboard
Copied
Yes, I see the issue now. I would say the file is probably corrupt. Do you know how it was created? The only thing I see in the Properties is that it was done using "ImageMagick"...
Copy link to clipboard
Copied
Well, the file (minus the comments) comes from the patent office. Also, for me the problem occurs with many different files, and only in Acrobat. I think this is more of a bug on the part of Adobe.
You can also try it with a file of yours by inserting Barlae's button and moving any content to another layer.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
The hidden layer even becomes visible if you just scroll the document. 🤦🏼
We probably now just have to wait 5 to 15 years for this bug to be fixed.
Copy link to clipboard
Copied
Ok, and when I try to auto-hide all layers after opening the document by using the following document level script, this works initially. However, in this case, the hidden layers are made visible again after scrolling by just one line, instead of after scrolling multiple pages (or zooming) as with the above example:
// Auto-hide all layers after opening the document
app.setTimeOut("ToggleAllLayers()", 2000); // Delays auto-execution when opening the document by 2 seconds
function ToggleAllLayers() {
var ocgLayers = this.getOCGs();
for (var i = 0; i < ocgLayers.length; i++) {
ocgLayers[i].state = !ocgLayers[i].state;
}
}
Out of desperation, I then tried this for further debugging. This document level script beeps every three seconds and writes the current time in a text field on the page. This works completely unpredictably one to four times after opening the document, before it stops:
// Beep every three seconds and write the current time in the document's text field named "FeedbackField
function testBeepAndUpdateField() { // beep and write current time in document text field
app.beep();
// Update the text field with the current time
var now = new Date();
var field = this.getField("FeedbackField");
if (field) {
field.value = "Updated at: " + now.toLocaleTimeString();
}
app.setTimeOut("testBeepAndUpdateField()", 3333); // function calls itself every 3333 milliseconds
}
app.setTimeOut("testBeepAndUpdateField()", 2000); // calls function and delays first auto-execution when opening the document by 2 seconds
I can't believe it. Q: How buggy do you want Adobe Acrobat to be? A: Yes.
(Works all perfectly in PDF-XChange Editor)
Copy link to clipboard
Copied
I encountered the same issue with another file, and it was driving me crazy.
What worked for me was changing the Inital State Visibility property in PDF XChange to "When On" instead of "Never" or "Always." I tested this on the file you provided, and it seems to resolve the problem as well.
I'm not sure if this solution is specific to my machine or if there's something else at play, but any feedback on this would be greatly appreciated.