Hi Indradeo,
Please refer below sample.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:Button Text="Export To PDF" ID="btnExport" runat="server" OnClick="Export" />
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using iTextSharp.text;
using iTextSharp.text.pdf;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Code
C#
private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void Export(object sender, EventArgs e)
{
DataTable dt = GetData("SELECT TOP 5 * FROM Customers");
Document document = new Document(PageSize.A3, 50f, 30f, 10f, 10f);
iTextSharp.text.Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
//Color color = null;
document.Open();
table = new PdfPTable(1);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
cell = ImageCell(@"~/Images/india.png", 200f, PdfPCell.ALIGN_CENTER);
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(1);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
phrase = new Phrase();
phrase.Add(new Chunk("Meja Thermal Power Project", FontFactory.GetFont("Arial", 16, Font.BOLD, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(1);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
phrase = new Phrase();
phrase.Add(new Chunk("A Joint Venture of NTPC Ltd & UPRVUNL", FontFactory.GetFont("Arial", 16, Font.BOLD, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(11);
table.TotalWidth = 700f;
table.LockedWidth = true;
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.SetWidths(new float[] { 0.7f, 1f, 1f, 1f, 1f, 0.7f, 0.7f, 0.7f, 0.7f, 1f, 1f });
table.SpacingBefore = 20f;
for (int j = 0; j < dt.Columns.Count; j++)
{
phrase = new Phrase();
phrase.Add(new Chunk(dt.Columns[j].ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
phrase = new Phrase();
phrase.Add(new Chunk(dr[0].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[1].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[2].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[3].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[4].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[5].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[6].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[7].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[8].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[9].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
phrase = new Phrase();
phrase.Add(new Chunk(dr[10].ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
table.AddCell(cell);
}
document.Add(table);
table = new PdfPTable(1);
table.TotalWidth = 500f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
phrase = new Phrase();
phrase.Add(new Chunk("\n\nThis is an electronically generated report, hence does not require a signature", FontFactory.GetFont("Arial", 6, Font.BOLD, BaseColor.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_RIGHT);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
document.Add(table);
document.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();
}
}
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
private static PdfPCell ImageCell(string path, float scale, int align)
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(path));
image.ScalePercent(scale);
PdfPCell cell = new PdfPCell(image);
cell.BorderColor = BaseColor.WHITE;
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 0f;
cell.PaddingTop = 0f;
return cell;
}
VB.Net
Private Function GetData(ByVal query As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Protected Sub Export(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetData("SELECT TOP 5 * FROM Customers")
Dim document As Document = New Document(PageSize.A3, 50.0F, 30.0F, 10.0F, 10.0F)
Dim NormalFont As iTextSharp.text.Font = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK)
Using memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream()
Dim writer As PdfWriter = PdfWriter.GetInstance(document, memoryStream)
Dim phrase As Phrase = Nothing
Dim cell As PdfPCell = Nothing
Dim table As PdfPTable = Nothing
document.Open()
table = New PdfPTable(1)
table.TotalWidth = 500.0F
table.LockedWidth = True
table.SetWidths(New Single() {1.0F})
cell = ImageCell("~/Images/india.png", 200.0F, PdfPCell.ALIGN_CENTER)
table.AddCell(cell)
document.Add(table)
table = New PdfPTable(1)
table.TotalWidth = 500.0F
table.LockedWidth = True
table.SetWidths(New Single() {1.0F})
phrase = New Phrase()
phrase.Add(New Chunk("Meja Thermal Power Project", FontFactory.GetFont("Arial", 16, Font.BOLD, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER)
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
table.AddCell(cell)
document.Add(table)
table = New PdfPTable(1)
table.TotalWidth = 500.0F
table.LockedWidth = True
table.SetWidths(New Single() {1.0F})
phrase = New Phrase()
phrase.Add(New Chunk("A Joint Venture of NTPC Ltd & UPRVUNL", FontFactory.GetFont("Arial", 16, Font.BOLD, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_CENTER)
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
table.AddCell(cell)
document.Add(table)
table = New PdfPTable(11)
table.TotalWidth = 700.0F
table.LockedWidth = True
table.HorizontalAlignment = Element.ALIGN_LEFT
table.SetWidths(New Single() {0.7F, 1.0F, 1.0F, 1.0F, 1.0F, 0.7F, 0.7F, 0.7F, 0.7F, 1.0F, 1.0F})
table.SpacingBefore = 20.0F
For j As Integer = 0 To dt.Columns.Count - 1
phrase = New Phrase()
phrase.Add(New Chunk(dt.Columns(j).ToString(), FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
Next
For i As Integer = 0 To dt.Rows.Count - 1
Dim dr As DataRow = dt.Rows(i)
phrase = New Phrase()
phrase.Add(New Chunk(dr(0).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(1).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(2).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(3).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(4).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(5).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(6).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(7).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(8).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(9).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
phrase = New Phrase()
phrase.Add(New Chunk(dr(10).ToString(), FontFactory.GetFont("Arial", 8, Font.NORMAL, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT)
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE
table.AddCell(cell)
Next
document.Add(table)
table = New PdfPTable(1)
table.TotalWidth = 500.0F
table.LockedWidth = True
table.SetWidths(New Single() {1.0F})
phrase = New Phrase()
phrase.Add(New Chunk(vbLf & vbLf & "This is an electronically generated report, hence does not require a signature", FontFactory.GetFont("Arial", 6, Font.BOLD, BaseColor.BLACK)))
cell = PhraseCell(phrase, PdfPCell.ALIGN_RIGHT)
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
table.AddCell(cell)
document.Add(table)
document.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
Private Shared Function PhraseCell(ByVal phrase As Phrase, ByVal align As Integer) As PdfPCell
Dim cell As PdfPCell = New PdfPCell(phrase)
cell.BorderColor = BaseColor.WHITE
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 2.0F
cell.PaddingTop = 0F
Return cell
End Function
Private Shared Function ImageCell(ByVal path As String, ByVal scale As Single, ByVal align As Integer) As PdfPCell
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(path))
image.ScalePercent(scale)
Dim cell As PdfPCell = New PdfPCell(image)
cell.BorderColor = BaseColor.WHITE
cell.VerticalAlignment = PdfPCell.ALIGN_TOP
cell.HorizontalAlignment = align
cell.PaddingBottom = 0F
cell.PaddingTop = 0F
Return cell
End Function
Screenshot