【问题标题】:How to preserve CSS using iTextSharp?如何使用 iTextSharp 保存 CSS?
【发布时间】:2014-02-20 06:23:53
【问题描述】:

我的首页顶部有一个这样的标题:

我使用 iTextSharp 代码来生成它的 PDF ...但是生成的 PDF 不包含这个带有黑色的标题:而是用一些 CSS 代替标题,如下所示:

我该如何解决这个问题??

代码:

 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();
    }

【问题讨论】:

标签: c# html asp.net css itextsharp


【解决方案1】:

您是否尝试过使用内联 CSS?因为 iTextSharp 中的 HTML/CSS 解析器不完整。因此,它可能无法按您的意愿工作。

还可以查看 iTextSharp 中 StyleSheet 的 LoadTagStyle 属性,看看是否有帮助。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多