7 Comments
User's avatar
Tom Mort's avatar

OK, thanks. I did some searching and found out where to enter a document level script.

Went to Tools/Javascript/Document Javascripts and pasted this in:

var ocg=this.getOCGs();

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

{

ocg[i].state=(this.getField(ocg[i].name).value!="Off")

}

tried a couple times and got errors, deleted some things and it seemed to work, pasted the code between the brackets the last time.

It did work with Foxit reader which is supposed to be compatible with Acrobat forms and controls. I tried on a couple of browsers, Chrome and Edge and the checkboxes don't work with those.

I thought I read that browsers could also work with these sorts of pdfs.

As long as there is an easy to get reader. I know Acrobat Reader works and I heard Foxit as well. I'll see what others might also work and what platforms they are available for.

I'm doing something very similar and will recreate this with watermark OSGs that use my image files.

I want to have several layers of image files stacked on top of each other and be able to vary the transparency/opacity of each layer. I played around a little with some image file watermark OSGs and found that if I had several layers of the same image each at say 20% opacity that I could vary the opacity of the number of that layer I made visible at the same time. I'm thinking although it would be busy and cumbersome I could have a row of checkboxes for each layer to change the number that are visible and in the process change the opacity.

I also want to investigate creating a slider with script to control the opacity of a stack of stamps. That seems more intuitive to use.

Expand full comment
David Dagley's avatar

Web browser PDF viewers hardly support any scripts so this probably won't work.

Expand full comment
Tom Mort's avatar

I can see that now. I think there is an Acrobat Reader for both Apple and Window and also for Iphone and Android. I know Foxit viewer also works. I mainly use Linux, most of the apps on Linux are free, ironically the viewers that work with interactive pdfs for Linux are expensive.

It doesn't matter for me though. I'm trying to emulate in a pdf what I have put together in a GIS. I already can see these images and toggle them on and off, adjust opacity and other things.

Expand full comment
Tom Mort's avatar

I went ahead and switched to an external editor and it worked. Notepad didn't, I even selected it with the selector. notepad++ did though. The watermark layers turn on and off now. I noticed in notepad++ that the hotkey for save is control S. That sounds familiar. I'm thinking I might have needed to use a hotkey in the built in editor and suspect that is it, but I haven't tried it yet.

I still don't understand where I put the script for initial state though. I looked at the properties tab for the checkbox controls and didn't see it and I thought I looked through every tab and combo box.

Expand full comment
David Dagley's avatar

Here are the steps for adding Notepad as the external JavaScript editor:

1) Type Notepad in Windows search then right click the app and select "Open file location".

2) Note the file location and close the window.

3) In Acrobat press Ctrl + K to open the preferences window.

4) In the JavaScript category, JavaScript editor section, select "Use external JavaScript editor".

5) Click the browse button and connect to the file from step 2 above.

If you connected Notepad++ you probably did all that, but with Notepad++ instead. I'm just wondering at which step this didn't work.

The initial state is in a document level script. The easiest way to get there is to press Shift + d when you're in form editing mode (prepare a form), which you can toggle back and forth between by pressing Ctrl + Shift + 7. Changing the state of layers does not "dirty" the document and therefore, the last state will not be remembered and has to be reset every time the document is opened. In this case it is set by the check box value. For a script to run every time a document is opened, the proper place to put it is a document level script. Some people put them in a page open action of the first page, but that script will not only run when the document is opened, but each time the user navigates to that page. This may or may not cause issues, depending on the script. Here's an article about document level scripts:

https://pdfautomationstation.substack.com/p/document-level-scripts

Expand full comment
Tom Mort's avatar

you state: 12) In the Actions tab, select the following dropdown items and click Add

you didn't specify, I assume it is to be run javascript.

that gets me to the built in javasctipt editor. I can paste in the script but only see close, no save. Script didn't work, but maybe because I didn't save? I'm new to this and ran into this editor earlier and didn't know how to save or check if there was a sript for this control.

Later: "Set the initial state when the PDF opens"

I guess for this I would enter the script via the console?

Expand full comment
David Dagley's avatar

I missed an image in point 12, which has now been added (Trigger: Mouse Up, Action: Run a JavaScript). After you paste the script into the JavaScript editor, click OK (the video shows closing the window and being prompted to dave because I'm using Notepad as my JavaScript editor instead of the built-in Acrobat one). If you go back into the field properties you should see the "Run a JavaScript" action. If you click Edit while it is selected you should see it there.

"Set the initial state when the PDF opens" should have been large heading text (also corrected now). The document level script that follows sets the intial state everytime the document opens.

Expand full comment