自动“放大” PDF 中的超文本框;无需 JavaScript

自动“放大” PDF 中的超文本框;无需 JavaScript

在以下两个问题中

@AlexG 演示了如何构造点击放大使用 javascript 的按钮。

不使用 javascript 可以做到这一点吗?

提出这一要求有以下理由:

  • 一些 PDF 查看器只是禁用了 javascript。
  • 根据 PDF 规范,可以按如下方式指定超链接的目标

    [/XYZ左边 顶部 飞涨]

    (然而,在使用 pdftex/lualatex/xelatex 时似乎禁用此功能。)

附言:

  • pdftex 答案就可以了。
  • 我只关心点击放大部分。点击缩小部分是不必要的,因为它可以通过键盘快捷键完成。

这是一个沙箱

\documentclass{beamer}

\usepackage{mwe}

\makeatletter
    % something?
\makeatother

\begin{document}
    \begin{frame}
        \includegraphics[width=1cm]{example-image-a}\par
        \includegraphics[width=1cm]{example-image-b}\par
        \includegraphics[width=1cm]{example-image-c}\par
        \includegraphics[height=1cm]{example-image-golden}\par
        \includegraphics[width=1cm]{example-image-golden-upright}\par
    \end{frame}
\end{document}

答案1

这是点击缩放框的 JavaScript 免费版本。(适用于 pdfLaTeX、LuaLaTeX、XeLaTeX、LaTeX+Dvips。)

\zoombox[dotted box line width in px, default: 1]{<contents>}

它利用“鼠标按钮按下”和“鼠标按钮弹起”事件来放大和缩小。在演讲情况下,这非常可靠,并且足以对幻灯片上的细节进行简短解释。

已使用 Acrobat Reader 和 PDF-XChange 成功测试。Foxit Reader 无法正确放大,应向开发人员报告,在 Evince 中它根本不起作用。

第一个代码示例目标beamer类文档,并应在全屏/演示模式。缩放目标位于屏幕中央按下鼠标按钮时。释放按钮时,它会缩小到整个页面视图。(这比返回第二个代码示例中使用的上一个视图更可靠。)为了获得最佳性能,文档的纵横比(beamer选项aspectratio)应与目标视频投影仪或显示器相匹配。


为了beamer课程文件:

\documentclass[
  aspectratio=169,% must match the target video projector/monitor, default: 43
  hyperref={pdfpagemode=FullScreen}
]{beamer}
\usepackage{graphicx}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  usage: \zoombox[<dotted border width in pix, default: 1>]{<content>}
%
%  * optimized version for BEAMER class:
%     - zoom target centred on screen upon zoom-in   (mouse btn press)
%     - whole-page-view upon zoom-out                (mouse btn release)
%                                       
%  * !! to be used in FULL-SCREEN mode !!
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{pdfbase,calc}

\ExplSyntaxOn
\let\pbsPdfDest\pbs_pdfdest:nnnn
\let\pbsPdfAnnot\pbs_pdfannot:nnnn
\ExplSyntaxOff
\newsavebox\zbox
\newcounter{zoom}
\newdimen\zAr\newdimen\pAr %aspect ratios of zoom target and document page
\newdimen\zwd\newdimen\zht\newdimen\zdp %intermediate and final zoom target dims

