iText7 - Displayed text in table issue

Obiwan Kenobi

I'm using iText7 library for .Net and I have a very strange issue for the displayed text. Explanations:

Context - In my business process, there are these steps:

  1. Load a PDF file from a RadDiagram (Télérik) with only one page, and save it as Byte() in my database. This operation can be done many times.
  2. Get all PDF files from database.
  3. For each PDF File, insert a table.
  4. Concatenate all files inside one global file.

There is a method that I'm using in steps 1 & 3. This method will create and insert a table inside the PDF document:

   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

And I'm using it like this:

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

Before the save in database, and after convert Byte() from the database in a new PDF file, the result is: here

Expected result - here

Issue - My problem is when I'm calling this method in the step 3. I'm calling it for data reasons (set the values). The table is at the good place, with the good background color and size, but there is a big problem with the text: here

Moreover, this same method is called for another page (wich is not loaded from the database) in the step 3, and I have the expected result.

To by-passed this problem, I tried to write all texts with Paragraph object, and not Table and Cell objects like actually. And I have the same problem (here => I should to have "CA Name")

I think that there is a problem with the text encoding from database or a problem with the superposition (table and PDF file from Byte()) The fonts are registered with the same manner.

I hope anybody can help me.

Obiwan Kenobi

I found a solution to by-pass the problem (but if someone has a better solution, I appreciate) :

Problem : like said @mkl, and I confirmed this with some tests, characters which do not exist before the serialization, their will not be visible after conversion from database if I'm trying to write them.

By-passing :

  1. I created a string containing a lot of each usable character (min & maj letters, numbers and special characters in my case).
  2. I created a Paragraph object with these values : Black for FontColor, 0.001 for FontSize, 0 for X position, 0 for Y position, 200 for Width.
  3. I added this paragraph in each PDF page.

... And the bug is fixed.

I repeat, it's not the solution, and I can't explain why there is this bug, but it's a workable way.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related