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:
- Use no comments. Bad Idea.
- Use only inline comments
/* ... */OK, not great. - 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).