我正在创建一个 PDF,该 PDF 专为在屏幕/平板电脑上查看而设计,并在黑色背景上使用白色文本。由于有些读者可能想要打印它,而我想避免创建第二个 PDF,所以我想在 PDF 中包含一个打印视图作为图层。
我怎样才能让(xe)latex 将相同的文本两次渲染在一起,并在中间放置一个背景图像,这样我就可以使用上面的文本层和图像作为一个 PDF 层来禁用它?
因此在 PDF 中,图层顺序如下(从下到上阅读):
WHITE TEXT \
> OCG Layer #1
BLACK BACKGROUND /
BLACK TEXT \
> regular PDF
WHITE BACKGROUND /
现在,如果阅读器隐藏 PDF 阅读器中的第 1 层,则其下方的打印版本将变为可见。
答案1
此示例生成两个不同版本的已排版页面框并将它们放置在不同的 OCG 层上。
两个图层均添加到单选按钮组“反向视频”中,以确保每次只能启用其中一个。
此外,我们禁止打印黑底白字版本,以防止用户意外浪费宝贵的墨水。(尽管只有 AcroRead 和 Chrome/Chromium 内置 PDF 查看器才允许这样做。)
此示例需要相当新且最新的 TeX 安装,例如 TeXLive-2020:
\documentclass{article}
\usepackage[a5paper]{geometry}
\usepackage{kantlipsum}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{ocgx2} %pdf layers
\usepackage{color}
\makeatletter
%make a copy from each ready page and repeat it with white
%textcolour on black background, put both versions on different layers
\AddToHook{shipout/before}{%
\setbox\ShipoutBox=\hbox{%
%normal & print layer
\begin{ocg}[
radiobtngrp=reverse video,
printocg=always,
]{normal and print}{normal}{off}%
\makebox[0pt][l]{%
\raisebox{1in}{\copy\ShipoutBox}%
}%
\end{ocg}%
%layer for tablets & screens
\begin{ocg}[
radiobtngrp=reverse video,
printocg=never,
]{for screens/tablets}{tablets}{on}%
\makebox[0pt][l]{%
\hspace{-1in}%
\raisebox{0pt}[\dimexpr\height-1in\relax]{%
\rule{\paperwidth}{\paperheight}}}%
\raisebox{1in}{{\color{white}\box\ShipoutBox}}%
\end{ocg}%
}%
}
\def\redefFooterAndHeader{%
%proof of concept: --> repeat the following with
%\@evenfoot, \@oddhead, \@evenhead
\let\@oddfootOrig\@oddfoot%
\gdef\@oddfoot{%
\makebox[\textwidth][c]{%
\begin{ocg}[
radiobtngrp=reverse video,
printocg=always,
]{normal and print}{normal}{off}%
\makebox[0pt][c]{\@oddfootOrig}%
\end{ocg}%
\begin{ocg}[
radiobtngrp=reverse video,
printocg=never,
]{for screens/tablets}{tablets}{on}%
\makebox[0pt][c]{{\color{white}\@oddfootOrig}}%
\end{ocg}%
}%
}%
}
\let\pagestyleOrig\pagestyle
\def\pagestyle#1{\pagestyleOrig{#1}\redefFooterAndHeader}%
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}\pagestyle{plain}
\kant[1-6]
\end{document}