# How can (easily) superimpose different text over odd/even pages of an included PDF file?

I have a series of pdf files that I'm trying to combine. That much I can do without any problem.

What I also need to do is add a small amount of text to the top 'outside' corner of each page, essentially identifying the original document that each page came from. I've managed so far to put the text in the upper left corner of each page - there are other questions on Stack Exchange which address that.

However, none of them deal with putting the text in a different position on odd and even pages - i.e. the upper left corner of even pages, and the upper right corner of odd pages. How can this be achieved?

\documentclass{article}

\usepackage[a4paper]{geometry}
\usepackage{pdfpages}
\usepackage{tikz}
\renewcommand{\contentsname}{My combined file}
\setlength\parindent{0pt}

\begin{document}

\includepdf[pages=-]{cover.pdf}
\includepdf[pages=2-4,pagecommand={\begin{tikzpicture}[remember picture, overlay]
\node[right] at (-2, 2.5) {\LARGE{doc1}};
\end{tikzpicture}}]{doc1.pdf}
% More files included here
\end{document}


For extra credit, is there a way to (automatically) use the filename of the included document as the inserted text?

This uses the everypage package to accomplish the task. The syntax is

\atxy{this page X}{alternate page X}{Y}{content}

If invoked on an odd page, "this page X" is the x-location of the content on odd pages, while "alternate page X" is the x-location of the content on even pages. Vice-versa if invoked on an even page.

One updates the content with the macro \updateText{}

Here is my MWE. It has been EDITED to allow the process to be invoked after page 1, if desired (though MWE shows it invoked on p.1).

\documentclass{article}
\usepackage{everypage}
\usepackage{lipsum}
% THESE ARE LaTeX DEFAULTS; CAN CHANGE IF NEEDED.
\def\PageTopMargin{1in}
\def\PageLeftMargin{1in}
\def\localX{0in}
\def\ThisX{0in}
\def\AltX{0in}
\newcommand\atxy[4]{%
\def\XYtext{#4}
\gdef\ThisX{#1}\gdef\AltX{#2}%
\raisebox{\dimexpr\PageTopMargin+\voffset-#3\relax}{\XYtext}}}}
\newcommand\updateText[1]{\gdef\XYtext{#1}}
\begin{document}
\atxy{7in}{0.2in}{0.5in}{\parbox[t]{1in}{\Huge This is my initial text}}
\lipsum[1-7]
\lipsum[8-35]
\end{document}


\documentclass{article}

\usepackage[a4paper]{geometry}
\usepackage{pdfpages}
\usepackage{ifoddpage}
\usepackage{background}

\newcommand{\mypdfinclude}[2][]{%
\backgroundsetup{%
placement=top,
angle=0,
nodeanchor=north east,
color=red,
scale=1,
contents={%
\checkoddpage\ifoddpage%
\begin{tikzpicture}[remember picture,overlay]
\node[text width=2in,anchor=north east,align=right,font=\Large] at
([shift={(-1cm,-1cm)}]current page.north east) {#2};
\end{tikzpicture}
\else
\begin{tikzpicture}[remember picture,overlay]
\node[text width=2in,anchor=north west,font=\Large] at ([shift={(1cm,-1cm)}]current
page.north west) {#2};
\end{tikzpicture}
\fi
}
}
\includepdf[#1]{#2}
}
\begin{document}
\mypdfinclude[pages=2-4]{pgfmanual.pdf}
\end{document}


Here is another suggestion using the package scrlayer to define a special pagestyle for included pdfs.

\documentclass[a4paper]{book}

\usepackage{pdfpages}
\usepackage{xcolor}

\usepackage{scrlayer}

\newcommand{\pdfpagestext}{}
\colorlet{pdfpagescolor}{purple}

% define new layers
\DeclareNewLayer[
foreground,
oddpage,
voffset=1cm,% distance from top
hoffset=1cm,% distance from left
width=\dimexpr\paperwidth-2cm\relax,% width of the layer
height=2cm,% height of the layer
contents={\parbox{\layerwidth}{\raggedleft\textcolor{pdfpagescolor}{\pdfpagestext}}}
]{pdfpages.odd}
\DeclareNewLayer[
clone=pdfpages.odd,
evenpage,
contents={\parbox{\layerwidth}{\raggedright\textcolor{pdfpagescolor}{\pdfpagestext}}}
]{pdfpages.even}

% define new pagestyle
\DeclareNewPageStyleByLayers{pdfpages}{pdfpages.odd,pdfpages.even}

\newcommand*\myincludepdf[2][]{%
\renewcommand\pdfpagestext{#2}%
\includepdf[pagecommand=\thispagestyle{pdfpages},#1]{#2}%
}

\begin{document}
\myincludepdf[pages=1-3]{dummy.pdf}
\end{document}


It works also if you use an oneside layout. Then the text is on every page at the right (=outer) margin automatically.

\documentclass[a4paper,oneside]{book}