ExpertPdf Html To Pdf Converter – Headers and Footers

ExpertPdf provides full support for pdf document headers and footers. In order to show or hide the header or footer on the rendered document you have to set the ShowHeader and ShowFooter properties of the PdfDocumentOptions property of the PdfConverter class. For example, to add both footer and header to the generated document you can use the following code:


PdfConverter pdfConverter = new PdfConverter();
pdfConverter.PdfDocumentOptions.ShowHeader = true;
pdfConverter.PdfDocumentOptions.ShowFooter = true;

The aspect of the header and footer can be controlled with the PdfHeaderOptions and PdfFooterOptions properties of the PdfConverter object.

Predefined Header and Footer Elements

For the header you can set a title and a subtitle, add a image in a specified position and draw a horizontal line under the header, change the text font and size, change the background color of the header. Below you can see a sample code to set the header options:

pdfConverter.PdfHeaderOptions.HeaderBackColor = Color.WhiteSmoke;
pdfConverter.PdfHeaderOptions.HeaderHeight = 50;
pdfConverter.PdfHeaderOptions.HeaderText = "Title";
pdfConverter.PdfHeaderOptions.HeaderTextColor = Color.Black;
pdfConverter.PdfHeaderOptions.HeaderTextFontType = PdfFontType.Helvetica;
pdfConverter.PdfHeaderOptions.HeaderTextFontSize = 18;
pdfConverter.PdfHeaderOptions.HeaderTextYLocation = 5;
pdfConverter.PdfHeaderOptions.HeaderSubtitleText = "Subtitle";
pdfConverter.PdfHeaderOptions.HeaderSubtitleTextColor = Color.Black;
pdfConverter.PdfHeaderOptions.HeaderSubtitleTextFontType = PdfFontType.Helvetica;
pdfConverter.PdfHeaderOptions.HeaderSubtitleTextFontSize = 12;
pdfConverter.PdfHeaderOptions.HeaderTitleSubtitleYSpacing = 7;
pdfConverter.PdfHeaderOptions.HeaderImageLocation = new PointF(0, 0);
pdfConverter.PdfHeaderOptions.HeaderImage = Image.FromFile(logoImageFullPath);
pdfConverter.PdfHeaderOptions.DrawHeaderLine = true;

The dimensions are specified in points and a point is 1/72 inches. The A4 page size in points is 595×842. At a screen resolution of 96 dpi, a A4 PDF page has 794 pixels in width and 1123 pixels in height.

For the footer you can set the text, to show or not the page numbering, the text that appears before the page number, the font text and color, the footer background color and to draw or not a line above the footer. Below you can see a sample code to set the footer options:

pdfConverter.PdfFooterOptions.FooterText = "Footer text";
pdfConverter.PdfFooterOptions.FooterBackColor = Color.WhiteSmoke;
pdfConverter.PdfFooterOptions.FooterHeight = 40;
pdfConverter.PdfFooterOptions.FooterTextColor = Color.Black;
pdfConverter.PdfFooterOptions.FooterTextFontType = PdfFontType.HelveticaOblique;
pdfConverter.PdfFooterOptions.FooterTextFontSize = 8;
pdfConverter.PdfFooterOptions.DrawFooterLine = true;
pdfConverter.PdfFooterOptions.PageNumberText = "Page";
pdfConverter.PdfFooterOptions.PageNumberTextColor = Color.Black;
pdfConverter.PdfFooterOptions.PageNumberTextFontType = PdfFontType.HelveticaBold;
pdfConverter.PdfFooterOptions.PageNumberTextFontSize = 10;
pdfConverter.PdfFooterOptions.ShowPageNumber = true;

The dimensions are specified in points and a point is 1/72 inches. The A4 page size in points is 595×842. At a screen resolution of 96 dpi, a A4 PDF page has 794 pixels in width and 1123 pixels in height.

Custom Header and Footer Elements

Besides the predefined elements like title, subtitle, page numbering the converter allows you add any number of custom elements like HtmlToPdfArea, ImageArea and TextArea that can be added in any position in the header and footer.

The PdfHeaderOptions and PdfFooterOptions define a property for each type of object. For example the HtmlToPdfArea property can be initialized with a HtmlToPdfArea object that will be automatically rendered in the header or footer when the PDF document is generated. The same for the TextArea and ImageArea properties. However, the number of possible custom elements is not limited to one element of each type, it is possible to add any number of HtmmlToPdfArea, TextArea and ImageArea elements to the header and footer using the following methods of PdfHeaderOptions and PdfFooterOptions:

public void AddHtmlToPdfArea(HtmlToPdfArea htmlToPdfArea)
public void AddImageArea(ImageArea imageArea)
public void AddTextArea(TextArea textArea)

The Page Numbering can also be added in position in the header and footer as a TextArea element containing &p; and &P; placeholders for current page number and total number of pages.
Bellow is a complete example for adding HTML in header and footer and page numbering in footer. The code was taken from the Getting Started sample application for ASP.NET:

private void AddHeader(PdfConverter pdfConverter)
{
string thisPageURL = HttpContext.Current.Request.Url.AbsoluteUri;
string headerAndFooterHtmlUrl = thisPageURL.Substring(0, thisPageURL.LastIndexOf('/')) + "/HeaderAndFooterHtml.htm";

//enable header
pdfConverter.PdfDocumentOptions.ShowHeader = true;
// set the header height in points
pdfConverter.PdfHeaderOptions.HeaderHeight = 60;
// set the header HTML area
pdfConverter.PdfHeaderOptions.HtmlToPdfArea = new HtmlToPdfArea(headerAndFooterHtmlUrl);
pdfConverter.PdfHeaderOptions.HtmlToPdfArea.EmbedFonts = cbEmbedFonts.Checked;
}

private void AddFooter(PdfConverter pdfConverter)
{
string thisPageURL = HttpContext.Current.Request.Url.AbsoluteUri;
string headerAndFooterHtmlUrl = thisPageURL.Substring(0, thisPageURL.LastIndexOf('/')) + "/HeaderAndFooterHtml.htm";

//enable footer
pdfConverter.PdfDocumentOptions.ShowFooter = true;
// set the footer height in points
pdfConverter.PdfFooterOptions.FooterHeight = 60;
//write the page number
pdfConverter.PdfFooterOptions.TextArea = new TextArea(0, 30, "This is page &p; of &P; ",
new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 10, System.Drawing.GraphicsUnit.Point));
pdfConverter.PdfFooterOptions.TextArea.EmbedTextFont = true;
pdfConverter.PdfFooterOptions.TextArea.TextAlign = HorizontalTextAlign.Right;
// set the footer HTML area
pdfConverter.PdfFooterOptions.HtmlToPdfArea = new HtmlToPdfArea(headerAndFooterHtmlUrl);
pdfConverter.PdfFooterOptions.HtmlToPdfArea.EmbedFonts = cbEmbedFonts.Checked;
}

For more details about ExpertPdf visit www.html-to-pdf.net.