将C#表转换为PDF文件

时间:2016-09-28 11:47:14

标签: c# html pdf

我正在尝试使用StringWriter&将我的Table(System.Web.UI.WebControls)放入pdf中。 HtmlTextWriter,但我收到一个错误的pdf文件。

这是我的代码:

        table.RenderControl(htw);
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=Reception.pdf");
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //render the htmlwriter into the response  
        HttpContext.Current.Response.Write(headerTable);
        HttpContext.Current.Response.Write(sw.ToString());
        HttpContext.Current.Response.End();
当我打开pdf文件时,我收到“加载文件pdf时出错”。

1 个答案:

答案 0 :(得分:0)

iTextSharp课程与此相关。 我把我的代码更改为:

        table.RenderControl(htw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=Reception.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();
相关问题