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.
-
1Install IronPDF with NuGet Package Manager
-
2Copy 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");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
Minimal Workflow (5 steps)
- Download the C# library to add headers and footers
- Load an existing PDF or render a new one
- Use the
AddTextHeadersandAddTextFootersmethods to add text headers and footers - Use the
AddHtmlHeadersandAddHtmlFootersmethods to add HTML headers and footers - Add headers and footers at rendering time by configuring the
RenderingOptions
How Do I Add a Text Header/Footer?
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.
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");Imports IronPdf
' Instantiate renderer and create PDF
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
' Create text header
Dim textHeader As New TextHeaderFooter With {
.CenterText = "This is the header!"
}
' Create text footer
Dim textFooter As New TextHeaderFooter With {
.CenterText = "This is the footer!"
}
' Add text header and footer to the PDF
pdf.AddTextHeaders(textHeader)
pdf.AddTextFooters(textFooter)
pdf.SaveAs("addTextHeaderFooter.pdf")Output
A centered text header and footer are stamped onto the rendered page.
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.
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");Imports IronPdf
' Instantiate renderer
Dim renderer As New ChromePdfRenderer()
' Create header and add to rendering options
renderer.RenderingOptions.TextHeader = New TextHeaderFooter With {
.CenterText = "This is the header!"
}
' Create footer and add to rendering options
renderer.RenderingOptions.TextFooter = New TextHeaderFooter With {
.CenterText = "This is the footer!"
}
' Render PDF with header and footer
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
pdf.SaveAs("renderWithTextHeaderFooter.pdf")Output
The same centered header and footer, applied during rendering through RenderingOptions.
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.
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
}Output
The left, center, and right text render with their configured fonts, and a colored divider line sits beneath them.

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.
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 mmImports IronPdf
' Instantiate renderer and create PDF
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
Dim header As New TextHeaderFooter With {
.CenterText = "This is the header!"
}
Dim footer As New TextHeaderFooter With {
.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 mmHow 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.
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>")Output
The 30mm side and 25mm top and bottom margins apply to the header, footer, and body together.
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:
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")Output
The blue HTML header expands to fit its content and the body shifts down to make room.
The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.
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 thetitletag 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.
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}"
}This header and footer carry every available placeholder. Each one resolves to its live value when the PDF renders, so the page shows its number and total, the date and time, the source URL, and both the HTML and PDF titles.
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.
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)Output
HTML headers and footers rendered from HtmlHeaderFooter fragments.
How do I control HTML header/footer margins?
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.
// 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)Passing 0 for the margin arguments makes the HTML header and footer span the full page width with no indent.
How Do I Detect If a Header/Footer Overlaps Existing Content?
When you add an HTML header or footer to an existing PDF, IronPDF can check whether the header/footer band would overlap content already on the page, and either warn or throw. Pass a ContentOverlapBehavior to the AddHtmlHeaders/AddHtmlFooters overloads, or set it at render time with RenderingOptions.HeaderFooterOverlapBehavior.
ContentOverlapBehavior | Behavior |
|---|---|
Ignore | No overlap check; the header/footer is stamped as-is. This is the default. |
Warn | Logs a warning listing the affected page indexes, then still adds the header/footer. |
Throw | Throws InvalidOperationException before stamping, listing the affected page indexes. |
using IronPdf;
// Start from an existing PDF (rendered here for the example)
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Invoice</h1><p>Line items and totals...</p>");
// A fixed MaxHeight is required for overlap detection to run
HtmlHeaderFooter footer = new HtmlHeaderFooter
{
HtmlFragment = "<div>Confidential</div>",
MaxHeight = 25,
};
// Throw an InvalidOperationException if the footer would overlap existing page content
pdf.AddHtmlFooters(footer, ContentOverlapBehavior.Throw);
pdf.SaveAs("invoice-with-footer.pdf");
Output
The Confidential HTML footer stamped below the invoice content.
Throw that does not fire is not a guarantee that the output is free of overlap. It also requires an explicit HtmlHeaderFooter.MaxHeight. Without one, that side auto-sizes and overlap detection is skipped for it.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 can I add headers and footers to a PDF using C# and IronPDF?
You can add headers and footers to a PDF using IronPDF by employing methods such as `AddTextHeaders` and `AddTextFooters` for simple text, or `AddHtmlHeaders` and `AddHtmlFooters` to include HTML content with full CSS styling support.
What methods are available for adding text headers and footers in IronPDF?
In IronPDF, you can use the `AddTextHeaders` and `AddTextFooters` methods along with `RenderingOptions` in `ChromePdfRenderer` to add text headers and footers. This allows you to include page numbers, dates, and other metadata in a straightforward manner.
How does IronPDF handle HTML headers and footers?
IronPDF allows you to add HTML headers and footers using the `HtmlHeaderFooter` class. This enables you to customize headers and footers with images, complex layouts, and styled content, offering maximum flexibility.
Can I customize the properties of text headers and footers?
Yes, with IronPDF's `TextHeaderFooter` class, you can set properties such as the text alignment (left, center, right), font type and size, and include divider lines with custom colors to match your document style.
How do I apply margins to headers and footers in IronPDF?
String headers and footers can have customized margins by using different overloads of `AddTextHeaders`, `AddTextFooters`, `AddHtmlHeaders`, and `AddHtmlFooters` methods, or by setting margin values in the `RenderingOptions` of `ChromePdfRenderer`.
Is it possible to detect overlap of headers and footers with existing content in IronPDF?
Yes, IronPDF can check for overlap using the `ContentOverlapBehavior` option when adding headers and footers. This enables you to receive warnings, throws an exception, or ignore the overlap based on your needs.
What are the performance considerations when choosing between text and HTML headers and footers?
Text headers and footers render faster as they don't require HTML parsing or CSS processing, making them ideal for performance-critical applications. HTML headers and footers provide greater customization but may require slightly more processing time.
How do I include metadata like page numbers and dates in headers and footers?
IronPDF supports metadata placeholders such as `{page}`, `{total-pages}`, `{date}`, and `{time}` in text headers and footers, which dynamically resolve to present values during PDF rendering.
What is Dynamic Margin Sizing in IronPDF?
Dynamic Margin Sizing in IronPDF automatically adjusts the height of the headers and footers based on the content. This feature helps accommodate variant header/footer content and ensures smooth integration with the main document body.
Which fonts are supported by default for PDF headers and footers in IronPDF?
IronPDF supports a wide range of standard fonts like Arial, Times New Roman, and Helvetica by default. To see the complete list, refer to the IronPDF API Reference for available font types.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.