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