【问题标题】:iText7 - Displayed text in table issueiText7 - 表格问题中显示的文本
【发布时间】:2018-02-16 14:28:54
【问题描述】:

我正在为 .Net 使用 iText7 库,但我对显示的文本有一个非常奇怪的问题。说明:

上下文 - 在我的业务流程中,有以下步骤:

  1. 从只有一页的 RadDiagram (Télérik) 加载 PDF 文件,并将其保存为 Byte() 在我的数据库中。此操作可以多次执行。
  2. 从数据库中获取所有 PDF 文件。
  3. 为每个 PDF 文件插入一个表格。
  4. 将所有文件连接到一个全局文件中。

我在第 1 步和第 3 步中使用了一种方法。此方法将在 PDF 文档中创建并插入一个表格:

   Private Function CreateWorkOrderHeaderTable() As Table
        Dim wLabels As Single = CSng(ReportPageSize.GetWidth * 0.06)
        Dim wValues As Single = CSng(ReportPageSize.GetWidth * 0.14)
        Dim workOrderHeaderTable As Table = New Table({ReportPageSize.GetWidth})

        Dim topHeaderTable As Table = New Table({(wLabels), (wValues), (wLabels), (wValues), (wLabels), (wValues), (wLabels), (wValues), (wLabels), (wValues)})
        topHeaderTable.SetBorder(Borders.Border.NO_BORDER)

        Dim headerNames As New List(Of String)
        Dim headerValues As New List(Of String)
        Dim emptyHeaderValues As New List(Of String)(New String() {"", "", ""})
        Dim headerLabelsTable As Table
        Dim headerValuesTable As Table

        headerLabelsTable = New Table({wLabels})
        headerNames = New List(Of String)(New String() {"CA Name", "Article", "Serial #"})
        headerNames.ForEach(Sub(headerName) headerLabelsTable.AddCell(New Cell().Add(String.Format("{0} :", headerName)).SetFont(_fontArial).SetFontSize(6).SetTextAlignment(TextAlignment.RIGHT).SetBorder(Borders.Border.NO_BORDER).SetBackgroundColor(ColorConstants.LIGHT_GRAY)))
        topHeaderTable.AddCell(New Cell().SetBorder(Borders.Border.NO_BORDER).Add(headerLabelsTable.SetBorder(Borders.Border.NO_BORDER)))

        headerValuesTable = New Table({wValues})
        headerValues = If((Me._currentMeasure IsNot Nothing), New List(Of String)(New String() {Me._currentMeasure.CA_Name, Me._currentMeasure.JDS.DS, Me._currentMeasure.SN}), emptyHeaderValues)
        headerValues.ForEach(Sub(headerValue) headerValuesTable.AddCell(New Cell().Add(headerValue).SetFont(_fontArial).SetFontSize(6).SetTextAlignment(TextAlignment.LEFT).SetBorder(Borders.Border.NO_BORDER)))
        topHeaderTable.AddCell(New Cell().SetBorder(Borders.Border.NO_BORDER).Add(headerValuesTable.SetBorder(Borders.Border.NO_BORDER)))

        headerLabelsTable = New Table({wLabels})
        headerNames = New List(Of String)(New String() {"Program", "Standard", "Desc."})
        headerNames.ForEach(Sub(headerName) headerLabelsTable.AddCell(New Cell().Add(String.Format("{0} :", headerName)).SetFont(_fontArial).SetFontSize(6).SetTextAlignment(TextAlignment.RIGHT).SetBorder(Borders.Border.NO_BORDER).SetBackgroundColor(ColorConstants.LIGHT_GRAY)))
        topHeaderTable.AddCell(New Cell().SetBorder(Borders.Border.NO_BORDER).Add(headerLabelsTable.SetBorder(Borders.Border.NO_BORDER)))

        headerValuesTable = New Table({wValues})
        headerValues = If((Me._currentMeasure IsNot Nothing), New List(Of String)(New String() {Me._currentMeasure.MM_Program, Me._currentMeasure.SelectedCA.Standard, Me._currentMeasure.CA_Description}), emptyHeaderValues)
        TrunkStringItems(headerValues, MAX_CHAR_BY_WORK_ORDER_PROP)
        headerValues.ForEach(Sub(headerValue) headerValuesTable.AddCell(New Cell().Add(headerValue).SetFont(_fontArial).SetFontSize(6).SetTextAlignment(TextAlignment.LEFT).SetBorder(Borders.Border.NO_BORDER)))
        topHeaderTable.AddCell(New Cell().SetBorder(Borders.Border.NO_BORDER).Add(headerValuesTable.SetBorder(Borders.Border.NO_BORDER)))

        headerLabelsTable = New Table({wLabels})
        headerNames = New List(Of String)(New String() {"Station", "WorkShop", "Machine"})
        headerNames.ForEach(Sub(headerName) headerLabelsTable.AddCell(New Cell().Add(String.Format("{0} :", headerName)).SetFont(_fontArial).SetFontSize(6).SetTextAlignment(TextAlignment.RIGHT).SetBorder(Borders.Border.NO_BORDER).SetBackgroundColor(ColorConstants.LIGHT_GRAY)))
        topHeaderTable.AddCell(New Cell().SetBorder(Borders.Border.NO_BORDER).Add(headerLabelsTable.SetBorder(Borders.Border.NO_BORDER)))

        headerValuesTable = New Table({wValues})
        headerValues = If((Me._currentMeasure IsNot Nothing), New List(Of String)(New String() {Me._currentMeasure.Station, Me._currentMeasure.WorkShop, Me._currentMeasure.Machine}), emptyHeaderValues)
        headerValues.ForEach(Sub(headerValue) headerValuesTable.AddCell(New Cell().Add(headerValue).SetFont(_fontArial).SetFontSize(6).SetTextAlignment(TextAlignment.LEFT).SetBorder(Borders.Border.NO_BORDER)))
        topHeaderTable.AddCell(New Cell().SetBorder(Borders.Border.NO_BORDER).Add(headerValuesTable.SetBorder(Borders.Border.NO_BORDER)))

        headerLabelsTable = New Table({wLabels})
        headerNames = New List(Of String)(New String() {"User", "First Name", "Last Name"})
        headerNames.ForEach(Sub(headerName) headerLabelsTable.AddCell(New Cell().Add(String.Format("{0} :", headerName)).SetFont(_fontArial).SetFontSize(6).SetTextAlignment(TextAlignment.RIGHT).SetBorder(Borders.Border.NO_BORDER).SetBackgroundColor(ColorConstants.LIGHT_GRAY)))
        topHeaderTable.AddCell(New Cell().SetBorder(Borders.Border.NO_BORDER).Add(headerLabelsTable.SetBorder(Borders.Border.NO_BORDER)))

        headerValuesTable = New Table({wValues})
        headerValues = If((Me._currentMeasure IsNot Nothing), New List(Of String)(New String() {Me._currentMeasure.Login, Me._currentMeasure.FirstName, Me._currentMeasure.LastName}), emptyHeaderValues)
        headerValues.ForEach(Sub(headerValue) headerValuesTable.AddCell(New Cell().Add(headerValue).SetFont(_fontArial).SetFontSize(6).SetTextAlignment(TextAlignment.LEFT).SetBorder(Borders.Border.NO_BORDER)))
        topHeaderTable.AddCell(New Cell().SetBorder(Borders.Border.NO_BORDER).Add(headerValuesTable.SetBorder(Borders.Border.NO_BORDER)))

        headerLabelsTable = New Table({wLabels})
        headerNames = New List(Of String)(New String() {"MSN", "WO #", "State"})
        headerNames.ForEach(Sub(headerName) headerLabelsTable.AddCell(New Cell().Add(String.Format("{0} :", headerName)).SetFont(_fontArial).SetFontSize(6).SetTextAlignment(TextAlignment.RIGHT).SetBorder(Borders.Border.NO_BORDER).SetBackgroundColor(ColorConstants.LIGHT_GRAY)))
        topHeaderTable.AddCell(New Cell().SetBorder(Borders.Border.NO_BORDER).Add(headerLabelsTable.SetBorder(Borders.Border.NO_BORDER)))

        headerValuesTable = New Table({wValues})
        headerValues = If((Me._currentMeasure IsNot Nothing), New List(Of String)(New String() {Me._currentMeasure.MSN, Me._currentMeasure.WorkOrder, GetMeasureStatus()}), emptyHeaderValues)
        headerValues.ForEach(Sub(headerValue) headerValuesTable.AddCell(New Cell().Add(headerValue).SetFont(_fontArial).SetFontSize(6).SetTextAlignment(TextAlignment.LEFT).SetBorder(Borders.Border.NO_BORDER)))
        topHeaderTable.AddCell(New Cell().SetBorder(Borders.Border.NO_BORDER).Add(headerValuesTable.SetBorder(Borders.Border.NO_BORDER)))

        workOrderHeaderTable.AddCell(New Cell().Add(topHeaderTable).SetBorder(Borders.Border.NO_BORDER))
        workOrderHeaderTable.SetBorder(Borders.Border.NO_BORDER)

        Return workOrderHeaderTable
    End Function

