Table: Layout issue rowspan/colspan

Hi,

i have an issue in creating a table. This should be the layout for my table:
table.png (4,7 KB)

But this is the result:
broken_table.png (6,0 KB)

I only add the number of cells that are necessary per row. Nothing special…

Dim doc As New Aspose.Pdf.Document()
doc.PageInfo.IsLandscape = True
Dim p As Page = doc.Pages.Add()

Dim table As New Table()
table.ColumnAdjustment = ColumnAdjustment.Customized
table.ColumnWidths = "68 125 160 27 75 75 45 215"
table.DefaultCellBorder = New BorderInfo(BorderSide.All, 0.75, Color.Red)
Dim row As Row = table.Rows.Add()
Dim c As Cell = row.Cells.Add("1/1")
c.RowSpan = 3
row.Cells.Add("1/2")
c = row.Cells.Add("1/3")
c.RowSpan = 3
c = row.Cells.Add("1/4")
c.ColSpan = 5

row = table.Rows.Add()
c = row.Cells.Add("2/1")
c.RowSpan = 2
row.Cells.Add("2/2")
row.Cells.Add("2/3")
row.Cells.Add("2/4")
row.Cells.Add("2/5")
c = row.Cells.Add("2/6")
c.RowSpan = 2

row = table.Rows.Add()
c = row.Cells.Add("3/1")
c.ColSpan = 4

p.Paragraphs.Add(table)

Does anyone has an idea whats wrong in my code or is it a bug?

Kind regards,
Maik

@maik.manke
Hi, I’ve checked the issue
The issue appears to be caused by mixed RowSpan/ColSpan handling when later rows omit cells that are still occupied by previous row spans. As a workaround, define all 8 cells in each physical row and hide internal borders for the cells that should visually span. Also make sure the page/content width is large enough for the total column width.

The following code seems to address the issue

Public Sub TableRowspanColspanLayoutIssue()
    Dim originalOutputPath As String = "table_rowspan_colspan_original_out.pdf"
    Dim fixedOutputPath As String = "table_rowspan_colspan_fixed_out.pdf"

    Using originalDoc As New Document()
        originalDoc.PageInfo.IsLandscape = True

        Dim page As Page = originalDoc.Pages.Add()
        page.Paragraphs.Add(CreateCustomerSpannedTable())

        originalDoc.Save(originalOutputPath)
    End Using

    Using fixedDoc As New Document()
        fixedDoc.PageInfo.IsLandscape = True
        fixedDoc.PageInfo.Margin = New MarginInfo With {
            .Left = 10,
            .Right = 10,
            .Top = 10,
            .Bottom = 10
        }

        Dim page As Page = fixedDoc.Pages.Add()
        page.Paragraphs.Add(CreateCustomerSpannedTableWithBorderWorkaround())

        fixedDoc.Save(fixedOutputPath)
    End Using
End Sub

Private Function CreateCustomerSpannedTable() As Table
    Dim table As New Table With {
        .ColumnAdjustment = ColumnAdjustment.Customized,
        .ColumnWidths = "68 125 160 27 75 75 45 215",
        .DefaultCellBorder = New BorderInfo(BorderSide.All, 0.75F, Color.Red)
    }

    Dim row As Row = table.Rows.Add()

    Dim cell As Cell = row.Cells.Add("1/1")
    cell.RowSpan = 3

    row.Cells.Add("1/2")

    cell = row.Cells.Add("1/3")
    cell.RowSpan = 3

    cell = row.Cells.Add("1/4")
    cell.ColSpan = 5

    row = table.Rows.Add()

    cell = row.Cells.Add("2/1")
    cell.RowSpan = 2

    row.Cells.Add("2/2")
    row.Cells.Add("2/3")
    row.Cells.Add("2/4")
    row.Cells.Add("2/5")

    cell = row.Cells.Add("2/6")
    cell.RowSpan = 2

    row = table.Rows.Add()

    cell = row.Cells.Add("3/1")
    cell.ColSpan = 4

    Return table
End Function

Private Function CreateCustomerSpannedTableWithBorderWorkaround() As Table
    Dim table As New Table With {
        .ColumnAdjustment = ColumnAdjustment.Customized,
        .ColumnWidths = "68 125 160 27 75 75 45 215",
        .DefaultCellBorder = New BorderInfo(BorderSide.None)
    }

    Dim row As Row = table.Rows.Add()

    AddWorkaroundCell(row, String.Empty, BorderSide.Left Or BorderSide.Top Or BorderSide.Right)
    AddWorkaroundCell(row, "1/2", BorderSide.All)
    AddWorkaroundCell(row, String.Empty, BorderSide.Left Or BorderSide.Top Or BorderSide.Right)
    AddWorkaroundCell(row, "1/4", BorderSide.Left Or BorderSide.Top Or BorderSide.Bottom)
    AddWorkaroundCell(row, String.Empty, BorderSide.Top Or BorderSide.Bottom)
    AddWorkaroundCell(row, String.Empty, BorderSide.Top Or BorderSide.Bottom)
    AddWorkaroundCell(row, String.Empty, BorderSide.Top Or BorderSide.Bottom)
    AddWorkaroundCell(row, String.Empty, BorderSide.Top Or BorderSide.Right Or BorderSide.Bottom)

    row = table.Rows.Add()

    AddWorkaroundCell(row, "1/1", BorderSide.Left Or BorderSide.Right)
    AddWorkaroundCell(row, "2/1", BorderSide.Left Or BorderSide.Top Or BorderSide.Right)
    AddWorkaroundCell(row, "1/3", BorderSide.Left Or BorderSide.Right)
    AddWorkaroundCell(row, "2/2", BorderSide.All)
    AddWorkaroundCell(row, "2/3", BorderSide.All)
    AddWorkaroundCell(row, "2/4", BorderSide.All)
    AddWorkaroundCell(row, "2/5", BorderSide.All)
    AddWorkaroundCell(row, "2/6", BorderSide.Left Or BorderSide.Top Or BorderSide.Right)

    row = table.Rows.Add()

    AddWorkaroundCell(row, String.Empty, BorderSide.Left Or BorderSide.Right Or BorderSide.Bottom)
    AddWorkaroundCell(row, String.Empty, BorderSide.Left Or BorderSide.Right Or BorderSide.Bottom)
    AddWorkaroundCell(row, String.Empty, BorderSide.Left Or BorderSide.Right Or BorderSide.Bottom)
    AddWorkaroundCell(row, "3/1", BorderSide.Left Or BorderSide.Top Or BorderSide.Bottom)
    AddWorkaroundCell(row, String.Empty, BorderSide.Top Or BorderSide.Bottom)
    AddWorkaroundCell(row, String.Empty, BorderSide.Top Or BorderSide.Bottom)
    AddWorkaroundCell(row, String.Empty, BorderSide.Top Or BorderSide.Right Or BorderSide.Bottom)
    AddWorkaroundCell(row, String.Empty, BorderSide.Left Or BorderSide.Right Or BorderSide.Bottom)

    Return table
End Function

Private Function AddWorkaroundCell(row As Row, text As String, borderSide As BorderSide) As Cell
    Dim cell As Cell = row.Cells.Add(text)
    cell.Border = New BorderInfo(borderSide, 0.75F, Color.Red)
    Return cell
End Function

table_rowspan_colspan_fixed_out.pdf (2.6 KB)

table_rowspan_colspan_original_out.pdf (2.1 KB)

i attached the results so you can check if that’s true on your side
If there’s still some issues remaining feel free to ask

1 Like

Hi,
thank you for making this effort. Basically the structure of the table is correct, but you can see some gaps in the cell frame. As a workaround, I set column 1/3 to rowSpan = 2 and an empty cell above it.

Can I have this problem recorded as a bug ticket?

Kind regards,
Maik

@maik.manke
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-62516

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Sure, I’ve added ticket with original description.

1 Like