How to Add Headers and Footers in PDF Using IronPDF

Add Headers and Footers in PDF Using C# and IronPDF

IronPDF enables you to easily add headers and footers to PDF documents in C# using methods like AddTextHeaders and AddTextFooters for simple text, or AddHtmlHeaders and AddHtmlFooters for HTML-based content with full CSS styling support. This powerful functionality is essential for creating professional PDFs with consistent branding, page numbering, and document metadata.

Need to include page numbers, a company logo, or a date at the top or bottom of every page in a PDF document? IronPDF makes it simple to apply headers and footers to PDFs in your C# project. Whether you're generating reports, invoices, or any business documents, headers and footers provide crucial navigation and identification elements that enhance document usability.

Quickstart: Add Headers and Footers to PDFs in C#

Effortlessly add headers and footers to your PDF documents using IronPDF in C#. This guide demonstrates how to apply text-based headers and footers with page numbers and custom text in seconds. Use the AddTextHeaders and AddTextFooters methods to enhance your PDF presentation quickly. Save your updated PDF with minimal code, ensuring a professional finish to your documents.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    new IronPdf.ChromePdfRenderer { RenderingOptions = { TextHeader = new IronPdf.TextHeaderFooter { CenterText = "Report • {date}" }, TextFooter = new IronPdf.TextHeaderFooter { RightText = "Page {page} of {total-pages}" } } }
        .RenderHtmlAsPdf("<h1>Hello World!</h1>")
        .SaveAs("withHeadersFooters.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

To create a header/footer with only text, instantiate a TextHeaderFooter object, add your desired text, and add the object to your PDF. The TextHeaderFooter class provides a straightforward way to add consistent text elements across all pages of your document. This method is particularly useful for simple headers and footers that don't require complex formatting or styling.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-add-textheaderfooter.cs
using IronPdf;

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Create text header
TextHeaderFooter textHeader = new TextHeaderFooter
{
    CenterText = "This is the header!",
};

// Create text footer
TextHeaderFooter textFooter = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

// Add text header and footer to the PDF
pdf.AddTextHeaders(textHeader);
pdf.AddTextFooters(textFooter);

pdf.SaveAs("addTextHeaderFooter.pdf");
$vbLabelText   $csharpLabel

How can I add headers/footers during rendering?

Alternatively, you can directly add a header/footer using the renderer's RenderingOptions. This adds the text header and footer during the rendering process, which is more efficient than adding them after the PDF is created. This approach is recommended when you know the header and footer content beforehand, as it reduces processing time and ensures consistent formatting from the start.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-render-with-textheaderfooter.cs
using IronPdf;

// Instantiate renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create header and add to rendering options
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
    CenterText = "This is the header!",
};


// Create footer and add to rendering options
renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

// Render PDF with header and footer
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");
pdf.SaveAs("renderWithTextHeaderFooter.pdf");
$vbLabelText   $csharpLabel

How Can I Customize Text and Divider Properties?

In the TextHeaderFooter class, you can set text for the left, center, and right positions. Additionally, you can customize the font type and size of the text and add a divider with a custom color by configuring the relevant properties. These customization options allow you to create headers and footers that match your corporate branding or document style guidelines. The divider line feature is particularly useful for creating visual separation between the header/footer and the main content.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-textheaderfooter-options.cs
using IronPdf;
using IronPdf.Font;
using IronSoftware.Drawing;

// Create text header
TextHeaderFooter textHeader = new TextHeaderFooter
{
    CenterText = "Center text", // Set the text in the center
    LeftText = "Left text", // Set left-hand side text
    RightText = "Right text", // Set right-hand side text
    Font = IronSoftware.Drawing.FontTypes.ArialBoldItalic, // Set font
    FontSize = 16, // Set font size
    DrawDividerLine = true, // Draw Divider Line
    DrawDividerLineColor = Color.Red, // Set color of divider line
};
$vbLabelText   $csharpLabel

What does the customized text header look like?

Text alignment example showing left, center, and right text positioning options

Which fonts are available by default?

You can see what font types are available by default in the IronPDF API Reference. IronPDF supports a wide range of standard fonts including Arial, Times New Roman, Helvetica, Courier, and their variations. If you need custom fonts, learn more about managing fonts in IronPDF.

How Do I Set Margins for Text Headers/Footers?

By default, text headers and footers in IronPDF come with predefined margins. If you want the text header to span the entire width of the PDF document, specify margin values of 0. This can be achieved by either setting the margins directly in the AddTextHeaders and AddTextFooters functions or through the RenderingOptions in ChromePdfRenderer. Understanding margin control is crucial for achieving pixel-perfect layouts, especially when working with custom paper sizes.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-textheaderfooter-margins.cs
using IronPdf;

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

