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

Why hidden OCG shows up after reopening the PDF

New Here ,
Dec 04, 2018 Dec 04, 2018

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and 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

correct answers 1 Correct answer

Community Expert , Dec 05, 2018 Dec 05, 2018

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 f

...

Votes

Translate

Translate
Community Expert ,
Dec 04, 2018 Dec 04, 2018

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

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
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

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!

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 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

Replace "and" with "&&".

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
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

Sorry, my typo when writing the reply (it is right in my local file). It should be &&.

But the code still not work as expected.

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 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

OK, I've noticed now that you wrote that you're trying to use it on a watermark layers. I believe it's not possible to change the state or initState values of layers that were added like this.

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
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

Hi try67, the Adobe API reference (2015 verison) says the access to "initState" property is R/W (Adobe Reader: R only), while "state" is R/W.

It should be possible to change at least the "state" of layer via JS in bith Adobe Acrobat and Reader.

Actually, that code works OK when the PDF is opened in PDF-XChange Viewer, but surprisingly not in Adobe Reader DC.

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 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

I'm aware of that. I guess you can call it a bug, or an "undocumented feature"...

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 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

It doesn't matter how the OCG was added, they all work the same.   However, The OCG will not follow the state if the "Visibility" is not set to "Visible when on". This is critical to it working, as stated in both the article and my previous post.

In your code, it is not a good idea to use the identity operator. Just do an equality test.  A better way to write the "if" is this.

if ((ocg.name == "Hide") && !ocg.initState)

Now

1) Are any errors reported in the console window?

2)  Is the point to turn off the OCG layer when the PDF is opened?  If so this code is completely unnecessary. If "initState" is set to false and the visibility set to "Visible when on" then the OCG will be hidden when the PDF is opened.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

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.

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 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

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.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
New Here ,
Dec 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

Hi Thom,

Very much appreciate your suggestion of using the text annotation. Let me look into it.

Meantime, can you point me to the actual link to the security layer example you are referring to?

I am not able to find it in the given URL.

Thanks!

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 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

It does matter, actually. If you add it using the Add Header function of Acrobat, for example, it won't work.

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 05, 2018 Dec 05, 2018

Copy link to clipboard

Copied

LATEST

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

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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