# How to apply different background images for each page using PerlTeX

How do I apply a different background image for each page? I have an array of pdf's to apply. I have used the watermark package. However, I do not like the idea of manually adding a different watermark for hundreds of pages, I prefer the idea of using perltex --love Perl! I guess the question is really, how do I access each page's data structure, just before it is written to a file?

Here is the perl code I have created so far:

\def\numbpgs{\getpagerefnumber{LastPage}}
\def\imgs{1 2 3 ... x}
\bkgrndimages{\numbpgs}{\imgs}

\perlnewcommand{\bkgrndimages}[2]{

my $numpgs =$_[0];
my @imgarray = split / /, $_[1]; my$return = ();

my $c = 1; for (0 ..$numpgs) {
# Here I wish to modify each page's background to
$return .= WATERMARK[LATEXPAGE[$c]] $imgarray[$c]\.pdf;
$c++; } return$return;
}

-
Welcome to TeX.sx. If you select your code and click {} button or press Ctrl-K the code is displayed correctly. (or you can introduce 4 whitespace characters at the beginning of each line manually) –  percusse May 28 '12 at 21:59
Did you look at the package atbegshi? –  egreg May 28 '12 at 22:31

I think your love for Perl is making you take the wrong path.

how do I access each page's data structure, just before it is written to a file?

This is normally done using one of the packages atbegshi, bophook, everyshi, eso-pic or \background The packages will enable you to hook into LaTeX's output routine and add a picture or text before shipment.

If all the documents use the same class, it is also possible to hook into every page using the header. This is my preferred way and here is a MWE as to how to do it.

\documentclass{scrbook}
\usepackage{lipsum,xcolor}
%\IfFileExists{changepage.sty}{%
%  \PassOptionsToPackage{strict}{changepage}
%  \RequirePackage{changepage}
%  }{}
\IfFileExists{rotating.sty}{\RequirePackage{rotating}}{}
\makeatletter
\def\even@samplepage{%
\begin{picture}(0,0)
\put(\Xeven,\Yeven){\turnbox{90}{\Huge \textcolor{\watermark@textcolor}{\watermark@text}}}
\end{picture}
}
%% Define a macro to print SAMPLE PAGE IN THE MARGIN
\def\odd@samplepage{%
\begin{picture}(0,0)
\put(\Xodd,\Yodd){\turnbox{90}{\Huge \textcolor{\watermark@textcolor}{\watermark@text}}}
\end{picture}
}
%  Define the watermark words
\def\watermarktext#1{\gdef\watermark@text{\fontfamily{phv}\selectfont#1}}
\def\watermarktextcolor#1{\gdef\watermark@textcolor{#1}}
\def\ps@samplepage{\let\@mkboth\@gobbletwo
%%
%% We define two macros to position the watermark on the page
\def\Xodd{500}
\def\Xeven{-70}\def\Yeven{-810}
\def\Yeven{-\expandafter\strip@pt\textheight}
\let\Yodd\Yeven
\watermarktext{SAMPLE PAGE}
\watermarktextcolor{blue}
\makeatother
\begin{document}
\pagestyle{samplepage}
\lipsum


\end{document}

-