Tuesday, October 28, 2008

Export DataTable into PDF with ASP.NET and iTextSharp.DLL

In this article I'll demonstrate how to export your DataTable to PDF file format.

Sometimes it may be the case that you need to export your search result(in tabular format) to a specified file format like .xls, .doc or even .txt. Converting your data to these file format is quite easy and ASP.NET can build it for you internally without letting you worry about any other things.

However, to export your records to .pdf(Adobe's Portable Digital Format) you need to include some third party tool. In the below case i have used " itextSharp.dll " (DLL) component. Which can be downloded from the SourceForge web-site (
http://sourceforge.net).

The following function takes a DataTable as a parameter and converts the data present in it into a PDF File Format in a same tabular view.


using iTextSharp.text;
using iTextSharp.text.pdf;

private void GeneratePDF(DataTable dataTable)
{
Document pdfDoc = new Document(PageSize.A4, 30, 30, 40, 25);
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
int cols = dataTable.Columns.Count;
int rows = dataTable.Rows.Count;
pdfDoc.Open();

iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
pdfTable.BorderWidth = 1;
pdfTable.Width = 100;
pdfTable.Padding = 1;
pdfTable.Spacing = 1;

//creating table headers
for (int i = 0; i < cols; i++)
{
Cell cellCols = new Cell();
Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
Chunk chunkCols = new Chunk(dataTable.Columns[i].ColumnName, ColFont);
cellCols.Add(chunkCols);
pdfTable.AddCell(cellCols);

}
//creating table data (actual result)
for (int k = 0; k < rows; k++)
{
for (int j = 0; j < cols; j++)
{
Cell cellRows = new Cell();
Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), RowFont);
cellRows.Add(chunkRows);
pdfTable.AddCell(cellRows);

}
}

pdfDoc.Add(pdfTable);
pdfDoc.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=Report.pdf");
Response.Clear();
Response.BinaryWrite(mStream.ToArray());
Response.End();

}
The above function is dynamic and can accept DataTable of any number of rows and columns.
Please note that "Table" class of DLL "itextSharp" namespace is conflicting with the .NET namespace "Web.UI.HtmlControls". Everything else is fine. That's why i used the complete name of this class.


14 comments:

wastrilith2k said...

Hi!

I like your code. I think it could help a great deal with something I'm working on. The only thing I ran into is that it doesn't seem to work with the current version of iTextSharp. I'm updating it as best I can and can send you what I have when I'm done if you like.

Thanks again for this!

James

Faiz said...

Hi,
My requirement is similar to this but I dont have datatable. My requirement is to display images 3 in a row. Am able to get the imagepath and total images count. But I dont have the idea of how to display images row wise in pdf.
Could you please help me regarding this.

Thanks in advance

Anonymous said...

Hi this is raghu, I'm disparate need of you help regarding...how to get the location of the user... using his IP address.(Like "LIVE TRAFFIC FEED...in you website)..... I need it for my current project...! If possible Plz can u mail the code. My email ID is nithinananda@gmail.com....!

, Thanks in advance.

Unknown said...

hi, My requirement is to export HTML string to pdf file, i have done it also but i m not able to apply the css to generated document, can you please help me...
Thnaks

Anonymous said...

Hi,

i am getting a pdf document with single line on top. pls help !! urgent

Anonymous said...

Hi, i am getting blank pdf doc..what may have gone wrong...??

Anonymous said...

the chunks wont work...

please refer to this link: http://stackoverflow.com/questions/13927675/convert-datatable-to-pdf

ram said...

The code here works like wonder. I already had generated PDF but using PDFCell(Cell here) and was showing only few lengths of cheracters in each of the columns.Now, this code is able to show multilines of text for a column. Worth it

Soumen Chatterjee said...

Hey, I saw your blog. I have a problem during creation of pdf using itextsharp. It working fine when i have only one datatable. But in my case i have six different datatable and i have to export all the records in one pdf.
Can you please help me out from this problem.

Shree said...

Nice work...
It is working good.

Thank you so much..

buyi wen said...

I find a free online pdf text extractor to extract text from pdf online.

AdeleB said...

How to extract and convert PDF to image in ASP.NET, convert PDF to jpeg using C# programming

Anonymous said...

i have multiple crystal report so when i convert these reports to pdf i want only one pdf,how to convert

Macrosoft said...

Great details about export table to pdf file. Good useful.

Convert ASP to ASP.Net