Javascript in PDF using LaTeX

Yesterday, I tried to measure the startup time of Adobe Reader (AR) on Windows and wondered if I could create a PDF file triggering the AR application exit on load of itself. For some hours, I wondered how the hell to include Javascript in a PDF document without having to use Acrobat.

I found a neat way to include Javascript in a PDF file: using LaTeX! Look at this piece of code:

\documentclass{article}
\usepackage[pdftex]{insdljs}
 
\OpenAction{\JS{%
app.alert("HALLO WELT");
}}
 
\begin{document}
THIS DOCUMENT IS A DOCUMENT
\end{document}

I compiled this using PDFLaTeX, yielding the following result:
js_in_pdf_test.pdf

The insdljs package is providing the \OpenAction command, which lets you define Javascript code executed on loading the PDF document at page one. And much more is possible. Together with some other packages collected in the AcroTeX [1] package, you can create interactive PDF documents using LaTeX, as partly presented in [2]. An entire documentation for creating such documents is provided in [3]. The Javascript API you can use for the Adobe Reader is documented in [4]. Unfortunately, there is no way to close the parent application using Javascript, so this whole approach was not useful for my initial problem. Anyway, I’ve learned something.

References:

[1] http://www.ctan.org/tex-archive/help/Catalogue/entries/acrotex.html
[2] http://www.tug.org/TUGboat/Articles/tb22-3/tb72story.pdf
[3] http://www.ctan.org/tex-archive/macros/latex/contrib/acrotex/doc/eformman.pdf
[4] http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JavaScript_SectionPage.70.1.html