Add PDF Headers and Footers in Python

Use this page to add consistent header and footer content across PDF pages with Aspose.PDF for Python via .NET.

You can build headers and footers with TextFragment, HtmlFragment, TeXFragment, Image, and Table objects, then apply them through HeaderFooter on each page.

Adding Headers and Footers as Text Fragments

Add simple text headers and footers to all pages in a PDF. It creates HeaderFooter objects, inserts TextFragment elements into them, sets MarginInfo for proper positioning, and attaches them to each page in the document. The result is a PDF where every page displays consistent header and footer text.

The following code snippet demonstrates how to add headers and footers as text fragments in a PDF using Python:

  1. Create text fragments for the header and footer.
  2. Create HeaderFooter objects and add the text fragments to them.
  3. Define margin settings to control the placement of the header and footer.
  4. Load the PDF document from the input file.
  5. Iterate through all pages in the document.
  6. Assign the header and footer to each page.
  7. Save the modified PDF to the output file.
import aspose.pdf as ap

def add_header_and_footer_as_text(input_file, output_file):
    # Create header text
    header_text = ap.text.TextFragment("Demo header")
    # Create header
    header = ap.HeaderFooter()
    header.paragraphs.add(header_text)

    # Create footer text
    footer_text = ap.text.TextFragment("Demo footer")

    # Create footer
    footer = ap.HeaderFooter()
    footer.paragraphs.add(footer_text)

    # Set header margin
    margin = ap.MarginInfo()
    margin.left = 50
    margin.top = 20
    header.margin = margin

    # Set footer margin
    footer.margin = margin

    # Open PDF document
    with ap.Document(input_file) as document:
        for i in range(1, len(document.pages) + 1):
            # Bind the header and footer to the page
            document.pages[i].header = header
            document.pages[i].footer = footer

        # Save PDF document
        document.save(output_file)

This method is useful for adding consistent titles, page indicators, or legal disclaimers at the top and bottom of each page. You can also extend it to include images or dynamic content, such as page numbers.

Adding Headers and Footers for Page Numbering

Add automatic page numbering to the headers and footers of a PDF document using Aspose.PDF for Python. Using the built-in variables $p (current page number) and $P (total number of pages), the script dynamically inserts page numbering on every page. Headers display the format ‘Page X from Y’, while footers show ‘Page X / Y’. The MarginInfo ensures proper placement on each page.

  1. Create a TextFragment for the header using “Page $p from $P” to show current and total pages.
  2. Create a HeaderFooter object and add the header text to it.
  3. Create a TextFragment for the footer using “Page $p / $P” for an alternative numbering style.
  4. Create a Footer object and add the footer text.
  5. Define margin settings (left = 50, top = 20) and apply them to both header and footer.
  6. Open the PDF document from the input file.
  7. Loop through all pages and assign the header and footer to each page.
  8. Save the updated PDF to the output path.
import aspose.pdf as ap

def using_header_and_footer_for_page_numbering(input_file, output_file):
    # Create header text
    header_text = ap.text.TextFragment("Page $p from $P")
    # Create header
    header = ap.HeaderFooter()
    header.paragraphs.add(header_text)

    # Create footer text
    footer_text = ap.text.TextFragment("Page $p / $P")

    # Create footer
    footer = ap.HeaderFooter()
    footer.paragraphs.add(footer_text)

    # Create margins
    margin = ap.MarginInfo()
    margin.left = 50
    margin.top = 20

    # Set header margin
    header.margin = margin
    # Set footer margin
    footer.margin = margin

    # Open PDF document
    with ap.Document(input_file) as document:
        for i in range(1, len(document.pages) + 1):
            # Bind the header and footer to the page
            document.pages[i].header = header
            document.pages[i].footer = footer

        # Save PDF document
        document.save(output_file)

Adding Headers and Footers as HTML Fragments

Apply HTML-formatted headers and footers to every page of a PDF document using Aspose.PDF for Python. By using HtmlFragment, the script allows rich text styling—such as bold and italic—to appear in the header and footer. MarginInfo is applied for proper placement, and the same formatted elements are attached to each page in the document.

