1

How can I globally set different PDF page boxes on odd and even pages?

I tried using the everypage package but it does not seem to actually insert the PDF page box into the current page object.

I can use \pdfpageattr on a page directly and the page box is set properly.

MWE

\documentclass{article}
\usepackage{ifoddpage}
\usepackage{everypage}
\AddEverypageHook{
  \checkoddpage
  \ifoddpage
    \pdfpageattr{/ArtBox [72 72 216 216]}
  \else
    \pdfpageattr{/ArtBox [216 216 360 360]}
  \fi
}
\begin{document}
\null
%\pdfpageattr{/ArtBox [72 72 216 216]}% directly inserting on each page works
\clearpage
\null
%\pdfpageattr{/ArtBox [216 216 360 360]}% directly inserting on each page works
\end{document}

The above file gives a PDF containing (no ArtBox):

2 0 obj
<<
/Type /Page
/Contents 3 0 R
/Resources 1 0 R
/MediaBox [0 0 595.276 841.89]
/Parent 4 0 R
>>
endobj
1 0 obj
<<
/ProcSet [ /PDF ]
>>
endobj
7 0 obj
<<
/Length 0         
>>
stream

endstream
endobj
6 0 obj
<<
/Type /Page
/Contents 7 0 R
/Resources 5 0 R
/MediaBox [0 0 595.276 841.89]
/Parent 4 0 R
>>
endobj

If I uncomment the \pdfpageattr lines in the body of the document I get what I want:

2 0 obj
<<
/Type /Page
/Contents 3 0 R
/Resources 1 0 R
/MediaBox [0 0 595.276 841.89]
/ArtBox [72 72 216 216]
/Parent 4 0 R
>>
endobj
1 0 obj
<<
/ProcSet [ /PDF ]
>>
endobj
7 0 obj
<<
/Length 0         
>>
stream

endstream
endobj
6 0 obj
<<
/Type /Page
/Contents 7 0 R
/Resources 5 0 R
/MediaBox [0 0 595.276 841.89]
/ArtBox [216 216 360 360]
/Parent 4 0 R
>>
endobj
6
  • \AtBeginShipout{\ifodd\c@page ... (with atbegshi) will work. But be aware that you are overwriting other uses of \pdfpageattr here, e.g. from hyperref, tagpdf, fancytoolstips, pdflscape, pdfx. – Ulrike Fischer May 22 '19 at 8:08
  • I'd recommend to add those boxes to your page styles, that is, include them into your \@oddfoot and \@evenfoot. – Lupino May 22 '19 at 10:18
  • @UlrikeFischer, thanks. That works. Feel free to add a quick answer if you like. – David Purton May 22 '19 at 12:55
  • @Lupino, I had tried using fancyhdr but that didn't work either. But feel free to add answer if you can make it work. – David Purton May 22 '19 at 12:56
  • @UlrikeFischer, BTW, why doesn't it work with everypage? – David Purton May 22 '19 at 13:05
1

Be aware that you are overwriting other uses of \pdfpageattr here, e.g. from hyperref, tagpdf, fancytoolstips, pdflscape, pdfx.

everypage places its hook in a group, so you need to make the setting global. There is imho no need to use ifoddpage.

\documentclass{article}
\pdfcompresslevel0
\pdfobjcompresslevel0
\usepackage{everypage}
\makeatletter
\AddEverypageHook{
  \ifodd\c@page
    \global\pdfpageattr{/ArtBox [72 72 216 216]}
  \else
    \global\pdfpageattr{/ArtBox [216 216 360 360]}
  \fi
}
\begin{document}
\null
%\pdfpageattr{/ArtBox [72 72 216 216]}% directly inserting on each page works
\clearpage
\null
%\pdfpageattr{/ArtBox [216 216 360 360]}% directly inserting on each page works
\end{document}

The alternative is atbegshi:

\documentclass{article}
\pdfcompresslevel0
\pdfobjcompresslevel0
\usepackage{atbegshi}
\makeatletter
\AtBeginShipout{
  \ifodd\c@page
    \global\pdfpageattr{/ArtBox [72 72 216 216]}
  \else
    \global\pdfpageattr{/ArtBox [216 216 360 360]}
  \fi
}
\begin{document}
\null
%\pdfpageattr{/ArtBox [72 72 216 216]}% directly inserting on each page works
\clearpage
\null
%\pdfpageattr{/ArtBox [216 216 360 360]}% directly inserting on each page works
\end{document}

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.