【问题标题】:iTextSharp HTML to PDF preserving spacesiTextSharp HTML 到 PDF 保留空格
【发布时间】:2011-11-28 18:03:57
【问题描述】:

我正在使用 FreeTextBox.dll 获取用户输入,并将该信息以 HTML 格式存储在数据库中。用户输入的示例如下:

 

133 peachtree st ne
atlanta,ga 30303
404-652-7777

Cindy Cooley
www.somecompany.com
产品管理mgr

                                                                                                                                                                                                                                                                                         =

我希望 HTMLWorker 保留用户输入的空白​​,但它会将其去除。有没有办法保留用户的空白?以下是我如何创建 PDF 文档的示例。

Public Shared Sub CreatePreviewPDF(ByVal vsHTML As String, ByVal vsFileName As String)

        Dim output As New MemoryStream()
        Dim oDocument As New Document(PageSize.LETTER)
        Dim writer As PdfWriter = PdfWriter.GetInstance(oDocument, output)
        Dim oFont As New Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL, BaseColor.BLACK)

        Using output
            Using writer
                Using oDocument
                    oDocument.Open()
                    Using sr As New StringReader(vsHTML)
                        Using worker As New html.simpleparser.HTMLWorker(oDocument)

                            worker.StartDocument()
                            worker.SetInsidePRE(True)
                            worker.Parse(sr)
                            worker.EndDocument()
                            worker.Close()
                            oDocument.Close()

                        End Using
                    End Using

                    HttpContext.Current.Response.ContentType = "application/pdf"
                    HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}.pdf", vsFileName))
                    HttpContext.Current.Response.BinaryWrite(output.ToArray())
                    HttpContext.Current.Response.End()

                End Using
            End Using
            output.Close()
        End Using


    End Sub

【问题讨论】:

  • 只是给你一些帮助 - 这可能是错误的,如果你将它重新标记到 Visual Basic,你可能会得到更多帮助。

标签: html vb.net pdf itextsharp


【解决方案1】:

iText 和 iTextSharp 中有一个小故障,但如果您不介意下载源代码并重新编译它,您可以很容易地修复它。您需要更改两个文件。我所做的任何更改都会在代码中内联注释。行号基于 5.1.2.0 代码 rev 240

第一个在iTextSharp.text.html.HtmlUtilities.cs。在第 249 行查找函数 EliminateWhiteSpace 并将其更改为:

    public static String EliminateWhiteSpace(String content) {
        // multiple spaces are reduced to one,
        // newlines are treated as spaces,
        // tabs, carriage returns are ignored.
        StringBuilder buf = new StringBuilder();
        int len = content.Length;
        char character;
        bool newline = false;
        bool space = false;//Detect whether we have written at least one space already
        for (int i = 0; i < len; i++) {
            switch (character = content[i]) {
            case ' ':
                if (!newline && !space) {//If we are not at a new line AND ALSO did not just append a space
                    buf.Append(character);
                    space = true;  //flag that we just wrote a space
                }
                break;
            case '\n':
                if (i > 0) {
                    newline = true;
                    buf.Append(' ');
                }
                break;
            case '\r':
                break;
            case '\t':
                break;
            default:
                newline = false;
                space = false;  //reset flag
                buf.Append(character);
                break;
            }
        }
        return buf.ToString();
    }

第二个变化是iTextSharp.text.xml.simpleparser.SimpleXMLParser.cs。在第 185 行的函数 Go 中,将第 248 行更改为:

if (html /*&& nowhite*/) {//removed the nowhite check from here because that should be handled by the HTML parser later, not the XML parser

【讨论】:

    【解决方案2】:

    感谢大家的帮助。通过执行以下操作,我能够找到一个小工作:

    vsHTML.Replace("  ", "&nbsp;&nbsp;").Replace(Chr(9), "&nbsp;&nbsp;&nbsp;&nbsp;").Replace(Chr(160), "&nbsp;").Replace(vbCrLf, "<br />")
    

    实际代码无法正确显示,但第一个替换是将空格替换为&amp;nbsp;,将Chr(9) 替换为5 &amp;nbsp;,将Chr(160) 替换为&amp;nbsp;

    【讨论】:

      【解决方案3】:

      我建议使用wkhtmltopdf 而不是 iText。 wkhtmltopdf 将完全按照 webkit (Google Chrome, Safari) 呈现的 html 输出,而不是 iText 的转换。它只是一个您可以调用的二进制文件。话虽如此,我可能会检查 html 以确保用户输入中有段落和/或换行符。它们可能会在转换之前被剥离。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 2013-07-11
      相关资源
      最近更新 更多