2

I need to include PDF pages with \includegraphics. I know there is the package \usepackage{pdfpages}. So this is what I have:

\begin{picture}(0,0)(75,272.5)
\put(0,0){\frame{\includegraphics[page=i]{pdf_file_name}}}
\end{picture}

I need those dimensions so that the PDF page covers the whole page it is included. Now I want to add every page in a row of that PDF file. How can I create a loop which does that for me? Is there a key for the last page of the file? I have no idea about using \while \for or \if in this case. If you have any further documentation about it that would great.

Example:

\for i=1 to i=last_page_of_pdf_file
{
\begin{picture}(0,0)(75,272.5)
\put(0,0){\frame{\includegraphics[page=i]{pdf_file_name}}}
\end{picture}
\newpage
}

I asked somthing similar using \includepdf. - Link

  • 2
    Could you elaborate a bit why you don't just use pdfpages (maybe with the picturecommand option of the \includepdf command)? – Daniel Jul 23 '13 at 20:21
  • Well I tried but I couldn't figure out to change the page layout so that the option fitpaper would work and the PDF page covers the whole page. - (tex.stackexchange.com/questions/125135/…) – Phil Jul 23 '13 at 20:29
  • This is what I used: \includepdf[page={-},frame,pagecommand={},fitpaper]{file_name}. But as I have a customized class there is always a margin to the right and to the bottom of the page which shouldn't be there with the option fitpaper. – Phil Jul 23 '13 at 20:40
11

The tricky part is to get the maximum number of pages in a PDF file. If a file is included in pdfTeX, then it sets a primitive \pdflastximagepages with the number of pages in the included PDF file.

Thus the following example defines the macro \getpdfpages. It includes the first page in a temporary box. Then it sets the LaTeX counter pdfpages to the value of \pdflastximagepages.

Looping can be done in many, many ways. The example just uses one of them, \foreach of pgf/TikZ:

\documentclass{article}
\usepackage{graphicx}
\usepackage{pgffor}

\newcounter{pdfpages}
\newcommand*{\getpdfpages}[1]{%
  \begingroup
    \sbox0{% 
      \includegraphics{#1}%
      \setcounter{pdfpages}{\pdflastximagepages}%
    }%
  \endgroup
}

\begin{document}
  \getpdfpages{t.pdf}
  \foreach \x in {1,...,\value{pdfpages}} {
    % your code for page \x of t.pdf goes here
    \includegraphics[page=\x]{t.pdf}%
  }
\end{document}
  • Cool, I wasn't aware of \pdflastximagepages. Very useful! – Daniel Jul 24 '13 at 21:05
  • Note that using \pdflastximagepages after \includegraphics should be handeled with care as \includegraphics doesn't always call \pdfximage. – Stephan Lehmke Dec 3 '13 at 15:14
  • @StephanLehmke: For example, MetaPost PostScript files (MPS files) are handled differently and processed by some ConTeXt macros of Hans Hagen without \pdfximage. – Heiko Oberdiek Dec 4 '13 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.