Javascript Manual Pdf

7 May 2012CPOL

The JavaScript Guide shows you how to use JavaScript and gives an overview of the language. If you need exhaustive information about a language feature, have a look at the JavaScript reference. This handbook is a getting started guide to Node.js, the server-side JavaScript runtime environment. The Node.js Handbook follows the 80/20 rule: learn in 20% of the time the 80% of a topic. Node.js is an open source cross-platform. Since its introduction in 2009, it got hugely popular and now plays.

This article describes how to use Javascript code to create interactive PDF documents.

Table of contents

Introduction

It shouldn't take long for Javascript to be able to render PDF documents. Meanwhile, the somewhat opposite functionality - executing Javascript in PDF documents - is available for quite a long time already. This article is about this functionality.

Any software contains not only an actively used set of features but also a share of rarely used features. Sometimes latter set can be significant in size. You can think of features of Microsoft Word or your favorite IDE, for example. Most probably, there are enough features in them that you never used.

Portable Document Format is also has many rarely used features. We are all accustomed to the text and images in PDF documents, but this is only a subset of what the format offers. PDF has set of features for creating documents that could change their contents in response to reader actions. One of such features is the ability to use Javascript in PDF documents.

Javascript in PDF is most often used for the following tasks:

  • To change document contents in response to some events. For example, to hide part of document before printing or pre-fill some form fields when document is opened.
  • To restrict actions of the reader. For example, to validate entered form field values.
  • To introduce malicious code in documents.

A Javascript API was developed for PDF viewers to be able to interpret Javascript code. The API was primarily developed for Adobe Acrobat family of products but alternative viewers usually also support a subset of the API.

Let's take a look at some samples.

Hello World

Here is a traditional 'Hello World' sample for a start. Please note that I'll use C# and Docotic.Pdf library for the samples. You can download source code of all the samples at the end of the article.

You should see something like on the following screenshot if you open the PDF created by the sample in Adobe Reader:

So, what's done by the sample? Here is the essential line:

PDF supports actions. It is something that is done when an event occurs. For example, clicking on a link in a PDF document outline might trigger an action that cause viewer to open a page in the document:

There are different types of actions defined. One of the types is Javascript actions. An action of this type can be created using PdfDocument.CreateJavaScriptAction method. The method accepts Javascript code. Then the action can be attached to the OnOpenDocument event. Obviously, the event occurs when document is opened in a viewer.

Adobe character animator puppet template. Mar 08, 2019  Created by JeffAlmasol on Jun 26, 2015 9:26 AM. Last modified by JeffAlmasol on Mar 8, 2019 1:43 PM. A copy of the template PSD or AI file is copied to the project folder, imported into the project, added to a new scene, and the PSD or AI file is opened in Photoshop or Illustrator for making changes. After saving changes to the file, switch back to Character Animator. The puppet updates to match. Jan 21, 2019- Create easily motion graphics and bring cartoon characters to life with Adobe Character Animator puppets! You will find more about our 1.) free pre-made. Jan 25, 2019  This free Adobe Character Animator puppet is created by using stop motion technique.Being the first stop motion puppet created in world (stated by the creator himself), the file contains shots of different face expressions and other assets.

Here is the Javascript code:

Static class app is part of the Javascript API. This class provides methods for communicating with a PDF viewer. In particular, this class contains alert method with a number of overloads. The alert method can be used to present a modal window with a message. The code above uses the overload with optional second parameter. This parameter specifies an icon to be displayed (3 is a code for Status Icon).

More complex scenarios

Let's try something more pragmatic.

There are many PDF documents with fillable forms. A document with a form can be a deposit account agreement, a visa application form, a questionnaire etc. Such documents can be conveniently filled right in a viewer and printed or saved for later use. Creators of such documents can employ Javascript to farther improve user experience.

Fillable forms often contain date fields. These fields might look like this:

Sure, a creator of a document can just add date field to the document and stop there. However, it would be nice to assist someone who will fill document later by putting current date to the date field. In most cases this will be just what is expected.

Default value for a date field

I won't go into details of how to create a date field. Let's focus on the Javascript part only. So, we need to put current date into a date field (there is three fields actually, one for day, month and year). Here is the script:

As you can see, there is much more code than in “Hello World” sample. Using a string for all this code is probably not very convenient because of need to escape quotes. Octane render cinema 4d download. And editing such code later might be painful because of lack of formatting. Let's put a text file with code into application resources and use another overload of CreateJavaScriptAction method.

When opened, a document with the script should look like the following:

Take a closer look at following part of the code:

Please notice that I am using an overload of util.printd method to get localized name of the month. This works as expected in Adobe Reader but, unfortunately, other PDF viewers might not support all built-in methods of Javascript API. This should be taken into account if you are going to support anything other than Adobe Reader. Another approach is to implement custom code that does the same as util.printd.

Please also notice that code populates a field only if it's empty. Without this additional check a following scenario might occur: a person fills the document, saves it but next time the document is opened all date fields are reset with current date.

Validation of data

Suppose you want to ensure that day and year are numbers. It's easy. Let's use following code for this:

PDF fields fire OnKeyPress event whenever something is typed in a field. You might want to create an action with the validation code and attach it to OnKeyPress event of fields.

After that a person filling the form won't be able to put anything other than numbers in day and year fields. Anything getting pasted from clipboard will also be validated.

Synchronization of data

Quite often same data should be put several times in different parts of a form. In such cases some Javascript code can help to eliminate the need for this repetitive efforts.

Assume that there is a document like this:

It would be nice to have both fields synchronized.

It's possible with following Javascript method:

The Javascript method could be used like this:

This will ensure that data in both fields is the same at all times. The data is synchronized whenever any field looses focus. Please notice that synchronizeFields method is put is shared scripts collection (PdfDocument.SharedScripts). This is done to use the same code from several actions.

Summary

As you can see, Javascript can be used not only in web development. With some additional efforts a PDF form with Javascript code can be created. And such form could please those who will fill it almost like an application with a well thought-out UI.

Don't get me wrong, most important part of a PDF file is its content. Javascript is just a nice addition. And there are some imitations:

  • Writing Javascript code for PDF files is not as convenient as for web pages
  • There is incomplete support for Javascript API in PDF viewers other than Adobe Reader
  • Execution of scripts can be disabled in a viewer

Also, Javascript in PDF files is a security threat. From time to time different vulnerabilities are found (and fixed) in viewers. Knowing all that don't be surprised if an opened PDF file will offer you a chess game to distract you from malicious acts getting performed in background. ' />

Download sample code

Active1 year, 5 months ago

Where can I find Documentation on running Javascript inside a PDF?

I've never added a javascript action inside a pdf. However, I've done quite a bit of web development using javascript. I have a few questions to whoever has any familiarity with javascript inside a PDF.

NitroPDF and Adobe Acrobat definitely support javascript in PDFs. Is there a standard on various objects that exist and functions to manipulate a pdf via javascript? Everything I've found so far has been from Adobe. Anywhere else seems to reference Adobe's docs. Is there a standard out there, or is Adobe just the 'de Facto' standard ?

Also, Do all PDF Viewers support javascript actions?

The best docs on using javascript inside a pdf document that I could find was from Adobe - Adobe :: Acrobat Javascript Scripting Guide.

NitroPDF has this link - NitroPDF :: Javascript in PDF Files, but it basically just says it supports what Adobe has.

I also found this link from Scribus - Scribus :: How to enhance your PDF forms with JavaScript, but this only includes a few small snippets of code. Nothing super useful.

Adobe has docs on how you can set breakpoints etc.. for javascript using their IDE. Is it possible to run javascript in a pdf using another IDE and have breakpoints etc..? Would be awesome to find something as cool as Firebug.

Note:

Please do not answer how to manipulate a PDF that is loaded in a web browser. The question is about running javascript from within a PDF document. I'm trying to explore the possibilities available via executing javascript in the PDF document. Specifically I may use functions from quickpdflibrary to add javascript functionality to existing documents.

Edit

Another useful link is Developing with Acrobat Javascript. Apparently js in PDFs is frequently called AcroJS or Acrobat JavaScript.

SamSam
23.4k11 gold badges63 silver badges94 bronze badges

closed as off-topic by Mogsdad, TylerH, too honest for this site, robinCTS, Nisse EngströmDec 31 '17 at 21:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • 'Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.' – Mogsdad, TylerH, too honest for this site, robinCTS, Nisse Engström
If this question can be reworded to fit the rules in the help center, please edit the question.

5 Answers

Probably you are looking for JavaScript™ for Acrobat® API Reference.

This reference should be the most complete. But, as @Orbling said, not all PDF viewers might support all of the API.

EDIT:

It turns out there are newer versions of the reference in Acrobat SDK (thanks to @jss).

Acrobat Developer Center contains links to different versions of documentation. Current version of JavaScript reference from Acrobat DC SDK is available there too.

BobrovskyBobrovsky
9,01718 gold badges63 silver badges113 bronze badges

I'm pretty sure it's an Adobe standard, bearing in mind the whole PDF standard is theirs to begin with; despite being open now.

My guess would be no for all PDF viewers supporting it, as some definitely will not have a JS engine. I doubt you can rely on full support outside the most recent versions of Acrobat (Reader). So I guess it depends on how you imagine it being used, if mainly via a browser display, then the majority of the market is catered for by Acrobat (Reader) and Chrome's built-in viewer - dare say there is documentation on whether Chrome's PDF viewer supports JS fully.

OrblingOrbling
18.5k3 gold badges45 silver badges62 bronze badges

The comprehensive place for Acrobat JavaScript documentation is the Acrobat SDK, which can be downloaded from the Adobe website. In the Documentation section, you will find all the material needed to work with Acrobat JavaScript.

To complete the documentation you may in addition get the specification of the JavaScript Core. My book of choice for that is 'JavaScript, the Definitive Guide' by David Flanagan, published by O'Reilly.

Max WyssMax Wyss
2,8951 gold badge13 silver badges23 bronze badges

Javascript Manual Pdf 2019

Look for books by Ted Padova. Over the years, he has written a series of books called The Acrobat PDF {5,6,7,8,9..} Bible. They contain chapter(s) on Javascript in PDFs. They are not as comprehensive as the reference docs listed here, but in the books there are some realistic use-cases discussed in context.

There was also a talk on hacking PDFs by a computer scientist, given at a conference in 2010. The link on the talk's announement-page to the slides is dead but google is your friend-. The talk is not exclusively on javascript, though. Youtube video : JS starts at 06:00

Scribblemacher
7621 gold badge7 silver badges24 bronze badges

Javascript The Missing Manual Pdf Download

knbknb

Javascript Manual Pdf Free

6,8414 gold badges46 silver badges72 bronze badges

here you can find 'Adobe Acrobat Forms JavaScript Object Specification Version 4.0'Revised: January 27, 1999

It`s very old but still usefull.

AnatolyAnatoly

Javascript & Jquery The Missing Manual Pdf Download

Not the answer you're looking for? Browse other questions tagged javascriptpdfdocumentationacrobatpdf-manipulation or ask your own question.