TextHeaderFooter header = new TextHeaderFooter
{
    CenterText = "This is the header!",
};

TextHeaderFooter footer = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

pdf.AddTextHeaders(header, 35, 30, 25); // Left Margin = 35, Right Margin  = 30, Top Margin = 25
pdf.AddTextFooters(footer, 35, 30, 25); // Margin values are in mm
$vbLabelText   $csharpLabel

How do I apply margins through rendering options?

If you add margin values in the RenderingOptions of ChromePdfRenderer, these margins will also apply to the header and footer. This approach provides a centralized way to manage margins across your entire document, including headers, footers, and main content. For more advanced margin customization, check our guide on setting custom margins.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-rendering-options-margins.cs
using IronPdf;

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();

TextHeaderFooter header = new TextHeaderFooter
{
    CenterText = "This is the header!",
};

TextHeaderFooter footer = new TextHeaderFooter
{
    CenterText = "This is the footer!",
};

// Margin values are in mm
renderer.RenderingOptions.MarginRight = 30;
renderer.RenderingOptions.MarginLeft = 30;
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;

// Add header and footer to renderer
renderer.RenderingOptions.TextHeader = header;
renderer.RenderingOptions.TextFooter = footer;

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");
$vbLabelText   $csharpLabel

Why should I avoid UseMarginsOnHeaderAndFooter?

The UseMarginsOnHeaderAndFooter property on RenderingOptions is not suitable for this use case. It applies the same margin values to the header, footer, and main content, which can cause the header to overlap with the document body. This property is primarily intended for applying headers and footers to existing PDFs using the AddTextHeadersAndFooters method. For better control over layout, consider using page breaks to manage content flow.

What is Dynamic Margin Sizing?

Static margins posed an issue when header content varied between documents. Adjustments were required not only for header and footer margins but also for the main HTML margin to accommodate different header and footer sizes. Consequently, we implemented a Dynamic Margin Sizing feature where the height of the header and footer will dynamically adjust based on content, and the main HTML will reposition accordingly. This feature is particularly useful when working with responsive CSS layouts. Use the code below to try this feature:

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-dynamic-marigns.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    HtmlFragment = @"<div style='background-color: #4285f4; color: white; padding: 15px; text-align: center;'>
                    <h1>Example header</h1> <br>
                    <p>Header content</p>
                    </div>",
    // Enable the dynamic height feature
    MaxHeight = HtmlHeaderFooter.FragmentHeight,
};

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>");
pdf.SaveAs("dynamicHeaderSize.pdf");
$vbLabelText   $csharpLabel

How Do I Add Metadata to Text Headers/Footers?

You can easily add metadata such as page numbers, date, and PDF title by incorporating placeholder strings in your text. These placeholders are automatically replaced with the corresponding values when the PDF is rendered. This feature is essential for creating dynamic headers and footers that update automatically based on the document's properties. Here are all available metadata options:

  • {page}: Current page number.
  • {total-pages}: Total page number.
  • {url}: Web URL from which the PDF document was rendered.
  • {date}: Current date.
  • {time}: Current time.
  • {html-title}: HTML title specified in the title tag in HTML.
  • {pdf-title}: PDF title specified in the PDF metadata.

Which placeholders should I use most often?

To learn more about {page} and {total-pages}, visit the IronPDF Page Numbers Guide. These placeholders are the most commonly used as they provide essential navigation information. The date and time placeholders are particularly useful for documents that need timestamp tracking, such as reports or invoices.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-mail-merge.cs
using IronPdf;

// Create header and footer
TextHeaderFooter textHeader = new TextHeaderFooter
{
    CenterText = "{page} of {total-pages}",
    LeftText = "Today's date: {date}",
    RightText = "The time: {time}",
};

TextHeaderFooter textFooter = new TextHeaderFooter
{
    CenterText = "Current URL: {url}",
    LeftText = "Title of the HTML: {html-title}",
    RightText = "Title of the PDF: {pdf-title}",
};
$vbLabelText   $csharpLabel

How Do I Add HTML Headers/Footers?

You can further customize your header/footer by utilizing HTML and CSS. To create an HTML header/footer, use the HtmlHeaderFooter class. This approach provides maximum flexibility, allowing you to include images, complex layouts, and styled content in your headers and footers. If you would like to retain CSS styles from a CSS style sheet, set LoadStylesAndCSSFromMainHtmlDocument = true in the class properties. This is particularly useful when working with web fonts and icons.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-htmlheaderfooter.cs
using IronPdf;

string headerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>";

string footerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>";

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Create header and footer
HtmlHeaderFooter htmlHeader = new HtmlHeaderFooter
{
    HtmlFragment = headerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter
{
    HtmlFragment = footerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

// Add to PDF
pdf.AddHtmlHeaders(htmlHeader);
pdf.AddHtmlFooters(htmlFooter);
$vbLabelText   $csharpLabel

Similar to text headers and footers, the AddHtmlHeaders and AddHtmlFooters methods have pre-defined margins applied to them. To apply custom margins, use an overload of the functions with the specified margin values. To span the whole content without any margins, set the margins in the overload function to 0. This level of control is essential when creating professional documents with specific layout requirements.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-htmlheaderfooter-margins.cs
// Add to PDF
pdf.AddHtmlHeaders(header, 0, 0, 0);
pdf.AddHtmlFooters(footer, 0, 0, 0);
$vbLabelText   $csharpLabel

Can I add HTML headers/footers during rendering?

Adding headers and footers can also be done directly through the renderer's RenderingOptions. This adds the HTML header and footer during the rendering process, which is more efficient than post-processing. This method is particularly useful when generating PDFs from HTML files or URL to PDF conversions.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-htmlheaderfooter.cs
using IronPdf;

string headerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>";

string footerHtml = @"
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>";

// Instantiate renderer and create PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");

// Create header and footer
HtmlHeaderFooter htmlHeader = new HtmlHeaderFooter
{
    HtmlFragment = headerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter
{
    HtmlFragment = footerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

// Add to PDF
pdf.AddHtmlHeaders(htmlHeader);
pdf.AddHtmlFooters(htmlFooter);
$vbLabelText   $csharpLabel

When Should I Use Text vs HTML Headers/Footers?

When deciding between Text and HTML headers/footers, consider the trade-offs. If you prioritize faster PDF rendering, opt for Text headers/footers. If customizability and styling are essential, choose HTML headers/footers. The rendering time difference between Text and HTML headers/footers is minimal when the HTML headers/footers contain limited content. However, it increases as the size and number of assets in the HTML headers/footers increase.

What are the performance implications?

Text headers/footers render faster because they don't require HTML parsing and CSS processing. HTML headers/footers provide more flexibility but require additional rendering time proportional to their complexity. When working with large documents or batch processing, the performance difference becomes more noticeable. For optimal performance in high-volume scenarios, consider our guide on async PDF generation.

Ready to see what else you can do? Check out our tutorial page here: Create PDFs

Frequently Asked Questions

How do I add text headers and footers to a PDF in C#?

With IronPDF, you can add text headers and footers using the AddTextHeaders and AddTextFooters methods. Simply instantiate a TextHeaderFooter object, add your desired text, and apply it to your PDF. This provides a straightforward way to add consistent text elements like page numbers or document titles across all pages.

Can I include page numbers in my PDF headers or footers?

Yes, IronPDF supports dynamic page numbering using special placeholders. You can use {page} for the current page number and {total-pages} for the total page count in your TextHeaderFooter objects. For example, setting RightText = "Page {page} of {total-pages}" will automatically display the correct page numbers on each page.

Is it possible to add HTML-based headers and footers with CSS styling?

Absolutely! IronPDF provides AddHtmlHeaders and AddHtmlFooters methods that allow you to add HTML content with full CSS styling support. This enables you to create complex headers and footers with formatted text, images, and custom styling to match your brand guidelines.

What's the most efficient way to add headers and footers to PDFs?

The most efficient approach is to add headers and footers during the rendering process using IronPDF's RenderingOptions. By configuring TextHeader and TextFooter properties in the ChromePdfRenderer before rendering, you reduce processing time compared to adding them after the PDF is created.

Can I add different content to the left, center, and right sections of headers/footers?

Yes, the TextHeaderFooter class in IronPDF provides LeftText, CenterText, and RightText properties, allowing you to place different content in each section. This gives you flexibility to organize information like dates on the left, titles in the center, and page numbers on the right.

How do I add a company logo to my PDF headers?

To add a company logo to your PDF headers, use the AddHtmlHeaders method in IronPDF. You can include an image tag in your HTML content pointing to your logo file, along with any additional styling or positioning using CSS to ensure it appears exactly where you want it.

Can I include dates in my PDF headers and footers?

Yes, IronPDF supports dynamic date insertion using the {date} placeholder in TextHeaderFooter objects. When you include {date} in your header or footer text, it will automatically be replaced with the current date when the PDF is generated.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...
Read More
Ready to Get Started?
Nuget Downloads 17,012,929 | Version: 2025.12 just released