Skip to main content
Participating Frequently
December 5, 2018
Answered

Why hidden OCG shows up after reopening the PDF

  • December 5, 2018
  • 2 replies
  • 2439 views

Hi,

I am trying to programmatically hide certain OCGs by putting them into separate layers and setting their states to false

However, when the PDF is reopened, such state is not retained and they still show up in the PDF.

Example code:

this.setOCGOrder(this.getOCGs()); var ocgs = this.getOCGs(); for (var i=0; i<ocgs.length; i++) {     var ocg = ocgs;     if (ocg.name === "Hide") {         ocg.state = false;     } }

I have tried several alternatives (described below) but still no luck:

  1. Set the initState to false
  2. Set the state of the layers to false when PDF is opened.

Does anyone know how to solve this issue?

Much appreciate it!

BTW, I am using Adobe Acrobat Pro DC

This topic has been closed for replies.
Correct answer Thom Parker

Hi Thom,

Thanks for the suggestion.

1) No errors are reported.

I tried to print the state of ocg before and after changing its value and the console shows the value is changed successfully.

But somehow, the layers still show up in Adobe Reader.

2) The full story is as following:

we are trying to keep the PDF editable while making sure a security watermark is always present.

Currently we come with an idea to use javascript to create watermark on the fly whenever users try to make a save.

So far it seems to work OK, but it will lead an issue that more and more layers will be created when more saves are made.

When there is a timestamp in the watermark, it looks really bad.

Then I am trying to see if it is possible to make only one layer visible and leave the rest hidden.


As it happens, I have a security layer example and all the code for doing exactly what you want at www.pdfscripting.com.  The example contains text that explains the technique. Which includes adding text as a watermark. There is only one bit that cannot be done from a script. Which is setting  the visibility option.  If you want to automate this process, then a plug-in is required to do the bit that cannot be done in JS. However, the example is only for site members.

Creating watermarks on the fly can be trouble some. They cannot be modified or removed directly with a script. And the visibility cannot be reliably controlled with a script. If you want a dynamic security warning, then a Text Annotation would be a better choice.

2 replies

Thom Parker
Community Expert
Community Expert
December 6, 2018

The first example in the list at that link is "Lite Doc Security Technique".  That's the one.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Community Expert
December 5, 2018

First, in the Properties dialog for the OCG Layer, the visibility has to be set to "Visible when on", and the default state to "Off". Then the PDF has to be saved.  So the layer will always be off when the PDF is opened.The default state in OCG properties dialog is the same as the "initState" JavaScript property. If you want to change the state on opening you have to change the "initState" property of the layer and then save the PDF.

The only critical bit here that cannot be done from JavaScript is setting the "Visibility property". This can only be done manually from the OCG Properties dialog.

All this and more is explained here:

https://acrobatusers.com/tutorials/create_use_layers

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
December 5, 2018

Hi Thom,

Thanks a lot for the reply. I was referring that tutorial to build up the flow (just noticed that you are the author. Thanks for the tutorial!)

Here is the code I used to changed the state on opening (the initState was set to false already in the earlier safe):

var ocgs = this.getOCGs();

for (var i=0; i<ocgs.length; i++) {

    var ocg = ocgs;

    if (ocg.name === "Hide" and ocg.initState === false) {

        ocg.state = false;

    }

}

But such code does not work in the Acrobat DC or Acrobat Reader DC.

Is there anything I missed?

It is very critical that JavaScript could not set the Visibility property.

I was using the API addWatermarkFromText to dynamically add watermark to the PDF and hide it later on, under certain cases.

Given your experience, would you see any other route to achieve this?

Thanks a head!

try67
Community Expert
Community Expert
December 5, 2018

Replace "and" with "&&".