The following code snippet demonstrates how to add headers and footers as HTML fragments to a PDF using Python:

  1. Create an HTML header snippet using HtmlFragment—including styled text such as ‘’ for bold.
  2. Create a HeaderFooter object and add the HTML header to it.
  3. Create an HTML footer snippet using ‘’ for italic styling.
  4. Create a Footer object and add the footer HTML to it.
  5. Configure margins (left = 50, top = 20) and assign them to both header and footer.
  6. Load the PDF document using ‘ap.Document()’.
  7. Loop through all pages and assign the header and footer to each one.
  8. Save the modified PDF to the specified output path.
import aspose.pdf as ap

def add_header_and_footer_as_html(input_file, output_file):
    # Create header HTML
    header_html = ap.HtmlFragment("This is an HTML <strong>Header</strong>")
    # Create header
    header = ap.HeaderFooter()
    header.paragraphs.add(header_html)

    # Create footer HTML
    footer_html = ap.HtmlFragment("Powered by <i>Aspose.PDF</i>")

    # Create footer
    footer = ap.HeaderFooter()
    footer.paragraphs.add(footer_html)

    # Set header margin
    margin = ap.MarginInfo()
    margin.left = 50
    margin.top = 20
    header.margin = margin

    # Set footer margin
    footer.margin = margin

    # Open PDF document
    with ap.Document(input_file) as document:
        for i in range(1, len(document.pages) + 1):
            # Bind the header and footer to the page
            document.pages[i].header = header
            document.pages[i].footer = footer

        # Save PDF document
        document.save(output_file)

Using HtmlFragment allows rich formatting with inline styles or HTML markup, giving you more design flexibility compared to plain text.

Adding Headers and Footers as Images

Add image-based headers and footers to each page of a PDF document using Aspose.PDF for Python. The same image file is used for both the header and footer on every page. MarginInfo positions the images, and the image automatically adjusts to fit within the header/footer area.

The following code snippet demonstrates how to add headers and footers as images to a PDF using Python:

  1. Load the image into an ‘ap.Image’ object and prepare it for use as a header.
  2. Create a HeaderFooter object and attach the header image to it.
  3. Load the same image again for use as a footer.
  4. Create a Footer object and add the footer image to it.
  5. Load the input PDF document using ‘ap.Document()’.
  6. Iterate through all pages of the document.
  7. Apply margins (left = 50) to position both header and footer.
  8. Assign the header and footer to each page in the PDF.
  9. Save the updated PDF to the specified output file.

This technique is ideal for branding documents with logos or watermarks in the header/footer area.

import aspose.pdf as ap

def add_header_and_footer_as_image(input_file, image_file, output_file):
    # Create header image
    header_image = ap.Image()
    header_image.file = image_file
    # Create header
    header = ap.HeaderFooter()
    header.paragraphs.add(header_image)

    # Create footer image
    footer_image = ap.Image()
    footer_image.file = image_file

    # Create footer
    footer = ap.HeaderFooter()
    footer.paragraphs.add(footer_image)

    # Open PDF document
    with ap.Document(input_file) as document:
        for i in range(1, len(document.pages) + 1):
            # Set header margin
            margin = ap.MarginInfo()
            margin.left = 50
            header.margin = margin

            # Set footer margin
            footer.margin = margin

            # Bind the header and footer to the page
            document.pages[i].header = header
            document.pages[i].footer = footer

        # Save PDF document
        document.save(output_file)

Adding Headers and Footers as Table

Add structured, table-based headers and footers to all pages of a PDF document using Aspose.PDF for Python. Table objects provide better layout control, alignment, and consistent formatting for complex headers and footers. The header text is centered while the footer text is left-aligned, both using Arial 12pt font. Column widths are calculated dynamically based on page dimensions to ensure proper placement.

