如何使用stringbuilder在c#中创建和下载pdf文件?

时间:2018-08-05 14:19:46

标签: c# itext stringbuilder streamreader stringwriter

我正在尝试使用String Builder转换创建和下载PDF文件

使用以下代码,但在“ htmlparser.Parse(sr);”上我收到一个错误..说为:

  

输入字符串的格式不正确。

先谢谢了。

 using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<table style='border-collapse:collapse' border='1px solid black;'>");
                    sb.Append("<tr>");
                    sb.Append("<td>Dummy Data</td>");
                    sb.Append("</tr>");
                    sb.Append("</table>");

                    StringReader sr = new StringReader(sb.ToString());
                    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
                    pdfDoc.Open();
                    htmlparser.Parse(sr);
                    pdfDoc.Close();
                    HttpContext.Current.Response.ContentType = "application/pdf";
                    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=POReport.pdf");
                    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    HttpContext.Current.Response.Write(pdfDoc);
                    HttpContext.Current.Response.End();

                }
            }

0 个答案:

没有答案