Skip to main content
Known Participant
March 30, 2023
Question

Redraw PDF after OCG initial state changed via Javascript

  • March 30, 2023
  • 1 reply
  • 6994 views

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!

1 reply

BarlaeDC
Community Expert
Community Expert
March 31, 2023

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.

Known Participant
March 31, 2023

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

 

BarlaeDC
Community Expert
Community Expert
April 4, 2023

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