|
Answer» using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using iTextSharp.text; using iTextSharp.text.PDF; using iTextSharp.text.html; using System.IO; using System.Collections; using System.Net; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ///Code to Bind GridView1 Here } protected void btnExport_Click(object sender, EventArgs e) { HtmlForm form = new HtmlForm(); //Below is control name to export to pdf form.Controls.Add(GridView1); StringWriter sw = new StringWriter(); HtmlTextWriter hTextWriter = new HtmlTextWriter(sw); form.Controls[0].RenderControl(hTextWriter); string html = sw.ToString(); Document Doc = new Document(); //PdfWriter.GetInstance is CREATED PdfWriter.GetInstance(Doc, new FILESTREAM(Environment.GetFolderPath (Environment.SpecialFolder.Desktop)+ " getquestions.pdf", FileMode.Create)); Doc.Open(); Chunk c = new Chunk("Export GridView to PDF n",FontFactory.GetFont("Verdana", 15)); Paragraph p = new Paragraph(); p.Alignment = Element.ALIGN_CENTER; p.Add(c); Chunk chunk1 = new Chunk("By Getproductprice.com team n",FontFactory.GetFont("Verdana", 8)); Paragraph p1 = new Paragraph(); p1.Alignment = Element.ALIGN_RIGHT; p1.Add(chunk1); Doc.Add(p); Doc.Add(p1); System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(new StringReader(html)); HtmlParser.Parse(Doc, xmlReader); Doc.Close(); string PATH = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ " Pdffilepath.pdf"; ShowPdf(Path); } private void ShowPdf(string strS) { Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition","attachment;filename=" + strS); Response.TransmitFile(strS); Response.End(); //Response.WriteFile(strS); Response.Flush(); Response.Clear(); } }
|