VOOZH about

URL: https://ironpdf.com/python/examples/adding-headers-and-footers-advanced/

⇱ Code Example: Customize Headers/Footers in .NET 10 | IronPDF for Python


Skip to footer content
Add Headers/Footers on Specific Pages
from ironpdf import *

# Instantiate Renderer
renderer = ChromePdfRenderer()

multi_page_html = """
 <p> This is 1st Page</p>
 <div style = 'page-break-after: always;' ></div>
 <p> This is 2nd Page</p>
 <div style = 'page-break-after: always;' ></div>
 <p> This is 3rd Page</p>
 <div style = 'page-break-after: always;' ></div>
 <p> This is 4th Page</p>
 <div style = 'page-break-after: always;' ></div>
 <p> This is 5th Page</p>
 <div style = 'page-break-after: always;' ></div>
 <p> This is 6th Page</p>
 <div style = 'page-break-after: always;' ></div>
 <p> This is 7th Page</p>
"""

# Create a PDF or Load an existing PDF using PdfDocument.FromFile()
pdf = renderer.RenderHtmlAsPdf(multi_page_html)

# Create a Header
header = HtmlHeaderFooter()
header.HtmlFragment = "<div>THIS IS HEADER {page} of {total-pages}</div>"

# Create a Page Range 0 .. 7
all_page_indexes = range(pdf.PageCount)

# Example 1
# Apply header to even page index only. (page number will be odd number because index start at 0 but page number start at 1)
even_page_indexes = [i for i in all_page_indexes if i % 2 == 0]
pdf.AddHtmlHeaders(header, 1, ToPageList(even_page_indexes))
pdf.SaveAs("even_page_indexes.pdf")

# Example 2
# Apply header to odd page index only. (page number will be even number because index start at 0 but page number start at 1)
new_pdf = renderer.RenderHtmlAsPdf(multi_page_html)
odd_page_indexes = [i for i in all_page_indexes if i % 2 != 0]
new_pdf.AddHtmlHeaders(header, 1, ToPageList(odd_page_indexes))
new_pdf.SaveAs("odd_page_indexes.pdf")

# Example 3
# Apply header to last page only.
new_pdf = renderer.RenderHtmlAsPdf(multi_page_html)
new_pdf.AddHtmlHeaders(header, 1, ToPageList([new_pdf.PageCount - 1]))
new_pdf.SaveAs("last_page_only.pdf")

# Example 4
# Apply header to first page only.
new_pdf = renderer.RenderHtmlAsPdf(multi_page_html)
new_pdf.AddHtmlHeaders(header, 1, ToPage(0))
new_pdf.SaveAs("first_page_only.pdf")

# Example 5
# Skip the first page.
new_pdf = renderer.RenderHtmlAsPdf(multi_page_html)
new_pdf.AddHtmlHeaders(header, 1, ToPageRange(1, new_pdf.PageCount))
new_pdf.SaveAs("skip_first_page.pdf")

# Example 6
# Skip first page and make second pages label as 1.
new_pdf = renderer.RenderHtmlAsPdf(multi_page_html)
new_pdf.AddHtmlHeaders(header, 0, ToPageRange(1, new_pdf.PageCount))
new_pdf.SaveAs("skip_and_dont_count_the_first_page.pdf")

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. firstPageNumber controls 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 by AddHtmlHeaders.
  • 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

ExampleDescription
Even page indexesHeader on pages 1, 3, 5… (0-based even indexes)
Odd page indexesHeader on pages 2, 4, 6… (0-based odd indexes)
Last page onlyHeader applied only to the final page
First page onlyHeader applied only to the first page
Skip first pageHeader starts from the second page onward
Skip and recountSecond page starts at page number 1

Learn to add headers and footers to PDFs with IronPDF for Python!

Ready to Get Started?
Version: 2026.6 just released

Still Scrolling?

Want proof fast?
run a sample watch your HTML become a PDF.

Install with pip
Version: 2026.6
 pip install ironpdf
  1. Download and install Python 3.7+.
  2. Install pip from pypi.org if it isn't installed already.
  3. Execute the above command in the terminal.
Download Module
Version: 2026.6
Download Now
Manually install into your project
  1. Download the package
  2. Run this command from the terminal
    pip install ironpdf-2026.6-py37-none-win_amd64.whi
Licenses from $749

Have a question? Get in touch with our development team.

Now you've installed with PyPi
Your browser is now downloading IronPDF

Next step: Start free 30-day Trial

No credit card required

  • Test in a live environment
  • Fully-functional product
  • 24/5 technical support

Thank You

Your trial key should be in the email.
If it is not, please contact
support@ironsoftware.com
Get your free 30-day Trial Key instantly.
Thank you.
If you'd like to speak to our licensing team:
πŸ‘ badge_greencheck_in_yellowcircle
The trial form was submitted
successfully.

Your trial key should be in the email.
If it is not, please contact
support@ironsoftware.com

Have a question? Get in touch with our development team.
No credit card or account creation required
Now you've installed with PyPi
Your browser is now downloading IronPDF

Next step: Start free 30-day Trial

No credit card required

  • Test in a live environment
  • Fully-functional product
  • 24/5 technical support
Thank you.
View your license options:
Thank you.
If you'd like to speak to our licensing team:
Have a question? Get in touch with our development team.
Have a question? Get in touch with our development team.
Talk to Sales Team

Book a No-obligation Consult

How we can help:
  • Consult on your workflow & pain points
  • See how other companies solve their .NET document needs
  • All your questions answered to make sure you have all the information you need. (No commitment whatsoever.)
  • Get a tailored quote for your project's needs
Get Your No-Obligation Consult

Complete the form below or email sales@ironsoftware.com

Your details will always be kept confidential.

Trusted by Millions of Engineers Worldwide
Book Free Live Demo

Book a 30-minute, personal demo.

No contract, no card details, no commitments.

Here's what to expect:
  • A live demo of our product and its key features
  • Get project specific feature recommendations
  • All your questions are answered to make sure you have all the information you need.
    (No commitment whatsoever.)
CHOOSE TIME
YOUR INFO
Book your free Live Demo

Trusted by Millions of Engineers Worldwide

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me