![隐藏 PDF 中的可搜索内容](https://linux22.com/image/374529/%E9%9A%90%E8%97%8F%20PDF%20%E4%B8%AD%E7%9A%84%E5%8F%AF%E6%90%9C%E7%B4%A2%E5%86%85%E5%AE%B9.png)
有没有办法使用 LaTeX 在 PDF 中隐藏可搜索的文本?例如,是否可以嵌入可搜索但不可见的 ID?
答案1
使用该accsupp
软件包后,文档中会显示“此处无可查看的内容”,但可以搜索“隐藏消息”。
\documentclass{article}
\usepackage{accsupp,lipsum}
\begin{document}
\lipsum[1]
\BeginAccSupp{method=escape,ActualText=Hidden message}%
Nothing to see here.
\EndAccSupp{}%
\lipsum[2]
\end{document}
该文件的可搜索内容如下:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cura-
bitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
Hidden message Nam dui ligula, fringilla a, euismod sodales, sollici-
tudin vel, wisi. Morbi auctor lorem non justo. Nam lacus libero, pretium at,
lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan biben-
dum, erat ligula aliquet magna, vitae ornare odio metus a mi. Morbi ac orci
et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a
nulla. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur
ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis.
Pellentesque cursus luctus mauris.
答案2
无需额外封装,通过将文本渲染模式设置为3
(不描边也不填充):
\documentclass{article}
\begin{document}
Hidden text follows, but you won't see it. \makebox[0pt][l]{\pdfliteral page {q 3 Tr}ID:3.1415\pdfliteral page {Q}}This comes after.
\end{document}
刚刚看到{xetex}
标签。因此,下面的代码可与pdflatex
、lualatex
、中的任何一个一起使用xelatex
。
此外,与 Evince 不同,Acrobat Reader 似乎会将隐藏文本与覆盖文本混杂在一起,最终导致无法搜索。为了解决这个问题,我们将隐藏文本缩小 100000 倍,并将其移到页面右侧。如果我们用鼠标选择普通文本,这也会阻止隐藏文本显示出来:
\documentclass{article}
\usepackage{pdfbase}
\ExplSyntaxOn
\newcommand\hiddenText[1]{
\makebox[0pt][l]{\hspace{\paperwidth}\pbs_literal:nn{page}{q~0.00001~0~0~0.00001~0~0~cm~3~Tr}#1\pbs_literal:nn{page}{Q}}
}
\ExplSyntaxOff
\begin{document}
Hidden text follows, but you won't see it.\hiddenText{ID:3.1415} This comes after.
\end{document}