Pages

Men

rh

1/19/2016

How to convert HTML Table data into PDF File using ASP.NET with C#


In my case i have dynamically generated Table using C# Code. That Generated code stored in String.

 String HtmlBody = String.Empty;

Example of code in the HTMLBody :


<table><tr><td>Good morning/afternoon/evening. My name is <Name> calling from One Housing Group. May I please speak to the resident of <address>? I am calling today about your recent complaint. This survey will take no longer than a few  minutes to complete and will be used to help improve our services in the future as we want to hear your feedback and opinions. Are you free to talk? </td></tr><tr><td>1. On a scale of 1 – 10, where 1 is very dissatisfied and 10 very satisfied, what score would you give us for: </td></tr><tr><td>Complaint ID </td><td>2356 </td></tr><tr><td>How easy it was to make your complaint? </td><td>2 - Fairly satisfied </td></tr><tr><td>How long it took to resolve your complaint? </td><td>2 - Fairly satisfied </td></tr><tr><td>Keeping you informed throughout the process? </td><td>2 - Fairly satisfied </td></tr><tr><td>How supportive and helpful we were? </td><td>2 - Fairly satisfied </td></tr><tr><td>How satisfied you are with the outcome of your complaint? </td><td>2 - Fairly satisfied </td></tr><tr><td>2. Overall, how satisfied or dissatisfied are you with the service you received? </td><td>2 - Fairly satisfied </td></tr><tr><td>3. Is there anything you can think of that we could have done better or is there anything we did particularly well? </td><td>NA </td></tr><tr><td>4. Finally on a scale of 1-10, where 1 is very unlikely and 10 is very likely, how likely are you to recommend One Housing Group to your friends and family? </td><td>7 </td></tr></table>

The above HTML Table content is sored in String Text. Called HtmlBody which is declared already in above.



The following code i have used to convert HTML table into BYTES and then converted into PDF file. 

By Default i have stored in PDFfile in my Application Folder called PDFFolder. And the file name is in PDFFolder is : PDFdata.pdf



Byte[] bytes;
            using (var ms = new MemoryStream())
            {
                using (var doc = new Document())
                {
                    using (var writer = PdfWriter.GetInstance(doc, ms))
                    {
                        doc.Open();
                        var example_html = HtmlBody;
                        using (var htmlWorker = new iTextSharp.text.html.simpleparser.HTMLWorker(doc))
                        {
                            using (var sr = new StringReader(example_html))
                            {
                                htmlWorker.Parse(sr);
                            }
                        }
                        doc.Close();
                    }
                }
                bytes = ms.ToArray();
            }
            //var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
            var testFile = HttpContext.Current.Server.MapPath("PDFFolder\\" + "PDFdata.pdf");
            System.IO.File.WriteAllBytes(testFile, bytes);
            FileStream fStream = File.OpenRead(testFile);
            byte[] fileData = new byte[fStream.Length];
            fStream.Read(fileData, 0, (int)fStream.Length);
            fStream.Close();




No comments :

Post a Comment