10

In my appendix, I want to add several PDF documents (questionnaires) which I used in a study. I managed to implement an appendix and include the PDF files (not hard to do), but now I have two issues I cannot solve:

  1. I would like to see that the first page of my PDF is placed directly below the title of my appendix A. Yet, appendix A is standing solo on the beginning of the page, then a lot of unused space followes, followed by the next page with the content of the PDF.
  2. At the moment, the PDF pages are covering the header of the sections (A Appendix). I would like to see the headers followed by the PDF pages ... Is this even possible?
\documentclass{article}
\usepackage{pdfpages}
\begin{document}
\newpage
\appendix
\section{Appendix A}

\includepdf[pages={1-2}]{Example.pdf}

\end{document}

Jens
  • 1,431
  • The issue is that pdfpages by design imports the pages each on their own page. Is there a reason you can't build the whole document all together, instead of piecing them together afterwards? – cslstr May 09 '14 at 00:04
  • 1
  • Why not use resizebox? – 1010011010 May 09 '14 at 00:11
  • Please complete your code with a suitable \documentclass and \usepackage. A document which starts \begin{document} will not compile. – cfr May 09 '14 at 00:31
  • 1
    http://tex.stackexchange.com/a/160677/39222 is my answer to a slightly different question but the problem is essentially similar. It demonstrates how to include a PDF page with the chapter header and headings appearing. (It involves multiple logical pages per physical page so it complicates but you could cut that stuff as you don't need it.) – cfr May 09 '14 at 00:38
  • @cslstr, I used several questionnaires in my research that were created in word. So I want to convert them into .pdf and piece them together as appendix. – Jens May 09 '14 at 18:00
  • Perhaps this would help? This allows you to use your regular pagestyle over top the included pdf. With appropriate margins, it would look flawless. – cslstr May 09 '14 at 18:15
  • @cslstr But how do you add a \section command in that case? – cfr May 10 '14 at 02:10
  • 1
    @cfr It would not solve the first part of the question; a full page section heading would still be necessary. But \pdfpages could include the external PDF full page with headers. To include section headers, the external doc would have to be scaled down as you have shown. – cslstr May 10 '14 at 02:14
  • 1
    @cslstr Indeed. That's what my solution does except that I use a scaling factor. Of course, if the PDFs are such that there is already room for the headers, you can just set the scaling factor to 1 for that case to avoid unnecessary reductions in size. However, if they were produced in Word, they are likely to have smaller margins than the LaTeX document so I suspect some scaling may be necessary. If you are going to do this for a bunch of PDFs, though, it is useful to wrap the command in a macro for the sake of consistency and easy adjustment. – cfr May 10 '14 at 02:20
  • @cfr @ cslstr: Yes, scaling down was necessary since the used questionnaires are using full page lenght and width in word.

    When I was using \pdfpages code only, the converted pages covered the whole page of each appendix page (including the header)

    – Jens May 10 '14 at 13:13

4 Answers4

9

Here is an adapted version of the code given in the answer I linked to. You will need to adjust this depending on the class and packages you are using and on how full the pages of your PDFs are. The code below is conservative and assumes the pages are quite full though the sample PDF I included doesn't meet this requirement so the pages look too empty. I'm guessing your PDFs are fuller but obviously that's just a guess.

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{pdfpages}
\usepackage{xparse}
\usepackage{kantlipsum}

\makeatletter
\NewDocumentCommand\headerspdf{ O {pages=-} m }{% [options for include pdf]{filename.pdf}
  \includepdf[%
    #1,
    pagecommand={\thispagestyle{fancy}},
    scale=.7,
    ]{#2}}
\NewDocumentCommand\secpdf{somO{1}m}{% [short title]{section title}[page specification]{filename.pdf} --- possibly starred
  \clearpage
  \thispagestyle{fancy}%
  \includepdf[%
    pages=#4,
    pagecommand={%
      \IfBooleanTF{#1}{%
        \section*{#3}}{%
        \IfNoValueTF{#2}{%
          \section{#3}}{%
          \section[#2]{#3}}}},
    scale=.65,
    ]%
    {#5}}
\makeatother

\pagestyle{fancy}

\begin{document}

\newpage
\appendix

\secpdf*{PDF on Starred Section Page}[3]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
  \kant[2]

\section{Include PDF after Section Page}
\kant[1]
\headerspdf[pages=1-2]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}

\secpdf{PDF on Section Page}[4]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\kant[2]

\secpdf[Short Title]{PDF on Section Page with Short Title}{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}
\kant[3]
\headerspdf[pages=2-3]{/usr/local/texlive/2013/texmf-dist/doc/latex/mwe/mwe.pdf}

\end{document}

Sample output:

PDF following section heading with headings

cfr
  • 198,882
  • thanks for your help. the preamble + the code \secpdf[Short Title]{PDF on Section Page with Short Title}{Example.pdf} works very well so far, the whole page is shown perfectly on the page... only problem: if I add a pdf with more than one page, it only displays first page, rest is not shown up. how can I solve this issue? – Jens May 09 '14 at 19:01
  • Sorry, had a logical failure here :)

    Of course \secpdf[Short Title]{PDF on Section Page with Short Title}{Example.pdf} is used for the first page, then I add \headerspdf[pages=2-X]{Example.pdf} for all remaining pages! Thanks a lot :)

    – Jens May 09 '14 at 19:27
  • 1
    @Jens Yes. The question I linked to has more explanation but it is for chapters rather than sections and has some other complications. Probably you want the scaling factor to be a little different on pages with a section title so it is easiest to have two commands so that you can adjust the options separately for pages following a section heading and for pages with just headers. – cfr May 10 '14 at 02:07
  • A while later, I am using KOMA-script and get an error about using fancy-package here. Any suggestion for this? – Jens Jan 02 '19 at 14:54
  • @Jens This answer does also work without fancyhdr. Just remove every \thispagestyle{fancy} and replace \pagestyle{fancy}, e.g., by \pagestyle{headings}. – Schweinebacke Jan 23 '19 at 09:44
  • it is great, but it seems no need to use package xparse, and why need to use \kant?, without it, also works. @Jens – biajia Jan 14 '22 at 09:27
7

Alternatively, I add the PDF as follows:

\usepackage{pdfpages}
\includepdf[pages=-,offset=75 -75]{myfile.pdf}
3

Just in case someone else stumbles on this looking for an answer. I found that you could just use the \section{appendix} command as part of the pagecommand:

\begin{appendix}
  \centering
  \includepdf[pages=2,scale=0.9,offset=0mm -75,pagecommand={
  \begin{flushleft}  
    \section{Appendix}
  \end{flushleft}
   \section{AnyPdf}\label{AnyPdf}}linktodoc=false]{AnyPdf.pdf}
Werner
  • 603,163
Wilf
  • 31
2

For anyone looking for an approach where multiple PDF pages are added to the project appendix under one section with an additional label to reference it. Here is my solution, constructed from the above answers:

\appendix
\chapter{Appendix}

see \ref{reference}

\includepdf[pages=1,offset=0 0, pagecommand={\section{OurAppendix}\label{reference}\thispagestyle{plain}} ]{src/our.pdf}

\includepdf[pages=2-,offset=0 0, pagecommand=\thispagestyle{plain} ]{src/our.pdf}

presa
  • 23