# Can you influence any PDF element using JavaScript?

Using the package insDLJS, one can add JavaScript to PDF's generated by LaTeX. To reference a specific element, one can use this.getField("myElementName"). Can I give any element in LaTeX a field name? And if so, how?

For example, imagine I have a coloured textbox using the package tcolorbox, of which I want to influence the border color (colframe) using JavaScript.

\begin{tcolorbox}[width=25mm,colback={green},colframe={orange}]
Hello World!
\end{tcolorbox}


If the textbox had a reference name, the code might be something like this.getField("myTextBox").borderColor = color.red;. But how can I give this textbox the name myTextBox? And, secondly, how can I figure out which aspects of the box can be influenced (the .borderColor part)?

• For stuff like this you need the a format with a DOM. PDF is not among these formats. Use XML or HTML5 instead. – Henri Menke Dec 22 '16 at 10:18
• @HenriMenke : A-Reader, A-Acrobat, Foxit, PDF-XChange do have a DOM representation of PDF objects within their JavaScript engines, but it is different from the HTML DOM. A lot is possible with JS inside the mentioned PDF viewers, making the question perfectly on-topic. – AlexG Dec 22 '16 at 10:41

Text and graphics which are the result of stroking and filling operations on a PDF document page cannot be accessed or modified using JavaScript.

To a limited extent, PDF Layers (officially named Optional Content Groups, OCGs) can be used to mutually replace, hide or show page content in a PDF. But this does not necessarily involve JavaScript (see example below).

JavaScript is used to access and to define the behaviour of interactive elements (PDF Annotations, officially), such as button fields, forms, embedded multimedia, links etc., or to get global document properties.

A number of trigger events (Page Open, Close, Mouse Over, Down, Up, etc.) can be associated with actions which may or may not be defined using JavaScript.

The PDF specification and the JavaScript documentation define what is possible. But note that the implementation of the documented standards largely differ between PDF viewers. The reference PDF viewers are A-Reader and A-Acrobat.

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{ocgx2}

\begin{document}
\begin{ocg}{Orange Colframe}{orange}{true}
\makebox[0pt][l]{%
\begin{tcolorbox}[width=25mm,colback={green},colframe={orange}]
Hello World!
\end{tcolorbox}%
}%
\end{ocg}%
\begin{ocg}{Red Colframe}{red}{false}%
\makebox[0pt][l]{%
\begin{tcolorbox}[width=25mm,colback={green},colframe={red}]
Hello World!
\end{tcolorbox}%
}%
\end{ocg}

\switchocg{orange red}{\fbox{Toggle}} Colframe colour.
\end{document}


Based on AlexG's suggestion of using PDF Layers, I made the following example which combines the toggling of layers with JavaScript functionality:

\documentclass[a4paper]{article}
\usepackage{animate}
\usepackage{tcolorbox}
\usepackage[pdftex]{insdljs}

\begin{insDLJS}[test]{test}{JavaScript}
var myToggle = 0;
function mySelect() {
if  (myToggle) {
myToggle = 0;
} else {
myToggle = 1;
}
this.getField("myTextBox").value += 1;
}
\end{insDLJS}

\begin{document}

\begin{Form}
\PushButton[name=myButton1,borderwidth=0px,bordercolor={1 1 1},onclick={mySelect(1);}]{
\begin{animateinline}[nomouse,step,label=myAnimation1]{0}
\begin{tcolorbox}[width=20mm,colback={green},colframe={orange}]
On
\end{tcolorbox}
\newframe
\begin{tcolorbox}[width=20mm,colback={yellow},colframe={red}]
Off
\end{tcolorbox}
\end{animateinline}}
\hspace{4mm}

Instead of the package ocg, this uses animate.