DynamicPDF table: is there a way to group rows?

276 Views Asked by At

We are writing a table out with DynamicPDF, but we want to "group" the rows so a page break does not occur in the middle of the group. The groups are anywhere from one to six lines. Has anyone found a way to do this? Here's our code:

// Create a PDF Document 
Document document = new Document();

Table2 table = new Table2(0, 0, 200, 700);

// Add columns to the table
table.Columns.Add(100);
table.Columns.Add(100);

// This loop populates the table 
for (int i = 1; i <= 400; i++)
{
   Row2 row = table.Rows.Add(20);
   row.Cells.Add("Row #" + i);
   row.Cells.Add("Item");
}

// This loop outputs the table to as many pages as is required
do
{
   Page page = new Page();
   document.Pages.Add(page);
   page.Elements.Add(table);
   table = table.GetOverflowRows();
} while (table != null);
1

There are 1 best solutions below

1
Andrew C. On

There isn't a property or method to do this directly, but you can achieve this as follows:

  1. Use the Row2.ActualRowHeight property to get the height of each row (this will be the actual height when output based on your data).
  2. Once you've found where you want the table to break (after taking your row groups into account), set the Table2.Height property to the sum of those rows.
  3. Call Table2.GetOverflowRows() to get an overflow table with the remaining rows.
  4. Note the Table2.VisibleStartRow value as the staring row for the overflow table and repeat from #1 until Table2.GetOverflowRows() returns null.

Keep in mind that if you have the Table2.RepeatRowHeaderCount property set > 0, you'll need to include those header rows in your calculation (#2 above) for each overflow table.

Full disclosure, I work for ceTe Software, the makers of DynamicPDF.