SelectPdf Free Html To Pdf Converter - Pdf Headers and Footers with Html to Pdf Converter - C# / ASP.NET Sample

This sample shows how to convert an url to pdf using SelectPdf Pdf Library for .NET and how to set html headers and footers for the generated pdf document.

This sample will also show how to add page numbers in the footer of the generated pdf document.

Url:

Header:



Height:  px
Sample header

Footer:



Height:  px

Sample footer


Sample Code C#



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SelectPdf;

namespace SelectPdf.Samples
{
    public partial class html_to_pdf_headers_and_footers : System.Web.UI.Page
    {
        protected void BtnCreatePdf_Click(object sender, EventArgs e)
        {
            // get parameters
            string headerUrl = (new Uri(Request.Url, Page.ResolveUrl("~/files/header.html")))
                                    .AbsoluteUri;
            string footerUrl = (new Uri(Request.Url, Page.ResolveUrl("~/files/footer.html")))
                                    .AbsoluteUri;

            bool showHeaderOnFirstPage = ChkHeaderFirstPage.Checked;
            bool showHeaderOnOddPages = ChkHeaderOddPages.Checked;
            bool showHeaderOnEvenPages = ChkHeaderEvenPages.Checked;

            int headerHeight = 50;
            try
            {
                headerHeight = Convert.ToInt32(TxtHeaderHeight.Text);
            }
            catch { }


            bool showFooterOnFirstPage = ChkFooterFirstPage.Checked;
            bool showFooterOnOddPages = ChkFooterOddPages.Checked;
            bool showFooterOnEvenPages = ChkFooterEvenPages.Checked;

            int footerHeight = 50;
            try
            {
                footerHeight = Convert.ToInt32(TxtFooterHeight.Text);
            }
            catch { }

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // header settings
            converter.Options.DisplayHeader = showHeaderOnFirstPage || 
                showHeaderOnOddPages || showHeaderOnEvenPages;
            converter.Header.DisplayOnFirstPage = showHeaderOnFirstPage;
            converter.Header.DisplayOnOddPages = showHeaderOnOddPages;
            converter.Header.DisplayOnEvenPages = showHeaderOnEvenPages;
            converter.Header.Height = headerHeight;

            PdfHtmlSection headerHtml = new PdfHtmlSection(headerUrl);
            headerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Header.Add(headerHtml);

            // footer settings
            converter.Options.DisplayFooter = showFooterOnFirstPage || 
                showFooterOnOddPages || showFooterOnEvenPages;
            converter.Footer.DisplayOnFirstPage = showFooterOnFirstPage;
            converter.Footer.DisplayOnOddPages = showFooterOnOddPages;
            converter.Footer.DisplayOnEvenPages = showFooterOnEvenPages;
            converter.Footer.Height = footerHeight;

            PdfHtmlSection footerHtml = new PdfHtmlSection(footerUrl);
            footerHtml.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
            converter.Footer.Add(footerHtml);

            // add page numbering element to the footer
            if (ChkPageNumbering.Checked)
            {
                // 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(TxtUrl.Text);

            // save pdf document
            doc.Save(Response, false, "Sample.pdf");

            // close pdf document
            doc.Close();
        }
    }
}