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

How do I call Javascript function upon opening of PDF

Explorer ,
Dec 23, 2016 Dec 23, 2016

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!

TOPICS
Acrobat SDK and JavaScript
7.4K
Translate
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 23, 2016 Dec 23, 2016

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.

Translate
Community Expert ,
Dec 23, 2016 Dec 23, 2016

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.

Translate
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
LEGEND ,
Dec 23, 2016 Dec 23, 2016

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.

Translate
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
Explorer ,
Dec 28, 2016 Dec 28, 2016

How do I add a a document-level JavaScript programmatically?

Translate
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
Explorer ,
Dec 28, 2016 Dec 28, 2016

Nevermind. This could be achieved programmatically by adding a Javascript name tree.

Translate
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 28, 2016 Dec 28, 2016

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.

Translate
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
Explorer ,
Dec 28, 2016 Dec 28, 2016

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.

Translate
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 28, 2016 Dec 28, 2016

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:

2016-12-28_08-58-05.png

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.

Translate
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
Explorer ,
Dec 28, 2016 Dec 28, 2016

Are your tools all for free? And what does COS stand for?

Translate
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 28, 2016 Dec 28, 2016

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.

Translate
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 28, 2016 Dec 28, 2016

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-

Translate
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
Explorer ,
Dec 28, 2016 Dec 28, 2016

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!

Translate
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 28, 2016 Dec 28, 2016

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-

Translate
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
Explorer ,
Dec 29, 2016 Dec 29, 2016
LATEST

OCG objects though do not have Bookmark's 'open' property or anything similar.

Translate
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