Copy link to clipboard
Copied
I want to call a Javascript function when a PDF opens or loads. Or when I click this Layer button.
How do I do it? Thanks in advance!
1 Correct answer
You can embed a code as a doc-level script and it will execute whenever that file is opened.
You can't assign an action to the opening of the Layers panel, though.
Copy link to clipboard
Copied
You can embed a code as a doc-level script and it will execute whenever that file is opened.
You can't assign an action to the opening of the Layers panel, though.
Copy link to clipboard
Copied
You can define a function in a document-level JavaScript and then place a call to it right after, something like:
// Document-level JavaScript
function doSomething() {
// Code goes here
}
// Call the function
doSomething();
There is no event that's available to JavaScript when you click the layers button.
Copy link to clipboard
Copied
How do I add a a document-level JavaScript programmatically?
Copy link to clipboard
Copied
Nevermind. This could be achieved programmatically by adding a Javascript name tree.
Copy link to clipboard
Copied
What do you mean by "programmatically"? What language are you using to edit the PDF?
If you mean using JS itself, the answer is to use the addScript method of the Document object.
Copy link to clipboard
Copied
I use Java to create my PDF with the help of Datalogics' library, and ISO 32000-1:2008, Document Management-Portable Document Format-Part 1: PDF 1.7 as reference.
Copy link to clipboard
Copied
Look at table 31 in ISO32000:2008, this will explain how the document level script is stored in the "Names" dictionary. What I usually do in order to get a better idea how things are actually stored in the PDF file is to create a sample document that contains the feature that I am looking for (in this case a document level script), and then use the Preflight tool's "Browse Internal PDF Structure" to follow along with the information from the specification. You would see something like this:
All you need to do is create the COS structure using the PDFL so that you have a valid script item in the Names hierarchy.
Copy link to clipboard
Copied
Are your tools all for free? And what does COS stand for?
Copy link to clipboard
Copied
Within the Acrobat SDK, the "COS" level refers to the object level described in the PDF specification. I am not familiar with the Java API of the PDFL, but chances are that you will find functions and objects that use a prefix of "COS", which would be the ones manipulating this level of a PDF file.
Which tools are you referring to? The screenshot is from Adobe Acrobat's Preflight tool, built into Adobe Acrobat Pro - so you would have to pay for Acrobat Pro to get access to it.
Copy link to clipboard
Copied
I'm happy to help you with using the Datalogics PDF Java Toolkit if you want to contact me directly. Adding document level JavaScript is pretty easy.
J-
Copy link to clipboard
Copied
Joel – I've done it by creating a NameTree object, and then put a PDFDict with S and JS keys. Something like this:
NameTree js = this.getDocument().createNameTree("JavaScript");
PDFDict jsdict = new PDFDict(doc, false);
jsdict.put("S", new PDFName("JavaScript", doc, false));
jsdict.put("JS", new PDFString("your_javascript", doc, false, false));
js.put(new PDFString("your_Javascript", doc, false, false), jsdict);
Do you have a simpler way to do that?
Also, do you know how to make the collapsible list in Layers to be expanded by default using Datalogics library?
Thanks in advance!
Copy link to clipboard
Copied
I'm not sure I could make it simpler than the 5 lines you have.
You might need to go deep into the OCG objects to expand the layers. I doubt there's a direct API to that.
J-
Copy link to clipboard
Copied
OCG objects though do not have Bookmark's 'open' property or anything similar.

