Export DataTable to PDF
Step
1 : Import the namespaces
using
iTextSharp.text;
using
iTextSharp.text.pdf;
using
iTextSharp.text.html;
using
iTextSharp.text.html.simpleparser;
Step
2 : Add the Export Method
public
void
ExportToPdf(DataTable
dt)
{
Document
document = new
Document();
//string
Filename = GetUniqueName(4);
PdfWriter
writer = PdfWriter.GetInstance(document,
new
FileStream(Server.MapPath("PDF/testpdf.pdf"),
FileMode.Create));
document.Open();
//iTextSharp.text.Image
img = iTextSharp.text.Image.GetInstance("c://ggi logo.bmp");
//document.Add(img);
iTextSharp.text.Font
font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA,
5);
//float[]
columnDefinitionSize = { 22F, 22F, 12F, 7.75F, 7.77F, 7.77F, 7.77F,
7.77F, 10.88F, 10.88F, 10.88F, 4.75F, 7.77F, 7.77F, 7.77F, 7.77F,
7.77F, 7.77F, 9F };
PdfPTable
table = new
PdfPTable(dt.Columns.Count);
PdfPRow
row = null;
float[]
widths = new
float[]
{ 4f, 4f, 4f, 4f };
//table.SetWidths(widths);
table.WidthPercentage
= 100;
int
iCol = 0;
string
colname = "";
PdfPCell
cell = new
PdfPCell(new
Phrase("Products"));
////table.AddCell(cell);
cell.Colspan
= dt.Columns.Count;
//cell.Border
= 0;
//cell.HorizontalAlignment
= 1;
foreach
(DataColumn
c in
dt.Columns)
{
table.AddCell(new
Phrase(c.ColumnName,
font5));
}
foreach
(DataRow
r in
dt.Rows)
{
for(int
i=0;i<dt.Columns.Count;i++)
{
table.AddCell(new
Phrase(r[i].ToString(),
font5));
}
}
document.Add(table);
document.Close();
}
Step 3 :
Invoke the Method
Comments
Post a Comment