Hi baswal07,
Refer below sample.
HTML
<asp:Button ID="btnReport" runat="server" Text="Generate Report" OnClick="OnReportClick" />
Namespaces
C#
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using System.Data;
using System.IO;
VB.Net
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.tool.xml
Imports System.Data
Imports System.IO
Code
C#
protected void OnReportClick(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
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");
if (!Directory.Exists(Server.MapPath("~/Files")))
{
Directory.CreateDirectory(Server.MapPath("~/Files"));
}
for (int i = 0; i < dt.Rows.Count; i++)
{
string myfile = dt.Rows[i][0] + "_" + DateTime.Now.ToString("ddMMyyyyHHms") + ".pdf";
DataRow[] dr = dt.Select("Id=" + dt.Rows[i][0]);
File.WriteAllBytes(Server.MapPath("~/Files/" + myfile), ExportToPDF(myfile, dr.CopyToDataTable()));
}
}
protected byte[] ExportToPDF(string myfile, DataTable dt)
{
GridView GridView1 = new GridView();
GridView1.DataSource = dt;
GridView1.DataBind();
byte[] bytes;
using (MemoryStream memoryStream = new MemoryStream())
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
bytes = memoryStream.ToArray();
memoryStream.Close();
}
}
}
return bytes;
}
VB.Net
Protected Sub OnReportClick(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), 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")
If (Not Directory.Exists(Server.MapPath("~/Files"))) Then
Directory.CreateDirectory(Server.MapPath("~/Files"))
End If
For i As Integer = 0 To dt.Rows.Count - 1
Dim myfile As String = dt.Rows(i)(0) & "_" + DateTime.Now.ToString("ddMMyyyyHHms") & ".pdf"
Dim dr As DataRow() = dt.Select("Id=" & dt.Rows(i)(0))
File.WriteAllBytes(Server.MapPath("~/Files/" + myfile), ExportToPDF(myfile, dr.CopyToDataTable()))
Next
End Sub
Protected Function ExportToPDF(ByVal myfile As String, ByVal dt As DataTable) As Byte()
Dim GridView1 As GridView = New GridView()
GridView1.DataSource = dt
GridView1.DataBind()
Dim bytes As Byte()
Using memoryStream As MemoryStream = New MemoryStream()
Using sw As StringWriter = New StringWriter()
Using hw As HtmlTextWriter = New HtmlTextWriter(sw)
GridView1.RenderControl(hw)
Dim sr As StringReader = New StringReader(sw.ToString())
Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream)
pdfDoc.Open()
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr)
pdfDoc.Close()
bytes = memoryStream.ToArray()
memoryStream.Close()
End Using
End Using
End Using
Return bytes
End Function