Header and Footer

The PDF document Header and Footer classes are a specialization of the PDF Repeat Canvas represented by the PdfDocumentHeader and PdfDocumentFooter. classes. The document header and footer can be created using the PdfDocumentCreateHeaderCanvas(Single) and PdfDocumentCreateFooterCanvas(Single) methods

Below is an example of creating a repeat canvas taken from the In Place HTML to PDF Object demo application:

Create Repeat Canvas Sample Source Code

C#
private void SetHeader(PdfDocument document)
{
    if (m_formCollection["checkBoxAddHeader"].Count == 0)
        return;

    // create the document header
    document.CreateHeaderCanvas(50);

    // add PDF objects to the header canvas
    string headerImageFile = m_hostingEnvironment.WebRootPath + "/DemoFiles/Images/HiQPdfLogo.png";
    PdfImage logoHeaderImage = new PdfImage(5, 5, 40, headerImageFile);
    document.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 = true;
    document.Header.Layout(headerHtml);

    PdfPage startPage = document.Pages[0];
    float headerWidth = startPage.Size.Width - startPage.Margins.Left - startPage.Margins.Right;
    float headerHeight = document.Header.Height;

    // create a border for header
    PdfRectangle borderRectangle = new PdfRectangle(1, 1, headerWidth - 2, headerHeight - 2);
    borderRectangle.LineStyle.LineWidth = 0.5f;
    borderRectangle.ForeColor = PdfColor.Navy;
    document.Header.Layout(borderRectangle);
}

private void SetFooter(PdfDocument document)
{
    if (m_formCollection["checkBoxAddFooter"].Count == 0)
        return;

    //create the document footer
    document.CreateFooterCanvas(50);

    // 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 = true;
    document.Footer.Layout(footerHtml);

    PdfPage startPage = document.Pages[0];
    float footerWidth = startPage.Size.Width - startPage.Margins.Left - startPage.Margins.Right;
    float footerHeight = document.Footer.Height;

    // add page numbering
    PdfFont pageNumberingFont = new PdfFont("Times New Roman", 8, true);
    PdfText pageNumberingText = new PdfText(5, footerHeight - 12, "Page {CrtPage} of {PageCount}", pageNumberingFont);
    pageNumberingText.HorizontalAlign = PdfTextHAlign.Center;
    pageNumberingText.EmbedSystemFont = true;
    pageNumberingText.ForeColor = PdfColor.DarkGreen;
    document.Footer.Layout(pageNumberingText);

    string footerImageFile = m_hostingEnvironment.WebRootPath + "/DemoFiles/Images/HiQPdfLogo.png";
    PdfImage logoFooterImage = new PdfImage(footerWidth - 40 - 5, 5, 40, footerImageFile);
    document.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 = PdfColor.DarkGreen;
    document.Footer.Layout(borderRectangle);
}

See Also