[OS X TeX] Toggling layers in PDFs via embedded JavaScript

J. McKenzie Alexander jalex at lse.ac.uk
Wed Feb 13 07:33:46 EST 2013


Hello,

I realise this is a somewhat esoteric question, but I'm hoping someone might be able to help. I include below a short LaTeX file which uses the ocg-p package to put some text in a paragraph on a separate layer. I then embed some JavaScript in the pdf to define a function for toggling the state of a named layer. I then create a push button and a check box via some standard macros from hyperref, and tell both to call the JavaScript function toggleLayer when clicked..

What's curious is that clicking on the push button successfully toggles the layer, but clicking on the check box doesn't! Any ideas as to what might be going on here would be greatly appreciated!  (Please note that you'll have to view the PDF in Adobe Reader with JavaScript enabled, as the standard Preview app doesn't support JavaScript.)

Many thanks,

Jason


\documentclass[12pt,a4paper]{article}
\usepackage{ocg-p}
\usepackage[pdftex]{hyperref}

\begin{document} 

This is a paragraph of text.
\begin{ocg}{hidden}{ochiddenid}{1}
This is some text which can be hidden.
\end{ocg}
And this is some more text which cannot be hidden.

\immediate\pdfobj{<<
  /S/JavaScript/JS (toggleLayer = function(arg){
    var docOCGs = this.getOCGs();
    for (var x=0; x < docOCGs.length; x++) 
	{
		if(docOCGs[x].name == arg)
		{
			docOCGs[x].state = !docOCGs[x].state;
		}
	}
	})
>>}
\edef\toggleLayer{\the\pdflastobj\space 0 R}

\pdfnames{
  /JavaScript <<
    %name tree mapping doc. level JavaScript functions to object numbers
    /Names [(toggleLayer) \toggleLayer ]
    /Limits [(toggleLayer)] %first and last in alphab. order
  >>
}


\begin{Form}
\PushButton[name=abutton,onclick={toggleLayer('hidden');}]{Toggle Text}

\CheckBox[name=acheck,checked,onclick={toggleLayer('hidden');}]{Text}
\end{Form}

\end{document}




Please access the attached hyperlink for an important electronic communications disclaimer: http://lse.ac.uk/emailDisclaimer



More information about the MacOSX-TeX mailing list