Add HTML Headers & Footers
HTML headers and footers provide a flexible method for creating dynamic headers and footers for your PDF documents. By adding headers and footers through this method, developers have complete control over how their headers and footers look, as they are rendered as independent HTML documents capable of containing their own assets and stylesheets.
Steps to Add Custom HTML Headers and Footers in a PDF with IronPDF
// Create a new instance of the ChromePdfRenderer class.
var renderer = new IronPdf.ChromePdfRenderer();
// Define a footer with the HtmlHeaderFooter class
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>", // Include page numbering
MaxHeight = 20, // Set maximum height for the footer
BaseUrl = new Uri(@"C:\assets\images\").AbsoluteUri // Base URL for resolving relative paths
};
// Define a header (optional)
// renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
// {
// HtmlFragment = "<img src='logo.png' alt='Company Logo' />",
// MaxHeight = 50,
// BaseUrl = new Uri(@"C:\assets\images\").AbsoluteUri
// };
// Set margins to prevent overlap between header/footer and main content
renderer.RenderingOptions.MarginBottom = 30; // Adjust bottom margin for footer
renderer.RenderingOptions.MarginTop = 50; // Adjust top margin for header
// Create a new instance of the ChromePdfRenderer class.
var renderer = new IronPdf.ChromePdfRenderer();
// Define a footer with the HtmlHeaderFooter class
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>", // Include page numbering
MaxHeight = 20, // Set maximum height for the footer
BaseUrl = new Uri(@"C:\assets\images\").AbsoluteUri // Base URL for resolving relative paths
};
// Define a header (optional)
// renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
// {
// HtmlFragment = "<img src='logo.png' alt='Company Logo' />",
// MaxHeight = 50,
// BaseUrl = new Uri(@"C:\assets\images\").AbsoluteUri
// };
// Set margins to prevent overlap between header/footer and main content
renderer.RenderingOptions.MarginBottom = 30; // Adjust bottom margin for footer
renderer.RenderingOptions.MarginTop = 50; // Adjust top margin for header
' Create a new instance of the ChromePdfRenderer class.
Dim renderer = New IronPdf.ChromePdfRenderer()
' Define a footer with the HtmlHeaderFooter class
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter With {
.HtmlFragment = "<center><i>{page} of {total-pages}</i></center>",
.MaxHeight = 20,
.BaseUrl = (New Uri("C:\assets\images\")).AbsoluteUri
}
' Define a header (optional)
' renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
' {
' HtmlFragment = "<img src='logo.png' alt='Company Logo' />",
' MaxHeight = 50,
' BaseUrl = new Uri(@"C:\assets\images\").AbsoluteUri
' };
' Set margins to prevent overlap between header/footer and main content
renderer.RenderingOptions.MarginBottom = 30 ' Adjust bottom margin for footer
renderer.RenderingOptions.MarginTop = 50 ' Adjust top margin for header
To begin with, you first need to create an instance of the ChromePdfRenderer
class, which handles the rendering of HTML content into a pixel-perfect PDF document.
Next, define a footer using the HtmlHeaderFooter
class, where you specify the MaxHeight
, HTML content for the footer (which in our case includes page numbering), and base URL for image resolution. The footer is styled to display centered page information.
To avoid overlap between the footer and the PDF's main content, set a bottom margin using the MarginBottom
property. Similarly, create a header that includes an image (such as a logo) by using the HtmlHeaderFooter
class. Here we have set up a BaseUrl
to the directory containing your image asset, allowing for proper image resolution during rendering.
Finally, use the MarginTop
property to set a top margin that prevents overlap between the header and the content. This example demonstrates how easy it is to implement custom HTML headers and footers in your PDF documents with IronPDF.
Click here to view the How-to Guide, including examples, sample code, and files