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

  • 1
    +1 Very good question! – Paul Gaborit Mar 5 at 15:42
  • 1
    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
  • 1
    @AndréC Updated w/ mouseover screenshots (pointer not visible) – Ian Appelbaum Mar 5 at 17:19
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}
  • 2
    One word: bravo! – AndréC Mar 5 at 16:14
  • 2
    @AndréC :-) approved also for me. :-). – Sebastiano Mar 5 at 16:21
  • 1
    @Ian It can be further simplified using a Radio Button Group. I'll update tomorrow. – AlexG Mar 5 at 17:52
  • @IanAppelbaum Ok. Done. – AlexG Mar 6 at 9:34

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.