【问题标题】:iTextSharp - html text into celliTextSharp - 将 html 文本放入单元格
【发布时间】:2014-07-06 04:24:19
【问题描述】:

我需要创建一个报价单 PDF。 我想将带有 HTML 代码的文本写入表格单元格。

...
 Dim texto As String
 texto = Server.HtmlDecode("descripcion del producto<br />DESC")
 table.AddCell(AgregaCelda(doc, texto))
 doc.Add(table)
...
Private Shared Function AgregaCelda(ByVal doc As iTextSharp.text.Document, ByVal Texto As   String) As iTextSharp.text.Cell
    Dim cell As iTextSharp.text.Cell = New iTextSharp.text.Cell(Texto)
    cell.Border = 0.0F
    Return cell
End Function

【问题讨论】:

  • 你有什么问题? HTML 是否显示为文本而不被呈现?
  • 是的,克里斯,它正在显示类似
    ...一些文本...
    • ...类似这样的内容,我需要呈现为 HTML .
  • PDF 和 HTML 100% 不相关。但是,iTextSharp 有一个名为 XmlWorker 的帮助程序库,可以在一定程度上将一些 HTML 标记转换为 PDF 命令。
  • 感谢您的回答,我正在尝试使用 HTMLWORKER 并将文本解析为 PDF,但我不知道如何控制它出现在表格单元格中。对不起,我的英语不是很好。 Dim hw As New iTextSharp.text.html.simpleparser.HTMLWorker(doc) hw.Parse(New StringReader(texto))
  • HTMLWorker 已经过时,不再维护。它支持非常简单的 HTML,基本上是粗体、斜体和简单的字体信息。 XmlWorker 是完成所有当前工作的地方。如果你搜索这个网站,你会发现很多这两个例子。如果你只有像&lt;br /&gt; 这样简单的东西,你可以(并且应该)只使用 iTextSharp。否则,请尝试其中一个解析器,让我们知道您遇到了什么问题。

标签: .net vb.net pdf pdf-generation itextsharp


【解决方案1】:

试试这个代码:

        table.AddCell("Some rich text:");
        PdfPCell cell = new PdfPCell();

        string htmlText =
            "<a class=\"unknownlink\" href=\".ashx\" title=\"\"></a><div class=\"imageleft\"></div><br><br><br><div style=\"font-size: 16px;\"><b>PDFsharp</b></div><b>A .NET library for processing PDF</b><br><br><br><b>PDFsharp</b> is the Open Source .NET library that easily creates and processes PDF documents on the fly from any .NET language. The same drawing routines can be used to create PDF documents, draw on the screen, or send output to any printer.<br>&nbsp;•&nbsp;<a class=\"pagelink\" href=\"Overview.ashx\" title=\"Overview\">Overview</a><br>&nbsp;•&nbsp;<a class=\"pagelink\" href=\"Features.ashx\" title=\"Features\">Features</a><br>&nbsp;•&nbsp;<a class=\"pagelink\" href=\"Downloads.ashx\" title=\"Downloads\">Downloads</a><br>&nbsp;•&nbsp;<a class=\"internallink\" href=\"/wiki/PDFsharpFirstSteps.ashx\" title=\"First Steps\">First Steps</a>  <small>'</small><br>&nbsp;•&nbsp;<a class=\"internallink\" href=\"/wiki/PDFsharpArticles.ashx\" title=\"Articles\">Articles</a>  <small>'</small><br>&nbsp;•&nbsp;<a class=\"internallink\" href=\"/wiki/PDFsharpSamples.ashx\" title=\"Samples\">Samples</a> <small>'</small><br>&nbsp;•&nbsp;<a class=\"internallink\" href=\"/wiki/PDFsharpFAQ.ashx\" title=\"FAQs\">FAQs</a>  <small>'</small><br>&nbsp;•&nbsp;<a class=\"internallink\" href=\"/wiki/\" title=\"Wiki\">Wiki</a>  <small>'</small><br>";
        using (StringReader sr = new StringReader(htmlText))
        {
            List<IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, null);
            foreach (IElement e in elements)
            {
                //Add those elements to the paragraph
                cell.AddElement(e);
            }
        }

        table.AddCell(cell);

【讨论】:

    猜你喜欢
    • 2012-04-08
    • 1970-01-01
    • 2018-06-26
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 2012-05-30
    • 1970-01-01
    相关资源
    最近更新 更多