Headers and Footers |
The header and the footer are shown in the generated document when the ShowHeader and ShowFooter properties of the PdfDocumentOptions object are true. For example, to add both a header and a footer to the generated document:
PdfConverter pdfConverter = new PdfConverter(); pdfConverter.PdfDocumentOptions.ShowHeader = true; pdfConverter.PdfDocumentOptions.ShowFooter = true;
The content and the look of the header and the footer are set with the PdfHeaderOptions and PdfFooterOptions properties of the PdfConverter object. The HTML content is placed between the header and the footer: their heights are taken from the page.
The header can have a title and a subtitle, an image at a given position and a line under the header; the font, size and color of the texts and the background color of the header can be changed. The image is given as the bytes of an image file (PNG, JPEG, BMP...):
pdfConverter.PdfHeaderOptions.HeaderBackColor = Color.LightGray; 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 = File.ReadAllBytes(logoImageFullPath); pdfConverter.PdfHeaderOptions.DrawHeaderLine = true;
The dimensions are specified in points; a point is 1/72 inch. The A4 page size in points is 595 x 842. At 96 pixels per inch, an A4 page is 794 pixels wide and 1123 pixels high.
The footer can have a text, the page numbers with a text before them, a line above the footer and a background color; the font and color of the texts can be changed:
pdfConverter.PdfFooterOptions.FooterText = "Footer text"; pdfConverter.PdfFooterOptions.FooterBackColor = Color.LightGray; 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;
PageNumberingFormatString replaces the text of the page numbers with a format of your own, for example "Page &p; of &P;" (see the placeholders below). PageNumberingStartIndex and PageNumberingPageCountIncrement change the first page number and the total page count. ShowOnFirstPage, ShowOnOddPages and ShowOnEvenPages choose the pages a header or a footer is shown on.
Besides the predefined title, subtitle and page numbers, any number of custom elements can be placed at any position in the header and the footer:
HtmlToPdfArea - HTML from a URL or a string, rendered by Chromium with options of its own (page loading, timeouts, CSS, scripts) and drawn on every page.
ImageArea - an image, given as the bytes of an image file or a file path.
WordToPdfArea - the first page of a Word document (docx, doc, rtf...), for example a letterhead.
PdfHeaderOptions and PdfFooterOptions have a property for one element of the first three types: HtmlToPdfArea, TextArea and ImageArea. Any number of elements can be added with the methods below:
public void AddHtmlToPdfArea(HtmlToPdfArea htmlToPdfArea); public void AddImageArea(ImageArea imageArea); public void AddTextArea(TextArea textArea); public void AddWordToPdfArea(WordToPdfArea wordToPdfArea);
Page numbers can also be placed anywhere in the header and the footer with a TextArea element whose text contains the &p; and &P; placeholders, replaced with the current page number and the total number of pages.
The example below adds HTML to the header and to the footer, and the page numbers to the footer:
private void AddHeader(PdfConverter pdfConverter, string headerAndFooterHtmlUrl) { // enable the 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); } private void AddFooter(PdfConverter pdfConverter, string headerAndFooterHtmlUrl) { // enable the footer pdfConverter.PdfDocumentOptions.ShowFooter = true; // set the footer height in points pdfConverter.PdfFooterOptions.FooterHeight = 60; // write the page numbers pdfConverter.PdfFooterOptions.TextArea = new TextArea(0, 30, "This is page &p; of &P; ", new Font("Arial", 10)); pdfConverter.PdfFooterOptions.TextArea.EmbedTextFont = true; pdfConverter.PdfFooterOptions.TextArea.TextAlign = HorizontalTextAlign.Right; // set the footer HTML area pdfConverter.PdfFooterOptions.HtmlToPdfArea = new HtmlToPdfArea( headerAndFooterHtmlUrl); }
A Word document as the header, for example a letterhead: