JavaScript in PDF to HTML5 Conversion: The Event Object

Previously I mentioned that a key component in understanding JavaScript in PDFs was understanding how events work, in this blog article I will go into a little more depth on that subject.

Within the JavaScript for Acrobat API Reference there is a section describing the event object (it’s actually one of the smaller objects if you have a look at the others) that explains briefly how events work within a PDF. This is one of the most vital objects used within PDFs containing JavaScript as it controls the output and results of each user driven event.

For instance, if the event in question is a Keystroke it runs the code for the Keystroke event, making use of an event object to store information on the event and ultimately, decide what output occurs.

After or during the Keystroke event a call can be made to the Validate events which in turn can follow down to other events including the Calculate and Format events.

If the event is not a Keystroke event we start at the Validate event. This is actually a gross simplification of how events work since there are also many other possible events and the Calculate event can generate additional Validate, Blur and Focus events. But it for now I will focus on Keystroke events since they are, from what I can tell the simplest event that can occur to a form field.

In our latest version of the converter we map the keystroke actions onto the onKeyPress attributes of input objects, taking into account how the events work within PDF files. Which for the most part works correctly as they seem to have a one to one relationship with each other. Originally we tried mapping onto the onKeyDown attribute because of how the browsers implemented key presses differently but eventually we wrote a work around for this issue as onKeyDown had some undesirable properties.

One major thing to consider about the conversion is that Calculate events also occur for all Field objects (that have them) whenever you leave a Field (on a blur) which in turn can generate more Validate and Format events. So we move the code from each Calculate event to a global function within our JavaScript that is then run on each field items onBlur attribute, resulting in similar behaviour to the PDF.

It’s quite a hard subject to explain in writing, so here is an example:

This is the PDF

And this is the HTML

  • Click here to see the PDF document and how it behaves.
  • Click here to see the outputted HTML (This example will be updated periodically as we add new features).

As you can see we have yet to fully implement the formatting for the calculated values but it’s well on it’s way!

Do you have any PDFs that make use of forms and JavaScript that would work a lot better as web pages?

Ebook Page Link

The following two tabs change content below.
Lyndon is a Developer at IDR Solutions. He currently focuses mostly on the JavaScript in the Viewer and PDF to HTML5 Converter and also the Android PDF Viewer. He gave a short talk at the GlassFish UnConference before JavaOne 2012. Outside of IDR Solutions he has a keen interest in AI and Games Programming and runs a blog that he periodically updates.

Related Posts:

lyndon

About Lyndon Armitage

Lyndon is a Developer at IDR Solutions. He currently focuses mostly on the JavaScript in the Viewer and PDF to HTML5 Converter and also the Android PDF Viewer. He gave a short talk at the GlassFish UnConference before JavaOne 2012. Outside of IDR Solutions he has a keen interest in AI and Games Programming and runs a blog that he periodically updates.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>