Skip to content

Instantly share code, notes, and snippets.

@guntidheerajkumar
Created May 5, 2016 06:06
Show Gist options
  • Save guntidheerajkumar/00dbd18918d7c8458a8a64b94f8b1e60 to your computer and use it in GitHub Desktop.
Save guntidheerajkumar/00dbd18918d7c8458a8a64b94f8b1e60 to your computer and use it in GitHub Desktop.
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
GrdView.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(GrdView);
frm.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 30f, 30f, 20f, 20f);
pdfDoc.SetMargins(40f, 40f, 40f, 40f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment