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 NairPosted Dec 7, 2010, 10:54 AM
you need to use third party tools to export to pdf.
refer article: http://www.c-sharpcorner.com/UploadFile/jayendra/4498/
Elijah RoriguezPosted May 26, 2011, 6:40 AM
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();
}
faraz akramPosted Dec 8, 2010, 1:54 AM
Suthish NairPosted Dec 8, 2010, 1:27 AM
faraz akramPosted Dec 8, 2010, 12:55 AM
thanks in advance
regards
fazy