Add Headers/Footers on Specific Pages
IronPDF for Python provides fine-grained control over which pages receive headers and footers, allowing you to target even pages, odd pages, first or last pages, or custom page ranges within a single document.
Getting Started
Render a multi-page PDF, create an HtmlHeaderFooter object, then use AddHtmlHeaders with one of the page-targeting helpers to apply the header selectively. Multiple calls with different page lists can add different headers to different sections of the document.
Understanding the Code
HtmlHeaderFooter: The header object containing the HTML fragment to stamp on each targeted page. Dynamic fields like{page}and{total-pages}are supported.AddHtmlHeaders(header, firstPageNumber, pageList): Applies the header to the pages specified by the page list.firstPageNumbercontrols the value of the{page}merge field on the first targeted page.ToPageList(indexes): Converts a Python list of zero-based page indexes into the page list format required byAddHtmlHeaders.ToPage(index): Targets a single page by its zero-based index.ToPageRange(start, end): Targets a contiguous range of pages by their zero-based indexes.
Page-Targeting Examples in This Code
| Example | Description |
|---|---|
| Even page indexes | Header on pages 1, 3, 5… (0-based even indexes) |
| Odd page indexes | Header on pages 2, 4, 6… (0-based odd indexes) |
| Last page only | Header applied only to the final page |
| First page only | Header applied only to the first page |
| Skip first page | Header starts from the second page onward |
| Skip and recount | Second page starts at page number 1 |
Learn to add headers and footers to PDFs with IronPDF for Python!
Related Docs Links
Ready to Get Started?
Version: 2026.6 just released
Still Scrolling?
Want proof fast?
run a sample watch your HTML become a PDF.






