Add Headers/Footers on Specific Pages

Adding headers and footers to freshly rendered PDFs or existing PDF documents is straightforward with IronPDF.

You can use the addHtmlHeader method to add a header and the addHtmlFooter method to add a footer. These methods require an object that contains the following properties: dividerLine, dividerLineColor, htmlFragment, loadStylesAndCSSFromMainHtmlDocument, and maxHeight.

  • dividerLine: Applies a divider line after the header or footer.
  • dividerLineColor: Customizes the divider color.
  • htmlFragment: Specifies the HTML string to be used for the header or footer.
  • loadStylesAndCSSFromMainHtmlDocument: Enables loading CSS from the main HTML document. This feature only works when rendering from HTML to PDF.
  • maxHeight: Sets a maximum height for the header and footer.

Below is an example demonstrating how you could use IronPDF to add headers and footers:

// Import the necessary libraries
using IronPdf;

class PdfExample
{
    static void Main()
    {
        // Create a new PDF document
        var pdf = new PdfDocument();

        // Add HTML content to the PDF
        pdf.AddHtml("<h1>Hello, World!</h1>");

        // Define header properties
        var header = new HtmlHeaderFooter()
        {
            HtmlFragment = "<h2>Header Content</h2>",
            DividerLine = true,
            DividerLineColor = "#000000",
            LoadStylesAndCSSFromMainHtmlDocument = false,
            MaxHeight = 50
        };

        // Define footer properties
        var footer = new HtmlHeaderFooter()
        {
            HtmlFragment = "<p>Page footer</p>",
            DividerLine = true,
            DividerLineColor = "#888888",
            LoadStylesAndCSSFromMainHtmlDocument = false,
            MaxHeight = 50
        };

        // Apply the header and footer to the PDF
        pdf.AddHtmlHeader(header, "all"); // Specify "all" to apply to all pages or specify a particular page range
        pdf.AddHtmlFooter(footer, "all");

        // Save the PDF to a file
        pdf.SaveAs("DocumentWithHeaderFooter.pdf");
    }
}
// Import the necessary libraries
using IronPdf;

class PdfExample
{
    static void Main()
    {
        // Create a new PDF document
        var pdf = new PdfDocument();

        // Add HTML content to the PDF
        pdf.AddHtml("<h1>Hello, World!</h1>");

        // Define header properties
        var header = new HtmlHeaderFooter()
        {
            HtmlFragment = "<h2>Header Content</h2>",
            DividerLine = true,
            DividerLineColor = "#000000",
            LoadStylesAndCSSFromMainHtmlDocument = false,
            MaxHeight = 50
        };

        // Define footer properties
        var footer = new HtmlHeaderFooter()
        {
            HtmlFragment = "<p>Page footer</p>",
            DividerLine = true,
            DividerLineColor = "#888888",
            LoadStylesAndCSSFromMainHtmlDocument = false,
            MaxHeight = 50
        };

        // Apply the header and footer to the PDF
        pdf.AddHtmlHeader(header, "all"); // Specify "all" to apply to all pages or specify a particular page range
        pdf.AddHtmlFooter(footer, "all");

        // Save the PDF to a file
        pdf.SaveAs("DocumentWithHeaderFooter.pdf");
    }
}
$vbLabelText   $csharpLabel

Afterward, you can specify the second parameter, which is the page number to which the header or footer will be applied. The specification can be for a single page, multiple pages, or all pages using the string "all". If the page number has not been specified, the method will apply the header or footer to all pages.