Saturday, July 20, 2013

Print out in PDF file

Referred URL - http://www.dotnetspider.com/forum/298029-Print-out-PDF-file.aspx

Download the latest version of iTextSharp.dll (v5.0.5.0) and use the following code:

using System.IO;
        using iTextSharp.text;
        using iTextSharp.text.pdf;
        using iTextSharp.text.html;
        using iTextSharp.text.html.simpleparser;
        using System.Text;


        private void button1_Click(object sender, EventArgs e)
        {
            string HtmlText = "<b>" + TextBox1.Text + "</b>";
            HTMLToPdf(HtmlText, "C://PdfFile.pdf");
        }

        public void HTMLToPdf(string HTMLContent, string FilePath)
        {
            string FileName = Guid.NewGuid().ToString();
            Document document = new Document();
            PdfWriter.GetInstance(document, new FileStream(FilePath, FileMode.Create));
            document.Open();
            iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
            iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
            hw.Style = styles;
            hw.Parse(new StringReader(HTMLContent));
            document.Close();
        }

No comments: