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:

app.alert("HALLO WELT");

I compiled this using PDFLaTeX, yielding the following result:

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.



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!