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.

Hello World!

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
  • 1
    @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.


\begin{ocg}{Orange Colframe}{orange}{true}
    Hello World!
\begin{ocg}{Red Colframe}{red}{false}%
    Hello World!

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

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


var myToggle = 0;
function mySelect() {
    if  (myToggle) {
        myToggle = 0;
        anim["myAnimation1"].frameNum = 0;
    } else {
        myToggle = 1;
        anim["myAnimation1"].frameNum = 1;
    this.getField("myTextBox").value += 1;


\PushButton[name=myButton1,borderwidth=0px,bordercolor={1 1 1},onclick={mySelect(1);}]{
\TextField[name=myTextBox,borderwidth=1px,bordercolor={0 0 0},readonly=true]{}


Instead of the package ocg, this uses animate.

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.