c# - 如何使用 iTextSharp 保留 CSS?

标签 c# html asp.net css itext

我的首页顶部有一个标题,如下所示:enter image description here

我使用 iTextSharp 代码生成它的 PDF ...但是生成的 PDF 不包含这个带有黑色的标题:相反,一些 CSS 被写在标题的位置,如下所示:

enter image description here

我怎样才能解决这个问题??

代码:

 protected void BtnPDF_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        this.Page.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();
    }

最佳答案

您尝试过使用内联 css 吗?因为 iTextSharp 中的 HTML/CSS 解析器不完整。所以它可能无法如你所愿地工作。

同时检查 iTextSharp 中 StyleSheet 的 LoadTagStyle 属性,看看是否有帮助。

有关它的更多详细信息,请参阅此答案:https://stackoverflow.com/a/9616429/604232

关于c# - 如何使用 iTextSharp 保留 CSS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21899853/

相关文章:

c# - 禁用 Graphics.FillRectangle 的抗锯齿功能?

html - 弄清楚过渡

html - 按钮的最小 Bootstrap css

javascript - 使用 bootstrap-confirmation 进入更新面板

asp.net - 如何让 Fiddler 捕获由我的 MVC 应用程序向我的 ASP.NET Web API 发出的 HTTP 请求?

javascript - 是否可以使用 C# 添加 Object 的属性

c# - 从客户端下载 Silverlight 文件

C# Selenium RC - 如何测试 jQuery 按钮点击?

javascript - 如何从 HTML 表单获取数字并将其传递给 JavaScript?

c# - 在完成之前停止中间的 for 循环