When you are working with complex and long tables, it can be useful to know if a table is broken up across multiple pages. These code snippets will help you with various table related tasks.
Consider a table with a large number of rows that has been inserted into a TextControl.
Although TextControl provides sophisticated features such as table header repetition, sometimes you may want to add specific text to rows at the beginning of a new page. In order to do this, you need to know whether or not a table is broken across multiple pages.
Table Broken?
The following method compares the start index and end index of the table with the index positions of the pages.
// Check if the table is across pages | |
// If the first cell of the table is on a different page than the last cell of the table, | |
// the table is across pages | |
private bool IsTableAcrossPages(Table table) { | |
int tableStart = table.Cells.GetItem(1, 1).Start; | |
int tableEnd = table.Cells.GetItem(table.Rows.Count, table.Columns.Count).Start; | |
int tableStartPage = textControl1.GetPages().GetItem(tableStart).Number; | |
int tableEndPage = textControl1.GetPages().GetItem(tableEnd).Number; | |
return tableStartPage != tableEndPage; | |
} |
When called, it returns true if the table has broken access pages. Otherwise, it returns false.
Table table = textControl1.Tables.GetItem(); | |
if (IsTableAcrossPages(table)) { | |
MessageBox.Show("Table is across pages"); | |
} | |
else { | |
MessageBox.Show("Table is not across pages"); | |
} |
Get All First Rows
The next method returns the first row after a given row on the next page. This is also done by comparing the line start values with the page indexes.
// Find the first table row on the next page | |
private TableRow FindFirstTableRowOnNextPage(Table table, TableRow startTableRow = null) { | |
if (startTableRow == null) { | |
startTableRow = table.Rows.GetItem(1); | |
} | |
int tableStart = table.Cells.GetItem(startTableRow.Row, 1).Start; | |
int tableStartPage = textControl1.GetPages().GetItem(tableStart).Number; | |
TableRow row = null; | |
// If the table is not across pages, | |
for (int i = startTableRow.Row + 1; i <= table.Rows.Count; i++) { | |
row = table.Rows.GetItem(i); | |
int rowStart = table.Cells.GetItem(row.Row, 1).Start; | |
// return the first row on the next page | |
if (textControl1.GetPages().GetItem(rowStart).Number > tableStartPage) { | |
break; | |
} | |
} | |
return row; | |
} |
The following code uses the method FindFirstTableRowOnNextPage to loop through all of the first rows on each page in order to highlight them.
Table table = textControl1.Tables.GetItem(); | |
TableRow nextRow = null; | |
TableRow row = null; | |
do { // find the first row on the next page | |
row = FindFirstTableRowOnNextPage(table, nextRow); | |
// highlight the row | |
if (row != null) { | |
row.CellFormat.BackColor = Color.Yellow; | |
} | |
nextRow = row; | |
} while (nextRow != null); // repeat until no more rows on the next page |
In the following screenshot, you can see the highlighted rows on each new page. This is where the table breaks across pages.