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.
How to Add Headers and Footers
- Download the C# library to add headers and footers
- Load an existing PDF or render a new one
- Use the
AddTextHeaders
andAddTextFooters
methods to add text headers and footers - Use the
AddHtmlHeaders
andAddHtmlFooters
methods to add HTML headers and footers - Add headers and footers at rendering time by configuring the rendering options
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronPDF on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming PDF with C#.
Install-Package IronPdf
Consider installing the IronPDF DLL directly. Download and manually install it for your project or GAC form: IronPdf.zip
Manually install into your project
Download DLLAdd a Text Header/Footer Example
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
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
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
}
Output Text Header
You can see what font types are available by default in the API Reference.
Set Margins for Text Header/Footer
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
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;
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.All;
// Add header and footer to renderer
renderer.RenderingOptions.TextHeader = header;
renderer.RenderingOptions.TextFooter = footer;
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>");
IRON VB CONVERTER ERROR developers@ironsoftware.com
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")
Metadata to Text Header/Footer
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 Add Page Numbers 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}"
}
Add HTML Header/Footer Example
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)
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)
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>")
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.