hi am trying to export datagrid to pdf file by using this piece of code:
string attachment = "attachment; filename=Contacts.pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
the file save at disk very fine but when i open the file it give me the error as the file cannot be opened either it is damaged or some file sent via email..
plz advise
thanks in advance
Loading

Suthish NairRank #92 · 22.8K reputationPosted Dec 7, 2010, 5:54 AM EST
you need to use third party tools to export to pdf.
refer article: http://www.c-sharpcorner.com/UploadFile/jayendra/4498/
faraz akram83 reputationPosted Dec 7, 2010, 7:55 PM EST
thanks in advance
regards
fazy
Suthish NairRank #92 · 22.8K reputationPosted Dec 7, 2010, 8:27 PM EST
faraz akram83 reputationPosted Dec 7, 2010, 8:54 PM EST
Elijah Roriguez2 reputationPosted May 26, 2011, 2:40 AM EDT
first download itextsharp.dill ok then put itextsharp.dll file into BIN folder
download link=sourceforge.net/projects/itextsharp/files/lates
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using System.Text;
using System.Net;
using System.IO;
using System.Reflection;
protected void btnpddf_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView gv1 = GetGridView();
// gv1.RenderControl(htmlWrite);
gv1.DataBind();
gv1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}