• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Redraw PDF after OCG initial state changed via Javascript

Explorer ,
Mar 30, 2023 Mar 30, 2023

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!

TOPICS
How to , JavaScript

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2023 Mar 31, 2023

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 -

BarlaeDC_0-1680271751977.png

 

If you uncomment the state line, then the code should work with out closing and opening.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 31, 2023 Mar 31, 2023

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 04, 2023 Apr 04, 2023

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 09, 2023 Dec 09, 2023

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 09, 2023 Dec 09, 2023

Copy link to clipboard

Copied

The function updateCommentsButtonDocLevelScript is not defined.

Look at the error in the Javascript console.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 09, 2023 Dec 09, 2023

Copy link to clipboard

Copied

That was the wrong button. I've removed it, see attached. It happens with BarlaeDC's button as well, see on page 1 or page 3 top right.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 09, 2023 Dec 09, 2023

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 09, 2023 Dec 09, 2023

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 10, 2023 Dec 10, 2023

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"...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 10, 2023 Dec 10, 2023

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2023 Dec 11, 2023

Copy link to clipboard

Copied

It could be a bug in Acrobat. You can report it here:

https://www.adobe.com/products/wishform.html

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 15, 2023 Dec 15, 2023

Copy link to clipboard

Copied

Thanks, done

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 16, 2023 Dec 16, 2023

Copy link to clipboard

Copied

LATEST

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines