Export HTML Table to PDF in ASP.Net with C# using iTextSharp DLL Library

Category > ASP.NET || Published on : Monday, January 22, 2018 || Views: 13031 || Export HTML Table to PDF in ASP.Net iTextSharp DLL Library Export HTML Table


Introduction

Here Pawan Kumar will explain how to Export HTML Table to PDF in ASP.Net with C# using iTextSharp DLL Library

Description

In previous post I have explained How to Access Data From Ordered Dictionary using C#, How to use Regular Expression (Regex) to accept only Alphanumeric (Alphabets and Numbers) in TextBox in ASP.Net using RegularExpression Validator, Set or Display Watermark Text for ASP.Net TextBox, Password and MultiLine TextArea using jQuery in ASP.Net with demo and Example codes, Create Login Page In ASP.NET Web Application Using ASP.Net, C# And SQL Server, Perserve/Retain state for dynamically created ASP.Net Dynamic Controls ViewState on PostBack, Allow only AlphaNumeric ( Alphabets and Numbers) characters and space only in TextBox using Javascript and jQuery(Example & Demo), and many more articles.

Now I will explain How to Export HTML Table to PDF in ASP.Net with C# using iTextSharp DLL Library

So follow the steps to learn Export HTML Table to PDF in ASP.Net with C# using iTextSharp DLL Library

In ASP.NET there are many ways to export HTML table ot PDF Document. Here we are going to use the easiest HTML table to PDF.

Note: In this article we are going to use iTextSharp DLL.

According to sourceforge.net
iText is a PDF library that allows you to CREATE, ADAPT, INSPECT and MAINTAIN documents in the Portable Document Format (PDF):
- Generate documents and reports based on data from an XML file or a database
- Create maps and books, exploiting numerous interactive features available in PDF
- Add bookmarks, page numbers, watermarks, and other features to existing PDF documents
- Split or concatenate pages from existing PDF files
- Fill out interactive forms
- Serve dynamically generated or manipulated PDF documents to a web browser

iText is used by Java, .NET, Android and GAE developers to enhance their applications with PDF functionality.
iTextSharp is the .NET port.

You can download the iTextSharp DLL from this link below:-
https://sourceforge.net/projects/itextsharp/

After downloading, include DLL file into BIN folder of the project.

Create a ASPX HTML page with TABLE and BUTTON on it. Table will contains 3 columns i.e. Name, Age & Sex and we ahve also created sme dummy row for demo purpose.

<table style="width: 500px; height: 200px; border:1px dotted">
                <tr>
                    <td>Name </td>
                    <td>Age  </td>
                    <td>Sex  </td>
                </tr>
                <tr>
                    <td>Ram </td>
                    <td>23 </td>
                    <td>Male </td>
                </tr>
                <tr>
                    <td>Shyam </td>
                    <td>34 </td>
                    <td>Male </td>
                </tr>
                <tr>
                    <td>Sita </td>
                    <td>56 </td>
                    <td>Female </td>
                </tr>
            </table>
            <asp:Button ID="btnSubmit" runat="server" Text="Create PDF" OnClick="btnSubmit_Click" />

Complete ASPX HTML Markup:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
    EnableEventValidation="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>HTML to PDF</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table style="width: 500px; height: 200px; border:1px dotted">
                <tr>
                    <td>Name </td>
                    <td>Age  </td>
                    <td>Sex  </td>
                </tr>
                <tr>
                    <td>Ram </td>
                    <td>23 </td>
                    <td>Male </td>
                </tr>
                <tr>
                    <td>Shyam </td>
                    <td>34 </td>
                    <td>Male </td>
                </tr>
                <tr>
                    <td>Sita </td>
                    <td>56 </td>
                    <td>Female </td>
                </tr>
            </table>
            <asp:Button ID="btnSubmit" runat="server" Text="Create PDF" OnClick="btnSubmit_Click" />
        </div>
    </form>
</body>
</html>

Next Add the below code in Code Behind of the webpage

Create Contants


    const string msgFormat = "table[{0}], tr[{1}], td[{2}], a: {3}, b: {4}";
    const string table_pattern = "<table.*?>(.*?)</table>";
    const string tr_pattern = "<tr.*?>(.*?)</tr>";
    const string td_pattern = "<td.*?>(.*?)</td>";
    const string a_pattern = "<a href=\"(.*?)\"></a>";
    const string b_pattern = "<b>(.*?)</b>";

Create  private static list like below:-


    private static List<string> GetContents(string input, string pattern)
    {
        MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Singleline);
        List<string> contents = new List<string>();
        foreach (Match match in matches)
            contents.Add(match.Value);
        return contents;
    }

On the Button click event handler write the following codes.

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        WebClient wc = new WebClient();
        string url = Request.Url.AbsoluteUri;
        string fileContent = wc.DownloadString(url);

        List<string> tableContents = GetContents(fileContent, table_pattern);

        string HTMLString = String.Join(" ", tableContents.ToArray());

        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(new Paragraph("Welcome! Buddy"));
        List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(HTMLString), null);
        for (int k = 0; k < htmlarraylist.Count; k++)
        {
            pdfDoc.Add((IElement)htmlarraylist[k]);
        }

        pdfDoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;" +
                                       "filename=test.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();
    }

Complete Code Behind codes are below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Adapters;
using System.Collections;
using System.Text.RegularExpressions;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Net;
using iTextSharp.text.html.simpleparser;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    const string msgFormat = "table[{0}], tr[{1}], td[{2}], a: {3}, b: {4}";
    const string table_pattern = "<table.*?>(.*?)</table>";
    const string tr_pattern = "<tr.*?>(.*?)</tr>";
    const string td_pattern = "<td.*?>(.*?)</td>";
    const string a_pattern = "<a href=\"(.*?)\"></a>";
    const string b_pattern = "<b>(.*?)</b>";

    private static List<string> GetContents(string input, string pattern)
    {
        MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.Singleline);
        List<string> contents = new List<string>();
        foreach (Match match in matches)
            contents.Add(match.Value);
        return contents;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        WebClient wc = new WebClient();
        string url = Request.Url.AbsoluteUri;
        string fileContent = wc.DownloadString(url);

        List<string> tableContents = GetContents(fileContent, table_pattern);

        string HTMLString = String.Join(" ", tableContents.ToArray());

        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        pdfDoc.Add(new Paragraph("Welcome! Buddy"));
        List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(HTMLString), null);
        for (int k = 0; k < htmlarraylist.Count; k++)
        {
            pdfDoc.Add((IElement)htmlarraylist[k]);
        }

        pdfDoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;" +
                                       "filename=test.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();
    }
}

 

Conclusion:

So, In this tutorial we have learned, Export HTML Table to PDF in ASP.Net with C# using iTextSharp DLL Library

Download Source Codes