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

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


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,




This is a paragraph of text.
This is some text which can be hidden.
And this is some more text which cannot be hidden.

  /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}

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

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



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

More information about the macostex-archives mailing list