Click or drag to resize
Pdf Library for .NET

Headers and Footers with SelectPdf HtmlToPdf Converter

Select.Pdf Html To Pdf Converter can add headers and footers to the generated pdf document 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();
See Also