This code snippet adds headers and footers (using tables) to each page of a PDF document with Aspose.PDF for Python via .NET.

  1. Define text styles using TextState for header and footer (font, size, alignment).
  2. Create HeaderFooter objects for the header and footer.
  3. Build the header Table with a single row and a cell containing the header text.
  4. Build the footer Table with a single row and cell containing the footer text.
  5. Add the tables to the corresponding HeaderFooter objects.
  6. Set footer MarginInfo for proper horizontal positioning.
  7. Open the Document using appropriate methods.
  8. Iterate through all pages and assign the table-based header and footer to each page.
  9. Save the modified Document to the output file.
import aspose.pdf as ap

def add_header_and_footer_as_table(input_file, output_file):
    text_state_header = ap.text.TextState()
    text_state_header.font = ap.text.FontRepository.find_font("Arial")
    text_state_header.font_size = 12
    text_state_header.horizontal_alignment = ap.HorizontalAlignment.CENTER
    text_state_footer = ap.text.TextState()
    text_state_footer.font = ap.text.FontRepository.find_font("Arial")
    text_state_footer.font_size = 12
    text_state_footer.horizontal_alignment = ap.HorizontalAlignment.LEFT
    # Create header
    header = ap.HeaderFooter()
    # Create footer
    footer = ap.HeaderFooter()
    # Create header Table
    table_header = ap.Table()
    table_header.column_widths = str(594 - header.margin.left - header.margin.right)
    header_row = table_header.rows.add()
    header_row.cells.add("This is a Table Header", text_state_header)
    # Create footer Table
    table = ap.Table()
    table.column_widths = str(594 - footer.margin.left - footer.margin.right)
    table.rows.add().cells.add("Powered by Aspose.PDF", text_state_footer)
    header.paragraphs.add(table_header)
    footer.paragraphs.add(table)
    # Set footer margin
    footer.margin.left = 150

    # Open PDF document
    with ap.Document(input_file) as document:
        for i in range(1, len(document.pages) + 1):
            # Bind the header and footer to the page
            document.pages[i].header = header
            document.pages[i].footer = footer

        # Save PDF document
        document.save(output_file)

Adding Headers and Footers as LaTeX

Add headers and footers containing LaTeX-formatted content to all pages of a PDF document using Aspose.PDF for Python. LaTeX allows rendering of mathematical symbols, dates, copyright marks, and other advanced formatting. The header includes a dynamic date, while the footer displays a copyright symbol along with the current page number and total page count.

The following code snippet shows how to use TeXFragment in headers and footers for a PDF using Aspose.PDF for Python via .NET.

  1. Open the Document using appropriate methods.
  2. Determine total page count to use in dynamic footers.
  3. Iterate through all pages of the document.
  4. Create a HeaderFooter object for the header.
  5. Create a TeXFragment for the header text containing LaTeX commands (e.g., \\today\\).
  6. Create a HeaderFooter object for the footer.
  7. Create a TeXFragment for the footer text including LaTeX symbols and page numbering.
  8. Add the TeXFragment to the corresponding header/footer object.
  9. Bind the header and footer to the current page.
  10. Save the modified Document to the output file.
import aspose.pdf as ap

def add_header_and_footer_as_latex(input_file, output_file):
    # Open PDF document
    with ap.Document(input_file) as document:
        page_count = len(document.pages)
        for i in range(1, page_count + 1):
            # Create header
            header = ap.HeaderFooter()
            h_latex_text = "This is a LaTeX Header. \\today\\"
            h_l_text = ap.TeXFragment(h_latex_text, True)
            # Create footer
            footer = ap.HeaderFooter()
            f_latex_text = (
                f"\\copyright\\ 2025 My Company -- Page \\thepage\\ is {page_count}"
            )
            f_l_text = ap.TeXFragment(f_latex_text, True)

            header.paragraphs.add(h_l_text)
            footer.paragraphs.add(f_l_text)
            # Bind the header and footer to the page
            document.pages[i].header = header
            document.pages[i].footer = footer

        # Save PDF document
        document.save(output_file)