You will need to make use of PdfPTable class of iTextSharp as iTextSharp does not support HTML Table. I have created a small sample to explain.
HTML
<asp:Button runat="server" Text="String to PDF" OnClick="StringToPdf" />
Namespace
using System.Data;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
C#
protected void StringToPdf(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
string html = string.Empty;
html += "<p style='font-family:verdana; font-size:23px; color:#FF0000'>ASPForums.Net Sample</p>";
html += "<br />";
PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 30;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.DefaultCell.BorderWidth = 1;
foreach (DataColumn column in dt.Columns)
{
PdfPCell cell = new PdfPCell(new Phrase(column.ColumnName));
cell.BackgroundColor = Color.YELLOW;
pdfTable.AddCell(cell);
}
foreach (DataColumn column in dt.Columns)
{
foreach (DataRow row in dt.Rows)
{
pdfTable.AddCell(row[column.ColumnName].ToString());
}
}
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
StringReader sr = new StringReader(html);
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Add(pdfTable);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=HTMLExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
}
Screenshot