Page views:
39161

In this article I’m going to explain how to export HTML table to PDF in ASP.NET.

If you are not aware to include iTextSharp.dll in ASP.NET project you could read this article

How to create PDF document in ASP.NET with C# using iTextSharp

          There are many ways we can export HTML Table to PDF document. Here I’ll explain the easiest way to export HTML Table to PDF.  

WebClient class provide the feature to download the HTML string from current URL. First we need to make sure that how we can extract HTML Table from entire source code and how we can get content from that HTML table.  <table> tag contains <tr> and <td> tags but it may have style properties. So first we have to avoid these style properties and after we can get required content from table.

   Here we have to format HTML tags for avoid style properties

Sample code:

    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>";

 

          After through looping we can find <tr> and <td> elements. Then we can get content within <td></td> tags by using this method.

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;

    }

 

Designer source code:

<%@ 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">

            <tr>

                <td> Name </td>

                <td> Age  </td>

                <td> Sex  </td>

            </tr>

            <tr>

                <td> Sachin </td>

                <td> 25 </td>

                <td> Male </td>

            </tr>

            <tr>

                <td> Saran </td>

                <td> 24 </td>

                <td> Male </td>

            </tr>

            <tr>

                <td> Priya </td>

                <td> 24 </td>

                <td> Female </td>

            </tr>

        </table>

        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

    </div>

    </form>

</body>

</html>

 

C# code:

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 to dotnetfox"));

        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=sample.pdf");

        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.Write(pdfDoc);

        Response.End();       

    }   

}



View demo Download
http://www.dotnetfox.com/Document/1092/0photo1.jpg

If you enjoyed this article, get email updates (it's free).


Related Articles
  • Export GridView data to excel using ASP.NET and c# In this article I’m going to explain how to export GridView data to excel using ASP.NET and C#. Sometimes we need to export gridview data to excel. Here I’ll show you how to export GridView data to Excel by using RkLib ExportData. RKLib ExportData is a class library used to export data to excel. So here we have to create table and fetch the records to DataTable and we can export to excel.
  • How to export GridView data to PDF document in ASP.NET with C# using iTextSharpIn this article I’m going to explain how to export GridView to PDF document. Other articles related to iTextSharp. iTextSharp is a C# port of iText, and open source Java library for PDF generation and manipulation. It can be used to create PDF documents from scratch, to convert XML to PDF (using the extra XFA Worker DLL), to fill out interactive PDF forms, to stamp new content on existing PDF doc...
  • how to export data from DataTable to Excel in ASP.NET using C#. In this article I’m going to explain how to export data from DataTable to Excel in ASP.NET using C#. Sometime we need to export data from DataTable to excel. Here I’ll show you how to export data to Excel by using RkLib ExportData. RKLib ExportData is a class library used to export data to excel. So here we have to create table and fetch records to DataTable and we can export to excel.
  • Export Chart control to PDF document in ASP.NET using iTextSharp and C#In this article I’m going to explain how to export ASP.NET chart control to PDF using C#. I have already written an article about bind ASP.NET Chart control from database. You could refer this article by following this below link. Bind ASP.NET chart control from database using C#. If you are not aware to include iTextSharp.dll in ASP.NET project you could read this article How to create PDF docume...
  • Export HTML Table to PDF in ASP.NET In this article I’m going to explain how to export HTML table to PDF in ASP.NET. There are many ways we can export HTML Table to PDF document. Here I’ll explain the easiest way to export HTML Table to PDF. WebClient class provide the feature to download the HTML string from current URL. First we need to make sure that how we can extract HTML Table from entire source code and how we can get conten...

Comments
comments powered by Disqus

Thanks for downloading..!!

×
Subscribe via Email
    
message to display in fancybox