How to define the cell splitting behavior

Good morning,

I’m trying to define the cell splitting behavior in such a way that, when a row contains one or more cells that are too tall to fit entirely on the current page, the row doesn’t get moved entirely to the next page.

I know that cell splitting is possible, as it occurs when the row height exceeds the page height. So, is there a way to prevent the resulting white space and allow partial row rendering across pages?

I’ve attached a code sample that can be run in LINQPad.

Thanks in advance!

class Program
{
	static void Main()
	{
		Document pdfDocument = new Document();
		Page page = pdfDocument.Pages.Add();
		Table table = new Table
		{
			ColumnWidths = "100 100 100",
			DefaultCellBorder = new BorderInfo(BorderSide.All, 0.5f),
			DefaultCellTextState = new TextState { FontSize = 12 },
			RepeatingRowsCount = 1,
		};
		Row headerRow = table.Rows.Add();
		headerRow.Cells.Add("Name");
		headerRow.Cells.Add("Age");
		headerRow.Cells.Add("Country");
		var longDesc = "antami, o Diva, del pelide Achille l'ira funesta che infiniti addusse lutti agli Achei, molte anzi tempo all'Orco generose travolse alme d'eroi, e di cani e d'augelli orrido pasto lor salme abbandonò (così di Giove l'alto consiglio s'adempìa), da quando primamente disgiunse aspra contesa il re de' prodi Atride e il divo Achille. E qual de numi inimicolli? Il figlio di Latona e di Giove. Irato al Sire destò quel Dio nel campo un feral morbo, e la gente perìa: colpa d'Atride, che fece a Crise sacerdote, oltraggio.";
		string[,] data = {
			{ "Alice", "21", "USA" },
			{ "Bob", "321", "UK" },
			{ "Charlie", longDesc, "Canada" },
			{ "Diana", longDesc, "Italy" }
		};
		for (int i = 0; i < data.GetLength(0); i++)
		{
			Row row = table.Rows.Add();
			for (int j = 0; j < data.GetLength(1); j++)
			{
				var stuff = new TextFragment(data[i, j]);
				var c = row.Cells.Add();

				c.Paragraphs.Add(stuff);
			}
		}

		page.Paragraphs.Add(table);

		pdfDocument.Save("C:\\TestPDF.pdf");
	}
}

//in the generated pdf there is a white space at the end of the first page, that is avoidable if the cell split

@Red192

Good morning!

To control the cell splitting behavior in your PDF table, you can set the IsSplit property of the Row to true. This allows the content of the cells to be split across pages if they exceed the page height, thus preventing unnecessary white space at the end of the page.

Here’s how you can modify your code to enable cell splitting:

class Program
{
    static void Main()
    {
        Document pdfDocument = new Document();
        Page page = pdfDocument.Pages.Add();
        Table table = new Table
        {
            ColumnWidths = "100 100 100",
            DefaultCellBorder = new BorderInfo(BorderSide.All, 0.5f),
            DefaultCellTextState = new TextState { FontSize = 12 },
            RepeatingRowsCount = 1,
        };
        
        Row headerRow = table.Rows.Add();
        headerRow.Cells.Add("Name");
        headerRow.Cells.Add("Age");
        headerRow.Cells.Add("Country");

        var longDesc = "antami, o Diva, del pelide Achille l'ira funesta che infiniti addusse lutti agli Achei, molte anzi tempo all'Orco generose travolse alme d'eroi, e di cani e d'augelli orrido pasto lor salme abbandonò (così di Giove l'alto consiglio s'adempìa), da quando primamente disgiunse aspra contesa il re de' prodi Atride e il divo Achille. E qual de numi inimicolli? Il figlio di Latona e di Giove. Irato al Sire destò quel Dio nel campo un feral morbo, e la gente perìa: colpa d'Atride, che fece a Crise sacerdote, oltraggio.";
        
        string[,] data = {
            { "Alice", "21", "USA" },
            { "Bob", "321", "UK" },
            { "Charlie", longDesc, "Canada" },
            { "Diana", longDesc, "Italy" }
        };
        
        for (int i = 0; i < data.GetLength(0); i++)
        {
            Row row = table.Rows.Add();
            row.IsSplit = true; // Enable cell splitting for the row
            for (int j = 0; j < data.GetLength(1); j++)
            {
                var textFragment = new TextFragment(data[i, j]);
                var cell = row.Cells.Add();
                cell.Paragraphs.Add(textFragment);
            }
        }

        page.Paragraphs.Add(table);
        pdfDocument.Save("C:\\TestPDF.pdf");
    }
}

By setting row.IsSplit = true;, the cells in that row can be split across pages, which should help eliminate the white space issue you are encountering.

If you have any further questions or need additional assistance, feel free to ask!

Doesn’t work, it doesn’t even compile.

But what i was looking for is: IsRowBroken

@Red192

Looks like you want to break the row when its cell data exceeds the page boundaries. In that case, you can keep checking the table height using Table.GetHeight() method and once it reaches particular value, you can break the row by specifying IsRowBroken property. Please also confirms if our understandings are correct and our suggestions helped you. We will further proceed accordingly.

No, it’s enough to set IsRowBroken.

I’m just warning you that the AI suggestion is not right, even if it suggested me to look into the properties of the row, where i found out that there is another IsRowBroken, like there is in the table properties, but the former actually works

@Red192

Thanks for highlighting the point. We are constantly working on improving auto-generated responses. However, it is good to know that you were able to sort out the issue. Please feel free to create a new topic in case you need any kind of assistance.