我是这样使用它的:

tableHeader.AddCell(New Cell().Add(CreateWorkOrderHeaderTable()).SetBorder(Borders.Border.NO_BORDER))

在保存到数据库之前,从数据库中将Byte()转换成一个新的PDF文件后,结果是:

预期结果 -

问题 - 我的问题是当我在步骤 3 中调用此方法时。我出于数据原因调用它(设置值)。表在好地方,背景颜色和大小都很好,但是文字有一个很大的问题:

此外,在步骤 3 中为另一个页面(未从数据库加载)调用了相同的方法,我得到了预期的结果。

为了绕过这个问题,我尝试使用 Paragraph 对象编写所有文本,而不是像实际上那样使用 Table 和 Cell 对象。我也有同样的问题( => 我应该有“CA Name”)

我认为是来自数据库的文本编码有问题或叠加有问题(来自Byte()的表格和PDF文件) 字体注册方式相同。

我希望有人可以帮助我。

【问题讨论】:

  • 对我来说,这看起来像是使用了一个已经被序列化的子集字体,因此在序列化时没有使用的字形丢失了。不幸的是,您的代码不是自包含的,您也没有共享示例 PDF。因此,我无法检查一种或另一种方式。
  • @mkl :在第 1 步和第 2 步之前,我做了 RegisterAllFonts 并注册了一个新字体,它是 Arial(在我的屏幕截图中 used font),例如:PdfFontFactory.RegisterSystemDirectories() 和 @ 987654334@。在保存到数据库之前和之后使用相同的方法。 - 你需要什么代码来帮助你?
  • @mkl : 我发现了另一件事 > 在第一部分,我创建了一个用 Arial 字体编写的页脚。此页脚包含一些字符(字母和数字),我可以看到此页脚中的_不存在的字母_在页眉中不可见。我上面的例子是在 2018 年 2 月 16 日生成的,这个日期写在页脚中。在截图中,您可以看到“6”字符。今天,我们是第 19 个,'9' 字符是可见的,但不是 '6' 字符
  • “您需要哪些代码来帮助您” - 我需要可以按原样运行的代码来重现问题。你只展示了一种方法,但没有展示它是如何使用的,甚至这个方法不仅使用了它的参数,还使用了一些类成员变量。正如您的下一条评论所建议的那样,您可以验证问题是否与序列化后的字体使用有关。从您的代码中,我无法识别您的字体使用模式是否错误或 iText 是否存在问题。

标签: .net vb.net text itext7


【解决方案1】:

我找到了绕过问题的解决方案(但如果有人有更好的解决方案,我很感激):

问题:就像说的@mkl,我通过一些测试证实了这一点,序列化之前不存在的字符,从数据库转换后它们将不可见如果我想写它们。

绕过:

  1. 我创建了一个string,其中包含大量每个可用字符(在我的例子中,最小和最大字母、数字和特殊字符)。
  2. 我用这些值创建了一个Paragraph 对象:Black 表示FontColor0.001 表示FontSize0 表示@ 987654325@,Y position0Width200
  3. 我在每个 PDF 页面中都添加了这一段。

...并且该错误已修复。

我再说一遍,这不是解决方案,我无法解释为什么会出现这个错误,但这是一种可行的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 2019-03-27
    相关资源
    最近更新 更多