The Document property of the HiQPdf.HtmlToPdf class having the PdfDocumentControl type is used to control most of the properties of the generated PDF document like page size, page orientation, page margins, header and footer, outlines, font embedding, the document standard and the document security.
The Header and Footer properties of the HiQPdf.PdfDocumentControl class control whether the header and footer are visible and also control their content. The PdfHeader and PdfFooter classes have a PdfHeaderLayout(PdfObject) method which can be used to layout various PDF objects in header or footer like text, images, HTML and graphics.
The PdfText objects laid out in header and footer can use two page numbering place holders: {CrtPage} which will be replaced by the current page number and the {PageCount} which will be replaced by the total number of pages in the generated PDF document.
In this demo you can see how to enable the header and footer and how to add HTML, text, images and page numbering to header and footer.
private void buttonConvertToPdf_Click(object sender, EventArgs e) { // create the HTML to PDF converter HtmlToPdf htmlToPdfConverter = new HtmlToPdf(); // set header and footer SetHeader(htmlToPdfConverter.Document); SetFooter(htmlToPdfConverter.Document); // convert URL string url = textBoxUrl.Text; pdfFile = Application.StartupPath + @"\DemoOut\ConvertUrl.pdf"; // ConvertUrlToFile() is called to convert the html document and save the resulted PDF into a file on disk // Alternatively, ConvertUrlToMemory() can be called to save the resulted PDF in a buffer in memory htmlToPdfConverter.ConvertUrlToFile(url, pdfFile); } private void SetHeader(PdfDocumentControl htmlToPdfDocument) { // enable header display htmlToPdfDocument.Header.Enabled = checkBoxAddHeader.Checked; if (!htmlToPdfDocument.Header.Enabled) return; // set header height htmlToPdfDocument.Header.Height = 50; float pdfPageWidth = htmlToPdfDocument.PageOrientation == PdfPageOrientation.Portrait ? htmlToPdfDocument.PageSize.Width : htmlToPdfDocument.PageSize.Height; float headerWidth = pdfPageWidth - htmlToPdfDocument.Margins.Left - htmlToPdfDocument.Margins.Right; float headerHeight = htmlToPdfDocument.Header.Height; // set header background color htmlToPdfDocument.Header.BackgroundColor = Color.WhiteSmoke; string headerImageFile = Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo.png"; PdfImage logoHeaderImage = new PdfImage(5, 5, 40, Image.FromFile(headerImageFile)); htmlToPdfDocument.Header.Layout(logoHeaderImage); // layout HTML in header PdfHtml headerHtml = new PdfHtml(50, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic""> Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null); headerHtml.FitDestHeight = true; headerHtml.FontEmbedding = checkBoxFontEmbedding.Checked; htmlToPdfDocument.Header.Layout(headerHtml); // create a border for header PdfRectangle borderRectangle = new PdfRectangle(1, 1, headerWidth - 2, headerHeight - 2); borderRectangle.LineStyle.LineWidth = 0.5f; borderRectangle.ForeColor = Color.Navy; htmlToPdfDocument.Header.Layout(borderRectangle); } private void SetFooter(PdfDocumentControl htmlToPdfDocument) { // enable footer display htmlToPdfDocument.Footer.Enabled = checkBoxAddFooter.Checked; if (!htmlToPdfDocument.Footer.Enabled) return; // set footer height htmlToPdfDocument.Footer.Height = 50; // set footer background color htmlToPdfDocument.Footer.BackgroundColor = Color.WhiteSmoke; float pdfPageWidth = htmlToPdfDocument.PageOrientation == PdfPageOrientation.Portrait ? htmlToPdfDocument.PageSize.Width : htmlToPdfDocument.PageSize.Height; float footerWidth = pdfPageWidth - htmlToPdfDocument.Margins.Left - htmlToPdfDocument.Margins.Right; float footerHeight = htmlToPdfDocument.Footer.Height; // layout HTML in footer PdfHtml footerHtml = new PdfHtml(5, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic""> Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null); footerHtml.FitDestHeight = true; footerHtml.FontEmbedding = checkBoxFontEmbedding.Checked; htmlToPdfDocument.Footer.Layout(footerHtml); // add page numbering Font pageNumberingFont = new Font(new FontFamily("Times New Roman"), 8, GraphicsUnit.Point); //pageNumberingFont.Mea PdfText pageNumberingText = new PdfText(5, footerHeight - 12, "Page {CrtPage} of {PageCount}", pageNumberingFont); pageNumberingText.HorizontalAlign = PdfTextHAlign.Center; pageNumberingText.EmbedSystemFont = true; pageNumberingText.ForeColor = Color.DarkGreen; htmlToPdfDocument.Footer.Layout(pageNumberingText); string footerImageFile = Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo.png"; PdfImage logoFooterImage = new PdfImage(footerWidth - 40 - 5, 5, 40, Image.FromFile(footerImageFile)); htmlToPdfDocument.Footer.Layout(logoFooterImage); // create a border for footer PdfRectangle borderRectangle = new PdfRectangle(1, 1, footerWidth - 2, footerHeight - 2); borderRectangle.LineStyle.LineWidth = 0.5f; borderRectangle.ForeColor = Color.DarkGreen; htmlToPdfDocument.Footer.Layout(borderRectangle); }
By default the header and footer is the same in all the PDF pages of a document but it is possible to override the header and footer in some or all the PDF pages with another header or footer.
The HiQPdf HTML to PDF Converter offers a great flexibility in setting the PDF document headers and footers. Per PDF page customization of header and footer can be done in HtmlToPdfPageCreatingEvent event handler. Basically you can add anything in the header and footer from plain text to full HTML documents, override the default document header and footer in any page with a customized header and footer, hide the header and footer in any PDF page.
In the code sample below, the default header on the second page will be replaced with a bigger header containing the rendering of a whole website like www.google.com. There also options to hide the header or footer on the first or on the second page of the generated PDF document.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using HiQPdf; namespace HiQPdf_Demo { public partial class PdfHeadersAndFooters : UserControl { public PdfHeadersAndFooters() { InitializeComponent(); } private void buttonCreatePdf_Click(object sender, EventArgs e) { // create the HTML to PDF converter HtmlToPdf htmlToPdfConverter = new HtmlToPdf(); // set the default header and footer of the document SetHeader(htmlToPdfConverter.Document); SetFooter(htmlToPdfConverter.Document); // set a handler for PageCreatingEvent where to configure the PDF document pages htmlToPdfConverter.PageCreatingEvent += new PdfPageCreatingDelegate(htmlToPdfConverter_PageCreatingEvent); Cursor = Cursors.WaitCursor; // convert HTML to PDF string pdfFile = Application.StartupPath + @"\DemoOut\PdfHeadersAndFooters.pdf"; try { // convert URL string url = textBoxUrl.Text; // ConvertUrlToFile() is called to convert the html document and save the resulted PDF into a file on disk // Alternatively, ConvertUrlToMemory() can be called to save the resulted PDF in a buffer in memory htmlToPdfConverter.ConvertUrlToFile(url, pdfFile); } catch (Exception ex) { MessageBox.Show(String.Format("Conversion failed. {0}", ex.Message)); return; } finally { Cursor = Cursors.Arrow; // dettach from PageCreatingEvent event htmlToPdfConverter.PageCreatingEvent -= new PdfPageCreatingDelegate(htmlToPdfConverter_PageCreatingEvent); } // open the PDF document try { System.Diagnostics.Process.Start(pdfFile); } catch (Exception ex) { MessageBox.Show(String.Format("Conversion succeeded but cannot open '{0}'. {1}", pdfFile, ex.Message)); } } void htmlToPdfConverter_PageCreatingEvent(PdfPageCreatingParams eventParams) { PdfPage pdfPage = eventParams.PdfPage; int pdfPageNumber = eventParams.PdfPageNumber; if (pdfPageNumber == 1) { // set the header and footer visibility in first page pdfPage.DisplayHeader = checkBoxDisplayHeaderInFirstPage.Checked; pdfPage.DisplayFooter = checkBoxDisplayFooterInFirstPage.Checked; } else if (pdfPageNumber == 2) { // set the header and footer visibility in second page pdfPage.DisplayHeader = checkBoxDisplayHeaderInSecondPage.Checked; pdfPage.DisplayFooter = checkBoxDisplayFooterInSecondPage.Checked; if (pdfPage.DisplayHeader && checkBoxCustomizedHeaderInSecondPage.Checked) { // override the default document header in this page // with a customized header of 200 points in height pdfPage.CreateHeaderCanvas(200); // layout a HTML document in header PdfHtml htmlInHeader = new PdfHtml("http://www.google.com"); htmlInHeader.FitDestHeight = true; pdfPage.Header.Layout(htmlInHeader); // create a border for the customized header PdfRectangle borderRectangle = new PdfRectangle(0, 0, pdfPage.Header.Width - 1, pdfPage.Header.Height - 1); borderRectangle.LineStyle.LineWidth = 0.5f; borderRectangle.ForeColor = Color.Navy; pdfPage.Header.Layout(borderRectangle); } } } private void SetHeader(PdfDocumentControl htmlToPdfDocument) { // enable header display htmlToPdfDocument.Header.Enabled = true; // set header height htmlToPdfDocument.Header.Height = 50; float pdfPageWidth = htmlToPdfDocument.PageOrientation == PdfPageOrientation.Portrait ? htmlToPdfDocument.PageSize.Width : htmlToPdfDocument.PageSize.Height; float headerWidth = pdfPageWidth - htmlToPdfDocument.Margins.Left - htmlToPdfDocument.Margins.Right; float headerHeight = htmlToPdfDocument.Header.Height; // set header background color htmlToPdfDocument.Header.BackgroundColor = Color.WhiteSmoke; string headerImageFile = Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo.png"; PdfImage logoHeaderImage = new PdfImage(5, 5, 40, Image.FromFile(headerImageFile)); htmlToPdfDocument.Header.Layout(logoHeaderImage); // layout HTML in header PdfHtml headerHtml = new PdfHtml(50, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic""> Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null); headerHtml.FitDestHeight = true; htmlToPdfDocument.Header.Layout(headerHtml); // create a border for header PdfRectangle borderRectangle = new PdfRectangle(1, 1, headerWidth - 2, headerHeight - 2); borderRectangle.LineStyle.LineWidth = 0.5f; borderRectangle.ForeColor = Color.Navy; htmlToPdfDocument.Header.Layout(borderRectangle); } private void SetFooter(PdfDocumentControl htmlToPdfDocument) { // enable footer display htmlToPdfDocument.Footer.Enabled = true; // set footer height htmlToPdfDocument.Footer.Height = 50; // set footer background color htmlToPdfDocument.Footer.BackgroundColor = Color.WhiteSmoke; float pdfPageWidth = htmlToPdfDocument.PageOrientation == PdfPageOrientation.Portrait ? htmlToPdfDocument.PageSize.Width : htmlToPdfDocument.PageSize.Height; float footerWidth = pdfPageWidth - htmlToPdfDocument.Margins.Left - htmlToPdfDocument.Margins.Right; float footerHeight = htmlToPdfDocument.Footer.Height; // layout HTML in footer PdfHtml footerHtml = new PdfHtml(5, 5, @"<span style=""color:Navy; font-family:Times New Roman; font-style:italic""> Quickly Create High Quality PDFs with </span><a href=""http://www.hiqpdf.com"">HiQPdf</a>", null); footerHtml.FitDestHeight = true; htmlToPdfDocument.Footer.Layout(footerHtml); if (checkBoxDisplayPageNumbersInFooter.Checked) { // add page numbering Font pageNumberingFont = new Font(new FontFamily("Times New Roman"), 8, GraphicsUnit.Point); PdfText pageNumberingText = new PdfText(5, footerHeight - 12, "Page {CrtPage} of {PageCount}", pageNumberingFont); pageNumberingText.HorizontalAlign = PdfTextHAlign.Center; pageNumberingText.EmbedSystemFont = true; pageNumberingText.ForeColor = Color.DarkGreen; htmlToPdfDocument.Footer.Layout(pageNumberingText); } string footerImageFile = Application.StartupPath + @"\DemoFiles\Images\HiQPdfLogo.png"; PdfImage logoFooterImage = new PdfImage(footerWidth - 40 - 5, 5, 40, Image.FromFile(footerImageFile)); htmlToPdfDocument.Footer.Layout(logoFooterImage); // create a border for footer PdfRectangle borderRectangle = new PdfRectangle(1, 1, footerWidth - 2, footerHeight - 2); borderRectangle.LineStyle.LineWidth = 0.5f; borderRectangle.ForeColor = Color.DarkGreen; htmlToPdfDocument.Footer.Layout(borderRectangle); } private void linkLabelOpen_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { // open the HTML document try { System.Diagnostics.Process.Start(textBoxUrl.Text); } catch (Exception ex) { MessageBox.Show(String.Format("Cannot open '{0}'. {1}", textBoxUrl.Text, ex.Message)); } } private void PdfHeadersAndFooters_Load(object sender, EventArgs e) { textBoxUrl.Text = "http://www.hiqpdf.com/html/html5_introduction.html"; } } }