Hey smile,
Please refer below sample.
HTML
<asp:Button Text="Export To Pdf" runat="server" OnClick="ExportToPDF" />
Namespaces
C#
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.Data
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
protected void ExportToPDF(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("CustomerId", 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");
Document doc = new Document();
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
doc.Open();
PdfPTable table = new PdfPTable(3);
table.TotalWidth = 165f;
table.LockedWidth = true;
PdfPCell cell = new PdfPCell(new Phrase("This is table 1"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
for (int i = 0; i < dt.Rows.Count; i++)
{
table.AddCell(dt.Rows[i]["CustomerId"].ToString());
table.AddCell(dt.Rows[i]["Name"].ToString());
table.AddCell(dt.Rows[i]["Country"].ToString());
}
table.WriteSelectedRows(0, -1, doc.Left, doc.Top, writer.DirectContent);
table = new PdfPTable(3);
table.TotalWidth = 165f;
table.LockedWidth = true;
cell = new PdfPCell(new Phrase("This is table 1 Copy"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
for (int j = 0; j < dt.Rows.Count; j++)
{
table.AddCell(dt.Rows[j]["CustomerId"].ToString());
table.AddCell(dt.Rows[j]["Name"].ToString());
table.AddCell(dt.Rows[j]["Country"].ToString());
}
doc.Add(table);
doc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
VB.Net
Protected Sub ExportToPDF(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("CustomerId", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
Dim doc As Document = New Document()
Using memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, memoryStream)
doc.Open()
Dim table As PdfPTable = New PdfPTable(3)
table.TotalWidth = 165.0F
table.LockedWidth = True
Dim cell As PdfPCell = New PdfPCell(New Phrase("This is table 1"))
cell.Colspan = 3
cell.HorizontalAlignment = 1
table.AddCell(cell)
For i As Integer = 0 To dt.Rows.Count - 1
table.AddCell(dt.Rows(i)("CustomerId").ToString())
table.AddCell(dt.Rows(i)("Name").ToString())
table.AddCell(dt.Rows(i)("Country").ToString())
Next
table.WriteSelectedRows(0, -1, doc.Left, doc.Top, writer.DirectContent)
table = New PdfPTable(3)
table.TotalWidth = 165.0F
table.LockedWidth = True
cell = New PdfPCell(New Phrase("This is table 1 Copy"))
cell.Colspan = 3
cell.HorizontalAlignment = 1
table.AddCell(cell)
For j As Integer = 0 To dt.Rows.Count - 1
table.AddCell(dt.Rows(j)("CustomerId").ToString())
table.AddCell(dt.Rows(j)("Name").ToString())
table.AddCell(dt.Rows(j)("Country").ToString())
Next
doc.Add(table)
doc.Close()
Dim bytes As Byte() = memoryStream.ToArray()
memoryStream.Close()
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf")
Response.ContentType = "application/pdf"
Response.Buffer = True
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(bytes)
Response.End()
Response.Close()
End Using
End Sub
Screenshot