PDF, Copy-Paste – Preventing Text Selection in Headers and Footers

accsuppcopy/pastepdf

I'm looking for a solution that will allow me to prevent some parts of the final pdf file (header, footer) from being selected and copied. It is to allow easier quoting of longer parts of texts from documents that will be published.

Best Answer

Based on this answer, you could use accsupp to define a \squelch command:

\documentclass{article}
\usepackage{accsupp}
\DeclareRobustCommand\squelch[1]{%
    \BeginAccSupp{method=plain,ActualText={}}#1\EndAccSupp{}}

\begin{document}
This text is selectable \squelch{but this text isn't}.
\end{document}

The squelched text cannot be highlighted in Acrobat, and copying the whole line gives this:

This text is selectable .

It's easy to combine this with fancyhdr:

\usepackage{fancyhdr}
\fancypagestyle{plain}{%
    \fancyhf{}%
    \fancyfoot[C]{\squelch{\thepage}}%
    \renewcommand{\headrulewidth}{0pt}%
    \renewcommand{\footrulewidth}{0pt}%
}
\pagestyle{plain}

You could also define a variant \squelchstyle for use with KOMA-Script's \addtokomafont:

\def\squelchstyle{%
    \BeginAccSupp{method=plain,ActualText={}}%
    \aftergroup\aftersquelchstyle}
\def\aftersquelchstyle{\EndAccSupp{}}
\addtokomafont{pagenumber}{\squelchstyle}
Related Question