iText7 - 在 table 问题中显示文本

iText7 - Displayed text in table issue

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

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

  1. 从只有一页的 RadDiagram (Télérik) 加载 PDF 文件,并将其另存为 Byte() 在我的数据库中。这个操作可以多次进行。
  2. 从数据库中获取所有 PDF 文件。
  3. 对于每个 PDF 文件,插入一个 table。
  4. 将所有文件合并到一个全局文件中。

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

   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 步中调用此方法时。我调用它是出于数据原因(设置值)。 table 的位置很好,背景颜色和大小都很好,但是文本有一个大问题:

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

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

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

我希望有人能帮助我。

我找到了 by-pass 问题的解决方案(但如果有人有更好的解决方案,我将不胜感激):

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

By-passing :

  1. 我创建了一个 string,其中包含很多可用字符(在我的例子中是最小和最大字母、数字和特殊字符)。
  2. 我用这些值创建了一个 Paragraph 对象:Black for FontColor0.001 for FontSize0 X position0 Y position200 Width.
  3. 我在每个 PDF 页面中添加了这一段。

...错误已修复。

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