# How to invert a PDF using OCG/Layers?

I am creating a PDF that is designed for viewing on Screens/Tablets, and uses white Text on black background. Since some readers might want to print it, and I want to avoid to create a second PDF, I would like to include a Print View as Layer in the PDF.

How can I make (xe)latex to render the same Text twice above each other, with a background image in between, so I can use the upper text layer and image as one PDF-Layer to disable it?

So in the PDF, it would be the following order of layers (read it bottom-to-top):

WHITE TEXT       \
> OCG Layer #1
BLACK BACKGROUND /

BLACK TEXT       \
> regular PDF
WHITE BACKGROUND /


Now if the reader hides Layer #1 in the PDF Reader, the printerfriendly Version below it becomes visible.

-
Just a minor note, most PDF viewers can invert the rendered image. Acrobat and Evince can at least. – alfC Nov 4 '15 at 18:40
Yes, but my situation is a bit more complex with artwork etc, so it is more than "just" inverting all colors. – TeXter Nov 6 '15 at 17:48

This example produces two different versions of the readily typeset page box and places them on separate OCG layers.

Both layers are added to the radio button group "reverse video" to ensure that only one of them can be enabled at a time.

Moreover, we forbid the white-on-black version to be printed in order to prevent the user from accidentally wasting precious ink.

This example requires package ocgx2, v. 10 [2015/11/02].

\documentclass{article}

\usepackage[a5paper]{geometry}
\usepackage{kantlipsum}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{ocgx2}[2015/11/02] %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
\AtBeginShipout{%
\setbox\AtBeginShipoutBox=\hbox{%
%normal & print layer
\begin{ocg}[
printocg=always,
]{normal and print}{normal}{off}%
\makebox[0pt][l]{%
\raisebox{1in}{\copy\AtBeginShipoutBox}%
}%
\end{ocg}%
%layer for tablets & screens
\begin{ocg}[
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\AtBeginShipoutBox}}%
\end{ocg}%
}%
}

%proof of concept: --> repeat the following with
\let\@oddfootOrig\@oddfoot%
\gdef\@oddfoot{%
\makebox[\textwidth][c]{%
\begin{ocg}[
printocg=always,
]{normal and print}{normal}{off}%
\makebox[0pt][c]{\@oddfootOrig}%
\end{ocg}%
\begin{ocg}[
printocg=never,
]{for screens/tablets}{tablets}{on}%
\makebox[0pt][c]{{\color{white}\@oddfootOrig}}%
\end{ocg}%
}%
}%
}

\let\pagestyleOrig\pagestyle