using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.IO; using System.Text.RegularExpressions; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; protected void ConvertToPDFNow() { StringWriter sw = new StringWriter(); HtmlTextWriter w = new HtmlTextWriter(sw); print.RenderControl(w); string htmWrite = sw.GetStringBuilder().ToString(); Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); htmWrite = Regex.Replace(htmWrite, "</?(a|A).*?>", ""); htmWrite = htmWrite.Replace("\r\n", ""); StringReader reader = new StringReader(htmWrite); Document doc = new Document(PageSize.A4); HTMLWorker parser = new HTMLWorker(doc); PdfWriter.GetInstance(doc, Response.OutputStream); doc.Open(); try { parser.Parse(reader); } catch (Exception ex) { } finally { doc.Close(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)