Convert from HTML to PDF in CSharp VB and ASP.NET with a Free 3rd Party Library

Converting from html to pdf is not an easy task. There are some tools that can do that, but most of them cost thousands of dollars. SelectPdf offers a Community Edition (FREE) of the powerful Html To Pdf Converter for .NET that can be found in the full featured pdf library.

 
 
 
 
 
3.6 Star
(59)
53,147 times
Add to favorites
5/10/2019
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
VB.NET
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 = Server.MapPath("~/files/header.html");
            string footerUrl = Server.MapPath("~/files/footer.html");

            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();
        }
    }
}