Add HTML Headers & Footers

IronPDF allows the developer to modify PDF documents to use customized headers and footers. This code example shows how developers can achieve this using the ChromePdfRenderOptions class along with the HtmlHeaderFooter class.

The HtmlHeaderFooter class allows the developer to customize a PDF's header or footer using HTML, as opposed to just using plain text (as is the case with the TextHeaderFooter class). Specify the HTML markup for the header or footer by calling the addHtmlFragment method on an instance of the class. This method accepts a string containing valid HTML5, CSS3, and JavaScript (ECMAScript 6). IronPDF will interpret the content in this string as rich hypertext markup, displaying it in resulting PDF documents in the same manner that a web browser would.

The developer can use this class to create headers that incorporate images, colors, special typefaces, and other, more complex design elements.

For more traditional and simplified text-based header/footer customizations, consider opting for the TextHeaderFooter class instead.

To learn more about customizing PDF headers and footers and for detailed documentation, visit the IronPDF Documentation.

// Import necessary libraries
using IronPdf;

class IronPdfHeaderFooterExample
{
    static void Main()
    {
        // Initialize a PDF document
        var pdfDocument = PdfDocument.FromFile("example.pdf");

        // Create an instance of `ChromePdfRenderOptions`
        var renderOptions = new ChromePdfRenderOptions();

        // Create a customized HTML header
        var htmlHeader = new HtmlHeaderFooter
        {
            Height = 50, // Set the height of the header
            HtmlFragment = @"
                <html>
                    <body style='font-family:Arial; font-size:12px;'>
                        <div style='width:100%; text-align:center;'>
                            <h1 style='color:#0073e6;'>Custom Header</h1>
                        </div>
                    </body>
                </html>"
        };

        // Assign the customized header to the document
        renderOptions.FirstPageHeader = htmlHeader;
        renderOptions.Header = htmlHeader;

        // Apply render options and save the updated PDF
        pdfDocument.RenderOptions = renderOptions;
        pdfDocument.SaveAs("updated_example.pdf");
    }
}
// Import necessary libraries
using IronPdf;

class IronPdfHeaderFooterExample
{
    static void Main()
    {
        // Initialize a PDF document
        var pdfDocument = PdfDocument.FromFile("example.pdf");

        // Create an instance of `ChromePdfRenderOptions`
        var renderOptions = new ChromePdfRenderOptions();

        // Create a customized HTML header
        var htmlHeader = new HtmlHeaderFooter
        {
            Height = 50, // Set the height of the header
            HtmlFragment = @"
                <html>
                    <body style='font-family:Arial; font-size:12px;'>
                        <div style='width:100%; text-align:center;'>
                            <h1 style='color:#0073e6;'>Custom Header</h1>
                        </div>
                    </body>
                </html>"
        };

        // Assign the customized header to the document
        renderOptions.FirstPageHeader = htmlHeader;
        renderOptions.Header = htmlHeader;

        // Apply render options and save the updated PDF
        pdfDocument.RenderOptions = renderOptions;
        pdfDocument.SaveAs("updated_example.pdf");
    }
}
' Import necessary libraries
Imports IronPdf

Friend Class IronPdfHeaderFooterExample
	Shared Sub Main()
		' Initialize a PDF document
		Dim pdfDocument = PdfDocument.FromFile("example.pdf")

		' Create an instance of `ChromePdfRenderOptions`
		Dim renderOptions = New ChromePdfRenderOptions()

		' Create a customized HTML header
		Dim htmlHeader = New HtmlHeaderFooter With {
			.Height = 50,
			.HtmlFragment = "
                <html>
                    <body style='font-family:Arial; font-size:12px;'>
                        <div style='width:100%; text-align:center;'>
                            <h1 style='color:#0073e6;'>Custom Header</h1>
                        </div>
                    </body>
                </html>"
		}

		' Assign the customized header to the document
		renderOptions.FirstPageHeader = htmlHeader
		renderOptions.Header = htmlHeader

		' Apply render options and save the updated PDF
		pdfDocument.RenderOptions = renderOptions
		pdfDocument.SaveAs("updated_example.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel