Export to PDF in ASP.Net

Posted: May 25, 2007 in ASP.Net

This code snippet is used to Export a DataTable content into PDF and send it to client through the browser. Office 2003 should be installed at the client end.

A copy of the pdf will be created at the server and the same is sent to the client along with the response stream as attachments.

I am using ITextSharp dll which is freeware and which does the conversion from Datatable to PDF format. You can go and change the pdf format in the CreatePDFForm function.

//Is needed at the top of your code for referencing the following namespaces
using iTextSharp.text.pdf;
using iTextSharp.text;

#region Export to PDF
public void CreatePDFReport(DataTable dt, string reportName)
{
try
{
CreatePdfForm(dt, reportName);
string path = System.IO.Path.GetTempPath();
System.IO.FileStream fs = new System.IO.FileStream((path + “/” + reportName), System.IO.FileMode.OpenOrCreate);
byte[] byteArray = new byte[fs.Length];
fs.Read(byteArray, 0, Convert.ToInt32(fs.Length));
Response.AddHeader(“Content-disposition”, “attachment; filename=” + fs.Name);
Response.ContentType = “application/octet-stream”;
Response.BinaryWrite(byteArray);
fs.Close();
Response.End();
}
catch (System.Threading.ThreadAbortException te)
{
Response.Write(te);
}
}
#endregion

#region PDF conversion functions
private void CreatePdfForm(DataTable dt, string headerText)
{
string pdfFileName = headerText;
Document document = new Document(PageSize.A4.Rotate(), 50, 50, 50, 50);
string path = System.IO.Path.GetTempPath();
PdfWriter.GetInstance(document, new System.IO.FileStream(path + pdfFileName, System.IO.FileMode.Create));
document.Open();
//page header
Phrase headerPhrase = new Phrase(headerText, FontFactory.GetFont(“Verdana”, 24));
HeaderFooter header = new HeaderFooter(headerPhrase, false);
header.Border = iTextSharp.text.Rectangle.NO_BORDER;
header.Alignment = Element.ALIGN_LEFT;
document.Header = header;
document.Add(headerPhrase);
//page footer
HeaderFooter footer = new HeaderFooter(new Phrase(“Page”), true);
footer.Border = iTextSharp.text.Rectangle.NO_BORDER;
document.Footer = footer;
//build the content of the document
int numColumns = dt.Columns.Count;
PdfPTable datatable = new PdfPTable(numColumns);
datatable.DefaultCell.Padding = 3;
datatable.WidthPercentage = 100; // percentage
datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
//header row
datatable.DefaultCell.BorderWidth = 1;
datatable.DefaultCell.GrayFill = 0.7f;
//get column names
foreach (DataColumn column in dt.Columns)
{
string columnName = column.ColumnName;
Phrase phrase = new Phrase(columnName, FontFactory.GetFont(“Arial”, 8, iTextSharp.text.Font.BOLD));
datatable.AddCell(phrase);
}
datatable.HeaderRows = 1;
// this is the end of the table header
//content of report
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
string columnName = column.ColumnName;
datatable.DefaultCell.GrayFill = 0.85f;
Phrase phrase = new Phrase(row[columnName].ToString(), FontFactory.GetFont(“Arial”, 8));
datatable.AddCell(phrase);
}
}
document.Add(datatable);
document.Close();
}

#endregion

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s