# OCGX and hyperref: visibility and clickability of overlapping links

I am using ocgx to display different text blocks (in the same area of the same page) upon clicking links in the PDF.

However, each of these textblocks has its own \href links, and these links overlap on the page (concealing earlier-defined OCGs) even when no text is visible(!). Is it possible to make an ocg toggle not just visibility, but also layer ordering?

In the MWE below, note that toggling the textblock visibility with buttons 1,2 does not toggle the presence of href links, and only the link "google.com" is present, even when neither ocg is visible (mouse pointer not captured in my screenshots)....

\documentclass{article}
\usepackage{ocgx}
\usepackage{hyperref}

\usepackage{textpos}
\setlength{\TPHorizModule}{12pt}
\setlength{\TPVertModule}{12pt}

\begin{document}
\actionsocg{ocg1}{}{ocg2}{{\color{blue}{button 1}}}$|$ %toggle ocg1, hide ocg2
\actionsocg{ocg2}{}{ocg1}{{\color{blue}{button 2}}}%toggle ocg2, hide ocg1

%define OCGs:
\begin{ocg}{}{ocg1}{0}
\begin{textblock}{40}(1,1)
\end{textblock}
\end{ocg}

\begin{ocg}{}{ocg2}{0}
\begin{textblock}{40}(1,1)
\end{textblock}
\end{ocg}

\end{document}


• +1 Very good question! – Paul Gaborit Mar 5 at 15:42
• – Paul Gaborit Mar 5 at 15:43
• The question is certainly a good question since Paul Gaborit, ocgx maintainer, says so. However, I did not understand the problem. Can you be more explicit? – AndréC Mar 5 at 15:47
• @AndréC Updated w/ mouseover screenshots (pointer not visible) – Ian Appelbaum Mar 5 at 17:19

This can be done with package ocgx2 plus patching hyperref a bit to make it PDF layer-aware.

Note however that only Acrobat Reader respects the visibility of links that are placed on PDF layers:

\documentclass{article}

\usepackage{ocgx2}
\usepackage{hyperref}
%patch hyperref to make PDF Annotations PDF-Layer-(OCG)-aware
\makeatletter
\let\Hy@setpdfborderOrig\Hy@setpdfborder
\def\Hy@setpdfborder{\ocgbase@insert@oc\Hy@setpdfborderOrig}%
\makeatother

\usepackage{textpos}
\setlength{\TPHorizModule}{12pt}
\setlength{\TPVertModule}{12pt}

\begin{document}
\actionsocg{ocg1}{}{ocg2}{{\color{blue}{button 1}}}$|$ %toggle ocg1, hide ocg2
\actionsocg{ocg2}{}{ocg1}{{\color{blue}{button 2}}}%toggle ocg2, hide ocg1

%define OCGs:
\begin{ocg}{}{ocg1}{0}
\begin{textblock}{40}(1,1)
\end{textblock}
\end{ocg}

\begin{ocg}{}{ocg2}{0}
\begin{textblock}{40}(1,1)
\end{textblock}
\end{ocg}

\end{document}


Creating switches for mutually concealing PDF layers by means of \actionsocg quickly gets cumbersome if the number of PDF layers increases. In order to simplify, pkg ocgx2 allows OCGs to be grouped into Radio Button Groups. From all OCGs belonging to the same Radio Button Group only one can be enabled at a time, automatically hiding the previously visible OCG. An OCG is added to a Radio Button Group with option radiobtngrp=.... Grouping OCGs into a Radio Button Group allows us to use the simple \showocg command to create switches for every layer:

\documentclass{article}

\usepackage{ocgx2}
\usepackage{hyperref}
%patch hyperref to make PDF Annotations PDF-Layer-(OCG)-aware
\makeatletter
\let\Hy@setpdfborderOrig\Hy@setpdfborder
\def\Hy@setpdfborder{\ocgbase@insert@oc\Hy@setpdfborderOrig}%
\makeatother

\usepackage{fontawesome,pgffor}
\parindent=0pt

\begin{document}

\foreach \ocgname / \urlicon in {%
}{\showocg{\ocgname}{\urlicon}\ }\\[1ex]
%define OCGs, within the same Radio Button Group
\foreach \ocgname / \weburl in {%