\newcommand{\zoombox}[2][1]{% [#1] (optional) sets border line width, default: 1 pixel
                            % {#2} box context
  \sbox\zbox{#2}%
  \setlength\zAr{1pt*\ratio{\wd\zbox}{\ht\zbox+\dp\zbox}}%
  \setlength\pAr{1pt*\ratio{\paperwidth}{\paperheight}}%
  \ifdim\zAr>\pAr\relax%
    \setlength\zwd{\wd\zbox}\setlength\zht{\zwd*\ratio{\paperheight}{\paperwidth}}%
    \setlength\zdp{(\zht-\ht\zbox-\dp\zbox)*\real{0.5}+\dp\zbox}%
    \setlength\zht{\zht-\zdp}%
  \else%
    \setlength\zht{\ht\zbox+\dp\zbox}%
    \setlength\zwd{\zht*\ratio{\paperwidth}{\paperheight}}%
    \setlength\zht{\ht\zbox}\setlength\zdp{\dp\zbox}%
  \fi%
  \pbsPdfDest{zb\thezoom.out}{fit}{1}{}% full-slide view for zoom-out
  \makebox[0pt][l]{\makebox[\wd\zbox][c]{% zoom-in target
    \pbsPdfDest{zb\thezoom.in}{fitr}{1}{\phantom{\vrule
      width \zwd\space height\space \zht\space depth \zdp
  }}}}%
  \makebox[0pt][l]{{\fboxsep=0.5\fboxsep\hskip-\fboxsep%
    \pbsPdfAnnot{\dimexpr\wd\zbox+2\fboxsep\relax}{%
      \dimexpr\ht\zbox+\fboxsep\relax}{%
      \dimexpr\dp\zbox+\fboxsep\relax}{%
      /Subtype/Link/Border [0 0 #1[#1]]%
      }%
    \pbsPdfAnnot{\dimexpr\wd\zbox+2\fboxsep\relax}{%
      \dimexpr\ht\zbox+\fboxsep\relax}{%
      \dimexpr\dp\zbox+\fboxsep\relax}{%
      /Subtype/Widget/FT/Btn/Ff 65536/H/N
      /T (zb\thezoom)
      /AA <<
        /D <</S/GoTo /D (zb\thezoom.in)>>
        /U <</S/GoTo /D (zb\thezoom.out)>>
        /X <</S/GoTo /D (zb\thezoom.out)>>
      >>
      }%
  }}%
  \usebox{\zbox}%
  \stepcounter{zoom}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{frame}
  1-px-border (default): \zoombox{\includegraphics[width=1cm]{example-image-a}}\quad
                         \zoombox{Hello World!}\\[2ex]
  2-px-border: \zoombox[2]{\includegraphics[width=1cm]{example-image-b}}\\[2ex]
  zero-width-border: \zoombox[0]{\includegraphics[width=1cm]{example-image-golden-upright}}\quad
  \zoombox[0]{\includegraphics[width=1cm]{example-image-golden}}
\end{frame}

\end{document}

第二个代码示例GoBack在释放鼠标按钮时执行操作(返回上一个视图)。

请注意,GoBack缩小操作在 A-Readers X 和 XI 中已损坏且无法使用,但在 AR-DC 中又恢复了。

适用于任何文档类,但稳定性较差。(PDF 查看器中保存的视图堆栈有时可能会变得混乱。)

\documentclass{article}
\usepackage{graphicx}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% usage: \zoombox[<dotted border width in pix, default: 1>]{<content>}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{pdfbase,calc}

\ExplSyntaxOn
\let\pbsPdfDest\pbs_pdfdest:nnnn
\let\pbsPdfAnnot\pbs_pdfannot:nnnn
\ExplSyntaxOff
\newsavebox\zbox
\newcounter{zoom}

\newcommand{\zoombox}[2][1]{% [#1] (optional) sets border line width, default: 1 pixel
  \leavevmode%                {#2} box context
  \sbox\zbox{#2}%
  \makebox[0pt][l]{{\fboxsep=0.5\fboxsep\hskip-\fboxsep%
    \pbsPdfAnnot{\dimexpr\wd\zbox+2\fboxsep\relax}{%
      \dimexpr\ht\zbox+\fboxsep\relax}{%
      \dimexpr\dp\zbox+\fboxsep\relax}{%
      /Subtype/Link/A <</S/GoTo /D (zb\thezoom)>>/Border [0 0 #1[#1]]%
      }%
    \pbsPdfAnnot{\dimexpr\wd\zbox+2\fboxsep\relax}{%
      \dimexpr\ht\zbox+\fboxsep\relax}{%
      \dimexpr\dp\zbox+\fboxsep\relax}{%
      /Subtype/Widget/FT/Btn/Ff 65536/H/N
      /T (zb\thezoom)/TU (Press mouse button to zoom in.)
      /AA <<
        /D <</S/GoTo /D (zb\thezoom)>>
        /U <</S/Named /N/GoBack>>
        /X <</S/Named /N/GoBack>>
      >>
      }%
  }}%
  \pbsPdfDest{zb\thezoom}{fitr}{1}{\usebox{\zbox}}%
  \stepcounter{zoom}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}\parindent=0pt

1-px-border (default): \zoombox{\includegraphics[width=1cm]{example-image-a}}\quad
2-px-border: \zoombox[2]{\includegraphics[width=1cm]{example-image-b}}\\[2ex]
zero-width-border: \zoombox[0]{\includegraphics[width=1cm]{example-image-golden-upright}}
\zoombox[0]{\strut Hello World!}

\end{document}

相关内容