Search content within the blog

Thursday, January 15, 2009

Download/Export data as pdf

Include the following
using iTextSharp.text;
using iTextSharp.text.pdf;

Add itextsharp.dll as a reference available to download from the foll link:
Click here

In this example, i have data from stringbuilder, instead we can have any content such as contents of gridview, datalist, customised data etc.

StringBuilder strProfile = new StringBuilder();
strProfile.Append("First Name:");

strProfile.AppendLine();
strProfile.Append("Last Name:");


Response.Clear();

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=MypdfFile.pdf");

//string html = sw.ToString();

Document document = new Document();
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
document.Add(new Paragraph(strProfile.ToString()));
document.Close();

Response.Flush();
Response.End();

No comments:

Post a Comment