Search code examples
headerlatexfooterpdfpages

How to correctly set a fancy header and footer in a included pdf, which contains many pages?


Last time I tried to add fancy header and footer in my document, but I'm not happy of it's result. How to correctly set a fancy header and footer in a included pdf, which contains many pages?

My goal, which I want achieve, is: If the number of page is odd, I want to have a header and the number of page on the left side of page, otherwise I want to have it on the right.

Here is my base code:

\documentclass[oneside, a4paper, 12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{polski}
\usepackage{pdfpages}
\usepackage{changepage}
\usepackage{fancyhdr}

\usepackage[right=2.7cm,left=3.5cm, top=2.7cm, bottom=2.5cm,includehead]{geometry}

\strictpagecheck

\begin{document}
    %\setcounter{page}{18}
\includepdf[pages=-,pagecommand={
    \ifoddpage 
    \pagestyle{fancy} \fancyhf{}
    \fancyhead[L]{Test of left header}
    \fancyfoot[L]{\thepage} 
    \else
    \fancyhead[R]{Test of right footer}
    \fancyfoot[R]{\thepage} 
    \fi}]{scenariusz_wersja_do_edycji.pdf}
\end{document}

I would be glad if somebody knows the solution, because i need it necessarily for the BA thesis!


Solution

  • If you use a twosideed documentclass, then you can let fancyhdr automatically place your header/page number on even/odd pages:

    \documentclass[twoside, a4paper, 12pt]{article}
    
    \usepackage[utf8]{inputenc}
    \usepackage{polski}
    \usepackage{pdfpages}
    \usepackage{changepage}
    \usepackage{fancyhdr}
    
    \usepackage[right=2.7cm,left=3.5cm, top=2.7cm, bottom=2.5cm,includehead]{geometry}
    
    \strictpagecheck
    
    \setlength{\headheight}{15pt}
    
    \fancypagestyle{mystyle}{%
    \fancyhf{}
    \fancyhead[LO,RE]{Some header}
    \fancyfoot[LO,RE]{\thepage}
    }
    
    
    \begin{document}
        %\setcounter{page}{18}
    \includepdf[pages=-,pagecommand={
    \thispagestyle{mystyle}
        }]{example-image-duck}
    \end{document}