コード例 #1
0
        private byte[] ConvertHtmlToPdf(StringBuilder sbHtmlText, string testname, string userid)
        {
            byte[] bPDF        = null;
            string returnvalue = string.Empty;

            returnvalue = Request.PhysicalApplicationPath + "\\Upload\\" + userid + testname + ".pdf";


            MemoryStream ms        = new MemoryStream();
            TextReader   txtReader = new StringReader(sbHtmlText.ToString());

            // 1: create object of a itextsharp document class
            Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

            // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
            PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

            // 3: we create a worker parse the document
            iTextSharp.text.html.simpleparser.HTMLWorker htmlWorker = new iTextSharp.text.html.simpleparser.HTMLWorker(doc);

            // 4: we open document and start the worker on the document
            doc.Open();
            htmlWorker.StartDocument();

            // 5: parse the html into the document
            htmlWorker.Parse(txtReader);

            // 6: close the document and the worker
            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();
            ms.Close();

            bPDF = ms.ToArray();

            //System.IO.File.WriteAllBytes(returnvalue, bPDF);

            return(bPDF);
        }