3

PDFs can be used to manage javascript, such as the packages ocgtools, ocg-p, ocgx or ocgx2, which allow you to create interactive and clickable PDFs. The hyperref package allows you to insert URLs or HTML anchors.

On the model of these packages, is it possible to create an interactive PDF file with a "go" button that when you click on it generates a random number (with javascript)?

AndréC
  • 24,137

1 Answers1

6

Sure, e.g. with acrotex:

\documentclass{article}
\usepackage[pdftex]{eforms}
\begin{document}

\pushButton[\CA{Push me}\A{\JS{app.alert(Math.random())}}]{jsEx}{3cm}{12pt}

\end{document}

enter image description here

You can sent values to other fields:

\documentclass{article}
\usepackage[pdftex]{eforms}
\begin{document}

\textField{myField}{5cm}{12pt}

\pushButton[\CA{Push me}\A{\JS{this.getField("myField").value=Math.random();}}]{myButton}{3cm}{12pt}\\

\end{document}

enter image description here

Ulrike Fischer
  • 327,261
  • Is it possible that these random numbers are not written in a javascript window but dynamically on the PDF itself? – AndréC Sep 11 '17 at 16:19
  • 1
    You can sent them to textfields. I added an example – Ulrike Fischer Sep 11 '17 at 16:43
  • Sadly, not in TeX Live :( – Fran Sep 11 '17 at 17:12
  • 1
    @Fran: Easy to install. See the comment below this question https://tex.stackexchange.com/questions/385577/how-to-have-form-options-in-table-cells – Ulrike Fischer Sep 11 '17 at 17:20
  • @UlrikeFische Very interesting. Is it possible to fill this textfield with a random numerical expression like -3+2= where the + - signs and 2;3 numbers are randomly generated? – AndréC Sep 11 '17 at 18:09
  • 2
    Sure, write a javascript function that generates your string and then use it -- but if it gets longer you should put it in an external file, see eformman.pdf. – Ulrike Fischer Sep 11 '17 at 18:49