Can I get a code for converting datatable to pdf in Asp.net Web application. I want to have functionality to export datatable
into PDF
. I found this article but it is using gridview
for exporting
Asked
Active
Viewed 5.1k times
12

Rajaram Shelar
- 7,537
- 24
- 66
- 107
-
so what is problem with gridview. you don't need to show gridview in you page. it's just take for creating HTML tabular format for your pdf data.. – Naresh Pansuriya Dec 18 '12 at 06:50
-
I am not showing grid view on page – Rajaram Shelar Dec 18 '12 at 06:56
-
1I am also saying that your data will not display in gridview. it;s just take from generate html view for generate pdf. Either you can go with iTextSharp but you need to put many effort for doing this. – Naresh Pansuriya Dec 18 '12 at 06:58
-
Is there any way to do this without gridview? – Rajaram Shelar Dec 18 '12 at 07:01
-
yeah you can do it as below posted answer with use of iTextSharp library. Actually above suggested url also using iTextSharp library but it wrapper with some method so you can directly generate PDF by html. – Naresh Pansuriya Dec 18 '12 at 07:05
-
1Use any third part libarary [As mentioned other about ItextSharp e.t.c] – Kamran Shahid Dec 18 '12 at 07:07
2 Answers
28
Using iTextSharp,you can do it.It can be download from internet and it is free. Please, find the code below,
public void ExportToPdf(DataTable dt,string strFilePath)
{
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strFilePath, FileMode.Create));
document.Open();
iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);
PdfPTable table = new PdfPTable(dt.Columns.Count);
PdfPRow row = null;
float[] widths = new float[dt.Columns.Count];
for (int i = 0; i < dt.Columns.Count; i++)
widths[i] = 4f;
table.SetWidths(widths);
table.WidthPercentage = 100;
int iCol = 0;
string colname = "";
PdfPCell cell = new PdfPCell(new Phrase("Products"));
cell.Colspan = dt.Columns.Count;
foreach (DataColumn c in dt.Columns)
{
table.AddCell(new Phrase(c.ColumnName, font5));
}
foreach (DataRow r in dt.Rows)
{
if (dt.Rows.Count > 0)
{
for (int h = 0; h < dt.Columns.Count; h++)
{
table.AddCell(new Phrase(r[h].ToString(), font5));
}
}
}
document.Add(table);
document.Close();
}

Himanshu
- 31,810
- 31
- 111
- 133

MahaSwetha
- 1,058
- 1
- 12
- 21
-
datatable is throwing an error .is there any conflict with type datatable – Muthu Ganapathy Nathan Jul 30 '13 at 12:08
5
You can't "convert" a DataTable
to a PDF Document. But you can insert data into it as normal content.
This would have to be done through a data control, like the GridView
or ListView
; just like in a normal webpage. Which is why the article you have linked to does that. GridView
is probably the closest and easiest way to make it aesthetically appear the same as a DataTable
. As it will just be stored as a normal table in the PDF Document.
Note that the GridView
is created in memory - you don't create or need to have one in your HTML page. Try and experiment with the code to understand this better.
So I recommend following the article.

TODOName
- 138
- 4