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

Leave a Reply

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

Human? Please fill this out: * Time limit is exhausted. Please reload CAPTCHA.

  1. 数声风笛离亭晚,我想潇湘君想秦! Avatar

    A simple and useful method! This is just what i need!

  2. Thejaswi Raya Avatar
    Thejaswi Raya

    Awesome! Looks so simple, however I was not able to get it to work. How did you compile it? I copied the code and I compiled it using “latex” command and then ran dvipdfm on the the dvi file, but the pdf generated did not throw any popup. Can you please help me out with this? It is very important to me.

    1. Jan-Philip Gehrcke Avatar

      Hey, as I wrote in the article, I used PDFLaTeX to compile the source. You definitely have to use this, too!

    2. 数声风笛离亭晚,我想潇湘君想秦! Avatar

      After a simple looking at the official document, I found that replacing “\usepackage[pdftex]{insdljs}” with “\usepackage[dvips]{insdljs}” will make “latex+dvips+ps2pdf” work!