8

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)
      \href{http://overleaf.com}{Link 1}
    \end{textblock}
  \end{ocg}

  \begin{ocg}{}{ocg2}{0}
    \begin{textblock}{40}(1,1)
      \href{http://google.com}{Link 2}    
    \end{textblock}
  \end{ocg}

\end{document}

mouse over link after pressing button 1 mouse over link after pressing button 1

AlexG
  • 54,894

1 Answers1

9

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)
      \href{http://overleaf.com}{Link 1}
    \end{textblock}
  \end{ocg}

  \begin{ocg}{}{ocg2}{0}
    \begin{textblock}{40}(1,1)
      \href{http://google.com}{Link 2}
    \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:

enter image description here

\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 {%
  GitHub/\faGithub, StackExchange/\faStackExchange, StackOverflow/\faStackOverflow, Google/\faGoogle% 
}{\showocg{\ocgname}{\urlicon}\ }\\[1ex]
%define OCGs, within the same Radio Button Group
\foreach \ocgname / \weburl in {%
  GitHub/{https://github.com}, StackExchange/{https://stackexchange.com}, StackOverflow/{https://stackoverflow.com}, Google/{https://google.com}%
}{%
  \makebox[0pt][l]{%
  \begin{ocg}[radiobtngrp=myURLs]{\ocgname}{\ocgname}{off}
    \url{\weburl}
  \end{ocg}}%
}

\end{document}
AlexG
  • 54,894
  • 2
    One word: bravo! – AndréC Mar 05 '19 at 16:14
  • 2
    @AndréC :-) approved also for me. :-). – Sebastiano Mar 05 '19 at 16:21
  • 1
    @Ian It can be further simplified using a Radio Button Group. I'll update tomorrow. – AlexG Mar 05 '19 at 17:52
  • @IanAppelbaum Ok. Done. – AlexG Mar 06 '19 at 09:34
  • @ALexG Does your method still work with Adobe Reader? Here, using evince as a PDF viewer, the links are not correct (even if the hyperref frames are hiding/showing well). – Paul Gaborit Apr 29 '20 at 05:29
  • @Paul Unfortunately, among the PDF viewers with OCG support only Acrobat Reader correctly acknowledges the visibility of links that are placed on PDF Layers. And, as shown in the answer, it requires some preparation for the links to be associated with OCGs. The link annotation needs an /OC <ocg/ocmd reference> entry in its dictionary (while the link text as part of the page content stream is already placed on the OCG) . – AlexG Apr 29 '20 at 06:03