How to Add Headers and Footers

Need to include page numbers, a company logo, or a date at the top or bottom of every page in a PDF document? With headers and footers, you can! With IronPDF, it is very simple to apply headers and footers to PDFs in your C# Project.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet

    PM > Install-Package IronPdf

  2. Copy the code

    new IronPdf.ChromePdfRenderer()
        .RenderHtmlAsPdf("<h1>Content</h1>")
        .AddTextHeaders(new IronPdf.TextHeaderFooter { CenterText = "My Company", DrawDividerLine = true })
        .AddTextFooters(new IronPdf.TextHeaderFooter { RightText = "Page {page} of {total‑pages}", DrawDividerLine = true })
        .SaveAs("withHeadersFooters.pdf");
  3. Deploy to test on your live environment

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

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer


To create a header/footer out of only text, instantiate a TextHeaderFooter object, add your desired text, and add the object to your PDF.

: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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Alternatively, you can directly add a header/footer using the renderer's rendering options. This will add the text header and footer during rendering process.

: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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Customize Text and Divider Properties

In the TextHeaderFooter class, you have the ability to set the 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.

: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
};
Imports IronPdf
Imports IronPdf.Font
Imports IronSoftware.Drawing

' Create text header
Private textHeader As New TextHeaderFooter With {
	.CenterText = "Center text",
	.LeftText = "Left text",
	.RightText = "Right text",
	.Font = IronSoftware.Drawing.FontTypes.ArialBoldItalic,
	.FontSize = 16,
	.DrawDividerLine = True,
	.DrawDividerLineColor = Color.Red
}
$vbLabelText   $csharpLabel

Output Text Header

Text Header

You can see what font types are available by default in the IronPDF API Reference.

By default, the Text header and footer in IronPDF come with predefined margins. If you want the text header to span the entire width of the PDF document, you can 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.

: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
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

If you add margin values in the RenderingOptions of ChromePdfRenderer, these margins will also be applied to the header and footer.

: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>");
Imports IronPdf

' Instantiate renderer and create PDF
Private renderer As New ChromePdfRenderer()

Private header As New TextHeaderFooter With {.CenterText = "This is the header!"}

Private footer As New TextHeaderFooter With {.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

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

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.

Dynamic Margin Sizing

The static margin posed an issue when the header content varied between documents. Adjustments were required not only for the header and footer margins but also for the main HTML margin to accommodate different header and footer sizes. Consequently, we have implemented a Dynamic Margin Sizing feature where the height of the header and footer will dynamically adjust based on the content, and the main HTML will reposition accordingly. Please use the code below to check the feature out:

: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");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

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

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

You can easily add metadata such as page numbers, the date, and the title of a PDF, by incorporating placeholder strings in your text. Here are all the 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.

To learn more about {page} and {total-pages}, please visit the IronPDF Page Numbers Guide How-To guide.

: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}",
};
Imports IronPdf

' Create header and footer
Private textHeader As New TextHeaderFooter With {
	.CenterText = "{page} of {total-pages}",
	.LeftText = "Today's date: {date}",
	.RightText = "The time: {time}"
}

Private textFooter As New TextHeaderFooter With {
	.CenterText = "Current URL: {url}",
	.LeftText = "Title of the HTML: {html-title}",
	.RightText = "Title of the PDF: {pdf-title}"
}
$vbLabelText   $csharpLabel

You can further customize your header/footer by utilizing HTML and CSS. To create an HTML header/footer, use the HtmlHeaderFooter class. If you would like to retain CSS styles from a CSS style sheet, make sure to set LoadStylesAndCSSFromMainHtmlDocument = true in the class properties.

: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);
Imports IronPdf

Private headerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>"

Private footerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>"

' Instantiate renderer and create PDF
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")

