Page Header and Footer

Overview

You can render additional header or footer on the output PDF page either by setting HtmlToPdfOptions's HeaderHtmlFormat or FooterHtmlFormat property, or handle the converter's AfterRenderPage event to render any additional contents on the page.

Using HeaderHtmlFormat/FooterHtmlFormat

Using AfterRenderPage

Adding Page Header/Footer After the Conversion

Using HeaderHtmlFormat/FooterHtmlFormat

The easiest way to render header and footer is to set HtmlToPdfOptions's HeaderHtmlFormat and FooterHtmlFormat property. The following code renders header text "Chapter 3 - Have Fun!" as page header and "http://somefunsite.com" as footer:

//Set the page header and footer to fixed text
EO.Pdf.HtmlToPdf.Options.HeaderHtmlFormat = "Chapter 3 - Have Fun!";
EO.Pdf.HtmlToPdf.Options.FooterHtmlFormat = "http://somefunsite.com";

HeaderHtmlFormat and FooterHtmlFormat can include HTML markups. For example, the following code aligns the header to the center (by default it aligns to the left):

//Page header with HTML markup
EO.Pdf.HtmlToPdf.Options.HeaderHtmlFormat = 
    "<div style='text-align:center'>Chapter 3 - Have Fun!</div>";

HeaderHtmlFormat and FooterHtmlFormat can also include "variables", which will be replaced with their value at runtime. For example, the following code uses the "page_number" variable, at runtime it will be replaced with the actual page number:

//Page header with "variable"
EO.Pdf.HtmlToPdf.Options.HeaderHtmlFormat = "Page {page_number}";

See HeaderHtmlFormat for a complete list of all supported page variables.

The top position of the header/footer area is specified by HeaderHtmlPosition and FooterHtmlPosition respecitvely. The left and right boundary of the header and footer area is the same as the page's OutputArea.

Using AfterRenderPage

You can also supply a delegate to be called after every page is generated. This gives you a chance to render any additional output on the page. For example, the following code renders an image on top of every page to create a "Letter Head" effect:

//Provide AfterRenderPage handler
EO.Pdf.HtmlToPdf.Options.AfterRenderPage =
    new EO.Pdf.PdfPageEventHandler(On_AfterRenderPage);

//This function is called after every page is created
private void On_AfterRenderPage(
    object sender, EO.Pdf.PdfPageEventArgs e)
{
    //Set the output area to the top portion of the page. Note
    //this does not change the output area of the original
    //conversion from which we are called
    EO.Pdf.HtmlToPdf.Options.OutputArea = new RectangleF(0, 0, 8.5f, 1f);
    
    //Render an image and a horizontal line. Note the
    //second argument is the PdfPage object
    EO.Pdf.HtmlToPdf.ConvertHtml(@"
        <img src='http://www.essentialobjects.com/images/logo.gif' >
        <br />", 
        e.Page);
}

Adding Page Header/Footer After the Conversion

You can also add page header/footer after the conversion, either using HTML to PDF or PDF Creator API. For example, the following code add footer to each page after the conversion:

//Convert the main contents into a PdfDocument object
PdfDocument doc = new PdfDocument();
HtmlToPdf.ConvertUrl(url, doc);

//Add footer to each page with a loop
for (int i = 0; i < doc.Pages.Count; i++)
{
    //Set the output area to the footer area
    HtmlToPdf.Options.OutputArea = new RectangleF(0, 10, 8.5f, 1);
    
    //Output whatever footer contents in the footer area
    //Note that the second argument is a PdfPage object
    HtmlToPdf.ConvertHtml("footer content", doc.Pages[i]);
}

It is also possible to use the PDF Creator API to add additional output on each page. In fact that is the recommended method since while PDF Creator API lacks the power and flexibility of a full featured HTML parser and render, it is much simpler thus runs much faster, which makes it especially suitable for generating relatively simple output such as page header and footer.