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

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