Code: Select all
\includepdf[pages=1], pagecommand={\doStuffOnPageOne}]{file.pdf}
\includepdf[pages=2-], pagecommand={\doStuffOnPagesAfterOne}]{file.pdf}
Fuck that. So here’s a demonstration of how to write code inside `pagecommand` and `picturecommand` that is page-specific:
Code: Select all
\documentclass{article}
% Demonstrates use of the pdfpages package with page-by-page actions that are specific to select pages.
\usepackage{mwe} % furnishes example-image-a4-numbered.pdf
\usepackage{pdfpages}
\usepackage{pdfcomment}
\usepackage{ocgx2} % furnishes the ocg environment (PDF layers)
\usepackage{fancybox} % furnishes oval box
\usepackage{fontawesome} % furnishes \faWarning
\begin{document}
\makeatletter
\includepdf[pages=1-,pagecommand={%
% \makeatletter ← ⚠ does not work in this scope; must wrap the whole includepdf construct
\pdfcomment[icon=Note, hoffset=0.5\textwidth, voffset=5em]{%
(inside pagecommand, executing on every page)\textLF\textLF
\texttt{\textbackslash AM@page} variable: \AM@page\textLF\textLF
Side-note: the voffset option has no effect when the value is positive (5em in this case)%
}%
\ifthenelse{\AM@page = 1 \OR \AM@page = 2 \OR \AM@page = 12}{%
\pdfcomment[icon=Insert, hoffset=0.5\textwidth, voffset=-6em]{%
(inside pagecommand, affecting only on pages 1, 2, and 12)\textLF\textLF
\texttt{\textbackslash AM@page} variable: \AM@page\textLF\textLF
Strangely, the voffset option only works if it is negative.%
}%
}{}
% \makeatother
}, picturecommand={%
\put(50,715){Inside the picture environment:}
\put(50,700){%
\begin{tabular}[t]{llp{0.6\textwidth}}
internal \texttt{\textbackslash @tempcnta} variable (useless): &\the\@tempcnta&\\
internal \texttt{\textbackslash @tempcntb} variable (useless): &\the\@tempcntb&\\
internal \texttt{\textbackslash AM@pagecnt} variable (useless): &\the\AM@pagecnt&\\
internal \texttt{\textbackslash AM@abs@page} variable (useless): &\AM@abs@page&\\
internal \texttt{\textbackslash AM@page} variable (interesting): &\AM@page & \faWarning Inside picturecommand, this number is 1 higher than the actual page number! But it’s correct inside pagecommand (see the annotation note to check).\\
internal \texttt{\textbackslash AM@pagecount} variable (interesting): &\AM@pagecount&\\%
\end{tabular}
% lastpage: \AM@lastpage% broken
\ifAM@firstpage
We might expect this to trigger on the 1st page, but it never does.
\fi
}
\put(500,770){%
\begin{ocg}{section labels}{sl1}{on}\color{blue}
\Large\rotatebox{-45}{\setlength{\fboxsep}{6pt}\Ovalbox{Section~A}}
\end{ocg}}}]%
{example-image-a4-numbered.pdf}
\makeatother
\end{document}