OCGX 和 hyperref:重疊連結的可見性和可點擊性

OCGX 和 hyperref:重疊連結的可見性和可點擊性

我用來ocgx在單擊 PDF 中的連結時顯示不同的文字區塊(在同一頁面的同一區域)。

然而,每個文字區塊都有自己的\href鏈接,即使沒有文字可見,這些連結也會在頁面上重疊(隱藏先前定義的 OCG)(!)。是否可以ocg不僅切換可見性,還可以切換圖層順序?

在下面的 MWE 中,請注意使用按鈕 1,2 切換文字區塊可見性才不是切換 href 連結的存在,並且僅存在連結“google.com”,即使兩個 ocg 都不可見(我的螢幕截圖中未捕獲滑鼠指標)...

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

按下按鈕 1 後將滑鼠懸停在連結上 按下按鈕 1 後將滑鼠懸停在連結上

答案1

這可以透過包來完成ocgx2加上hyperref一些修補,使其能夠識別 PDF 圖層。

但請注意,只有 Acrobat Reader 尊重放置在 PDF 圖層上的連結的可見性:

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

\actionsocg如果 PDF 層數增加,建立相互隱藏 PDF 層的開關很快就會變得很麻煩。為了簡化,pkgocgx2允許 OCG 分組為單選按鈕組。屬於同一單選按鈕組的所有 OCG 一次只能啟用一個,從而自動隱藏先前可見的 OCG。使用選項將 OCG 新增到單選按鈕組radiobtngrp=...。將 OCG 分組為單選按鈕群組可讓我們使用簡單的\showocg命令為每一層建立開關:

在此輸入影像描述

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

相關內容