Aspose Pdf - Rowspan and Colspan issue

Hi Team, I am using Aspose PDF version 23.5 . I am generating a table where the content is generated dynamically which has RowSpans and ColSpans.
For Eg: string[,] headerarray = new string[,]{
{colheader1, “RowSpan=1”},
{colheader2, “”},
{colheader3, “ColSpan=2”}
};
And we are assigning the colspan by reading the above array and applying to the cell. But when the pdf report is generated, the spans are not applied.

Can you guide me on the above issue.

Thanks
Swapna

@swapnavr

To address the issue of applying RowSpan and ColSpan in your dynamically generated table using Aspose PDF, you need to ensure that you are correctly setting the properties for the cells in your table. Here’s a general approach to implement RowSpan and ColSpan in your table:

  1. Create the Table: Start by creating an instance of the Table class.

  2. Define Column Widths: Set the column widths for your table using the ColumnWidths property.

  3. Add Rows and Cells: For each row, add cells and set the ColSpan and RowSpan properties as needed.

Here’s a code example that demonstrates how to implement RowSpan and ColSpan:

using Aspose.Pdf;
using Aspose.Pdf.Text;

public class TableWithSpans
{
    public static void Run()
    {
        // Create a new PDF document
        Document pdfDocument = new Document();
        Page page = pdfDocument.Pages.Add();

        // Create a table
        Table table = new Table();
        table.ColumnWidths = "100 100 100"; // Set column widths
        page.Paragraphs.Add(table);

        // Add first row
        Row row1 = table.Rows.Add();
        Cell cell1 = row1.Cells.Add("Header 1");
        cell1.ColSpan = 2; // Set ColSpan for the first cell
        Cell cell2 = row1.Cells.Add("Header 2");

        // Add second row
        Row row2 = table.Rows.Add();
        Cell cell3 = row2.Cells.Add("Row 2, Cell 1");
        Cell cell4 = row2.Cells.Add("Row 2, Cell 2");
        Cell cell5 = row2.Cells.Add("Row 2, Cell 3");

        // Add third row with RowSpan
        Row row3 = table.Rows.Add();
        Cell cell6 = row3.Cells.Add("Row 3, Cell 1");
        cell6.RowSpan = 2; // Set RowSpan for the first cell
        Cell cell7 = row3.Cells.Add("Row 3, Cell 2");

        // Add fourth row
        Row row4 = table.Rows.Add();
        Cell cell8 = row4.Cells.Add("Row 4, Cell 1");
        Cell cell9 = row4.Cells.Add("Row 4, Cell 2");

        // Save the document
        pdfDocument.Save("TableWithSpans.pdf");
    }
}

In this example:

  • The first row has a cell that spans two columns.
  • The third row has a cell that spans two rows.

Make sure that the total number of cells in each row matches the defined column widths, taking into account the spans. If the spans are not applied correctly, double-check the logic where you are assigning the spans based on your dynamic content.

If you continue to experience issues, please provide more details about how you are generating the table and applying the spans, so I can assist you further.

Sources:
[1]: TableInHeaderFooterSection.cs
[2]: RenderTable.cs

But we are adding the code dynamically for row and column spans… where the spans are not applied.

@swapnavr

Can you please provide a minimal code snippet for our reference that we can try and replicate the issue in our environment? We will test the scenario and address it accordingly. Please also try using 24.10 version of the API before sharing the requested information.