I'm trying to create some dynamic forms in my PDF file with JavaScript. I am using xelatex to compile it. Something in the way the insdljs package is inserting the JavaScript is making it not work.

Here is a sample file:


\begin{insDLJS}{hello}{Hello, World}
var done=0;

function hello() 
    if (!done) {
        // check if we are done
        app.alert("Hello, World!");


\OpenAction{/S /JavaScript /JS (hello();)}

If you remove the useless comment and compile with xelatex, then open the compiled PDF in Acrobat Reader, you get a “Hello, World!” alert. But with the comment you get no alert.

Looking at the actual JavaScript (you need Acrobat Pro to do this, I think) that gets inserted into the PDF, I see that all the newlines are removed. It becomes

var done=0;function hello(){if (!done) {// check if we are done done=1;app.alert("Hello, World!");}}

In JavaScript, the double slash // indicates that the rest of the line is a comment. When all the newlines are stripped, half of the code is ignored! Acrobat's JavaScript console gives error messages consistent with this (it doesn't find the } ending the hello() function block).

So here are my workarounds:

  1. Use no comments. Bad Idea.
  2. Use only inline comments /* ... */ OK, not great.
  3. Coerce newlines to appear. After all, aside from comments, there are probably good reasons to have newlines.

Any ideas on implementing #3? The eforms manual says you can insert newlines with \n, but this seems to not be happening. If I do that I get instead

var done=0;function hello(){if (!done) {// check if we are done \ndone=1;app.alert("Hello, World!");}}

(the \n just went straight into the document level JavaScript).


If works if you add a TeX-comment:

    %// check if we are done

But from the documentation I would have expected that a // works too, so I suggest that you write the author.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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