Converting html to pdf using iTextSharp
using System.IO;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
protected void btn1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4,35f,0f,0f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
StyleSheet ST = new StyleSheet();
FontFactory.Register("c:\\windows\\fonts\\Corbel.ttf");
ST.LoadTagStyle("body", "face", "Corbel");
ST.LoadTagStyle("body", "size", "12pt");
ST.LoadTagStyle(HtmlTags.P, "face", "Corbel");
ST.LoadTagStyle(HtmlTags.P, "size", "10pt");
ST.LoadTagStyle(HtmlTags.H2, "face", "Corbel");
ST.LoadTagStyle(HtmlTags.H2, "size", "12pt");
ST.LoadTagStyle(HtmlTags.H2, "align", "center");
ST.LoadTagStyle(HtmlTags.TD, "width", "10%");
htmlparser.SetStyleSheet(ST);
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}