Hi Akhter,
Please refer below sample.
HTML
<asp:Button Text="Generate PDF" runat="server" ID="btnGeneratePdf" OnClick="GeneratePDF" />
Namespaces
C#
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;
using System.IO;
using iTextSharp.text.html.simpleparser;
VB.Net
Imports System.Data
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Text
Imports System.IO
Imports iTextSharp.text.html.simpleparser
Code
C#
protected void GeneratePDF(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("ID", typeof(Int32)),
new DataColumn("Name"),
new DataColumn("Country")
});
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");
StringBuilder sb = new StringBuilder();
sb.Append("<table><tr><th>Id</th><th>Name</th><th>Country</th></tr>");
for (int i = 0; i < dt.Rows.Count; i++)
{
sb.Append("<tr>");
sb.Append("<td>"+ dt.Rows[i]["ID"] + "</td><td>"+ dt.Rows[i]["Name"] + "</td><td>"+ dt.Rows[i]["Country"] + "</td>");
sb.Append("</tr>");
}
sb.Append("</table>");
StringReader sr = new StringReader(sb.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Invoice_" + "invoice" + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
VB.Net
Protected Sub GeneratePDF(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("ID", GetType(Int32)), New DataColumn("Name"), New DataColumn("Country")})
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")
Dim sb As StringBuilder = New StringBuilder()
sb.Append("<table><tr><th>Id</th><th>Name</th><th>Country</th></tr>")
For i As Integer = 0 To dt.Rows.Count - 1
sb.Append("<tr>")
sb.Append("<td>" & dt.Rows(i)("ID") & "</td><td>" + dt.Rows(i)("Name") & "</td><td>" + dt.Rows(i)("Country") & "</td>")
sb.Append("</tr>")
Next
sb.Append("</table>")
Dim sr As StringReader = New StringReader(sb.ToString())
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", "attachment;filename=Invoice_" & "invoice" & ".pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Write(pdfDoc)
Response.End()
End Sub
Screenshot