Click or drag to resize
Pdf Library for .NET
Headers and Footers with SelectPdf HtmlToPdf Converter

The Headers And Footers page describes how to add headers and footers to a PdfDocument using Pdf Templates. That is the approach to be used if the html to pdf conversion is done using PdfHtmlElement objects as described here.

When the pdf conversion is done using the main HtmlToPdf converter object, there is another way of adding headers and footers to the generated pdf document. That is done using the Header and Footer properties of the HtmlToPdf converter object.

Header and Footer Options are covered in more details in the following sections:

To display a custom header in the generated pdf, the DisplayHeader property of the HtmlToPdfOptions object must be set to true. If the DisplayHeader property of the Options object is false, the header will not be displayed and all the options set for the header will have no effect.

The header of the pdf document generated from the html to pdf conversion can be customized using the Header property of the HtmlToPdf converter object. That is an instance of PdfHeader class and has the following properties:

  • Height - The height of the pdf document header.

  • DisplayOnFirstPage - Controls the visibility of the header on the first page of the generated pdf document.

  • DisplayOnOddPages - Controls the visibility of the header on the odd numbered pages of the generated pdf document.

  • DisplayOnEvenPages - Controls the visibility of the header on the even numbered pages of the generated pdf document.

  • FirstPageNumber - Controls the page number for the first page being rendered.

  • TotalPagesOffset - Controls the total number of pages offset in the generated pdf document.

To add content to the pdf header, PdfHeader class exposes the Add(PdfSectionElement) method. That can be used to add 3 types of objects to the header:

Page numbers can be added to pdf document headers using PdfTextSection objects. The page number is displayed setting a {page_number} placeholder in a Text property of a PdfTextSection object that can be added to the header. By default the page numbers start with 1. This can be changed using the FirstPageNumber property of the PdfHeader object.

The total number of pages is displayed setting a {total_pages} placeholder in the Text property of a PdfTextSection object that can be added to the header. The total number of pages can be incremented with a value specified by the TotalPagesOffset property of the PdfHeader object. This could be useful when the generated pdf will be merged with other documents.

The following code sample shows how to add a custom header to the pdf document generated from a html to pdf conversion:

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// header settings
converter.Options.DisplayHeader = true;
converter.Header.DisplayOnFirstPage = true;
converter.Header.DisplayOnOddPages = true;
converter.Header.DisplayOnEvenPages = true;
converter.Header.Height = 50;

// add some html content to the header
PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl);
headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
converter.Header.Add(headerHtml);

// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();

To display a custom footer in the generated pdf, the DisplayFooter property of the HtmlToPdfOptions object must be set to true. If the DisplayFooter property of the Options object is false, the footer will not be displayed and all the options set for the footer will have no effect.

The footer of the pdf document generated from the html to pdf conversion can be customized using the Footer property of the HtmlToPdf converter object. That is an instance of PdfFooter class and has the following properties:

  • Height - The height of the pdf document footer.

  • DisplayOnFirstPage - Controls the visibility of the footer on the first page of the generated pdf document.

  • DisplayOnOddPages - Controls the visibility of the footer on the odd numbered pages of the generated pdf document.

  • DisplayOnEvenPages - Controls the visibility of the footer on the even numbered pages of the generated pdf document.

  • FirstPageNumber - Controls the page number for the first page being rendered.

  • TotalPagesOffset - Controls the total number of pages offset in the generated pdf document.

To add content to the pdf footer, PdfFooter class exposes the Add(PdfSectionElement) method. That can be used to add 3 types of objects to the footer:

Page numbers can be added to pdf document footer using PdfTextSection objects. The page number is displayed setting a {page_number} placeholder in a Text property of a PdfTextSection object that can be added to the footer. By default the page numbers start with 1. This can be changed using the FirstPageNumber property of the PdfFooter object.

The total number of pages is displayed setting a {total_pages} placeholder in the Text property of a PdfTextSection object that can be added to the footer. The total number of pages can be incremented with a value specified by the TotalPagesOffset property of the PdfFooter object. This could be useful when the generated pdf will be merged with other documents.

The following code sample shows how to add a custom footer to the pdf document generated from a html to pdf conversion:

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// footer settings
converter.Options.DisplayFooter = true;
converter.Footer.DisplayOnFirstPage = true;
converter.Footer.DisplayOnOddPages = true;
converter.Footer.DisplayOnEvenPages = true;
converter.Footer.Height = 50;

// add some html content to the footer
PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl);
footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
converter.Footer.Add(footerHtml);

// page numbers can be added using a PdfTextSection object
PdfTextSection text = new PdfTextSection(0, 10, "Page: {page_number} of {total_pages}  ", new System.Drawing.Font("Arial", 8));
text.HorizontalAlign = PdfTextHorizontalAlign.Right;
converter.Footer.Add(text);

// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();
Setting a Different Header or Footer for a Specific Pdf Page

With Select.Pdf Library for .NET it is possible to customize headers and footers of the generated pdf document and have different headers and footers on specific pages. This can be done using CustomHeader and CustomFooter properties of the PdfPage objects from the generated pdf document.

The following sample code shows how to customize the header of the 3rd page of a generated pdf document:

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// header settings
converter.Options.DisplayHeader = true;
converter.Header.DisplayOnFirstPage = true;
converter.Header.DisplayOnOddPages = true;
converter.Header.DisplayOnEvenPages = true;
converter.Header.Height = 50;

// add some html content to the header
PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl);
headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
converter.Header.Add(headerHtml);

// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);

// custom header on page 3
if (doc.Pages.Count >= 3)
{
    PdfPage page = doc.Pages[2];

    PdfTemplate customHeader = doc.AddTemplate(page.PageSize.Width, headerHeight);
    PdfHtmlElement customHtml = new PdfHtmlElement(
        "<div><b>This is the custom header that will appear only on page 3!</b></div>", 
        string.Empty);
    customHeader.Add(customHtml);

    page.CustomHeader = customHeader;
}

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();
See Also