' Create header and footer
Private htmlHeader As New HtmlHeaderFooter With {
	.HtmlFragment = headerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

Private htmlFooter As New HtmlHeaderFooter With {
	.HtmlFragment = footerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

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

Similar to the text header and footer, the AddHtmlHeaders and AddHtmlFooters methods shown above 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.

: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);
' Add to PDF
pdf.AddHtmlHeaders(header, 0, 0, 0)
pdf.AddHtmlFooters(footer, 0, 0, 0)
$vbLabelText   $csharpLabel

Adding headers and footers could also be done directly through the renderer's rendering options. This will add the HTML header and footer during rendering process.

:path=/static-assets/pdf/content-code-examples/how-to/headers-and-footers-render-with-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
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create header and footer and add to rendering options
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
    HtmlFragment = headerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
    HtmlFragment = footerHtml,
    LoadStylesAndCSSFromMainHtmlDocument = true,
};

// Render PDF with header and footer
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");
Imports IronPdf

Private headerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a header!</h1>
    </body>
    </html>"

Private footerHtml As String = "
    <html>
    <head>
        <link rel='stylesheet' href='style.css'>
    </head>
    <body>
        <h1>This is a footer!</h1>
    </body>
    </html>"

' Instantiate renderer
Private renderer As New ChromePdfRenderer()

' Create header and footer and add to rendering options
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter With {
	.HtmlFragment = headerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter With {
	.HtmlFragment = footerHtml,
	.LoadStylesAndCSSFromMainHtmlDocument = True
}

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

Choosing Between Text and HTML Headers/Footers

In deciding between Text and HTML headers/footers, consider the trade-offs. If you prioritize faster PDF rendering, opt for Text headers/footers. On the other hand, if customizability and styling are essential, go for HTML headers/footers. The difference in rendering times between Text and HTML headers/footers is not too significant when the HTML headers/footers do not contain much content. However, it does increase as the size and number of assets in the HTML headers/footers increase.

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

Frequently Asked Questions

How can I add headers and footers to a PDF document in a C# project?

To add headers and footers to a PDF document in a C# project, utilize IronPDF. First, download the IronPDF library, then load or create a PDF. You can use methods like AddTextHeaders and AddTextFooters for text-based headers/footers or AddHtmlHeaders and AddHtmlFooters for HTML-based options.

What methods are available for adding text and HTML headers or footers in C#?

In IronPDF, you can use AddTextHeaders and AddTextFooters for adding text headers and footers, while AddHtmlHeaders and AddHtmlFooters are used for HTML headers and footers.

How do I customize headers and footers in a PDF using C#?

Customize headers and footers in IronPDF by adjusting text properties such as font type, size, and alignment using the TextHeaderFooter class. For HTML headers and footers, leverage HTML and CSS styling for enhanced design.

How can I ensure headers and footers do not overlap with the main content in a PDF?

Ensure headers and footers do not overlap with the main content by setting appropriate margins in IronPDF. Use the RenderingOptions in ChromePdfRenderer to specify margin values, or enable Dynamic Margin Sizing which automatically adjusts based on content.

What is the advantage of using HTML headers and footers in PDF documents?

HTML headers and footers in IronPDF offer greater customization and styling flexibility compared to text headers and footers. They allow the use of HTML and CSS, enabling complex and visually appealing designs.

Can I include dynamic content like page numbers and dates in PDF headers and footers?

Yes, you can include dynamic content such as page numbers and dates in PDF headers and footers using IronPDF. Use placeholder strings like {page}, {total-pages}, {date}, and {pdf-title} within the header/footer text.

How do I choose between text and HTML headers/footers in my PDF project?

Choose text headers/footers in IronPDF for faster rendering times. Opt for HTML headers/footers if you need more customization and styling options. Consider the complexity and size of the content, as these can influence rendering performance.

Is it possible to apply custom margins to HTML headers and footers in PDFs?

Yes, custom margins can be applied to HTML headers and footers in IronPDF. Use the overloads of AddHtmlHeaders and AddHtmlFooters to specify margin values and control the header/footer width.

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