Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

\documentclass{article}
\usepackage[dvipdfm]{eforms}

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

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

\end{insDLJS}

\OpenAction{/S /JavaScript /JS (hello();)}
\begin{document}
Hello,world!
\end{document}

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).

share|improve this question
add comment

1 Answer 1

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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