I have included the code which shows an example of what I would like to do. The cell text doesn’t matter, I can easily repeat it so it is left out of the example.
The code generates 3 tables. The red table is 15 rows * 3 columns and overflows off the bottom of the slide. The two blue tables, represent what I would like to end up with, a 10 * 3 table and 5 * 3 table.
- when I get to row 10 and realise there isn’t any space left, undo the merging of 15 cells, re-merge the first 10, then remove the rows that overflow. I can then repeat the leftover rows on the next table.
I am working on a workaround so that I can attempt to calculate the row height before rendering the table. I am just hoping that I can avoid that if possible.
ISlide sld = _presentation.Slides[0];
double[] dblCols = { 50, 50, 50 }; // 3
double[] dblRows = { 50, 50, 50, 50, 50,
50, 50, 50, 50, 50,
50, 50, 50, 50, 50 }; // 15
int splitAtRowNum = 10;
int rowsRemaining = dblRows.Length - splitAtRowNum;
ITable originalTable = sld.Shapes.AddTable(50, 50, dblCols, dblRows);
PopulateData(originalTable);
originalTable.MergeCells(originalTable[0, 0], originalTable[0, dblRows.Length - 1], false);
AddBorderColors(originalTable, Color.Red);
// show example of table split into two
if (true) {
ITable tableToSplitPart1 = sld.Shapes.AddTable(300, 50, dblCols, dblRows);
PopulateData(tableToSplitPart1);
tableToSplitPart1.MergeCells(tableToSplitPart1[0, 0], tableToSplitPart1[0, splitAtRowNum - 1], false);
RemoveRowsFromTable(tableToSplitPart1, rowsRemaining, true);
AddBorderColors(tableToSplitPart1, Color.Blue);
ITable tableToSplitPart2 = sld.Shapes.AddTable(500, 50, dblCols, dblRows);
PopulateData(tableToSplitPart2);
tableToSplitPart2.MergeCells(tableToSplitPart2[0, splitAtRowNum ], tableToSplitPart2[0, splitAtRowNum + rowsRemaining - 1], false);
RemoveRowsFromTable(tableToSplitPart2, splitAtRowNum, false);
AddBorderColors(tableToSplitPart2, Color.Blue);
}
//
// helper methods
//
private void AddBorderColors(ITable table, Color color) {
foreach (IRow row in table.Rows) {
foreach (ICell cell in row) {
cell.FillFormat.FillType = FillType.Solid;
cell.FillFormat.SolidFillColor.Color = Color.White;
cell.BorderTop.FillFormat.FillType = FillType.Solid;
cell.BorderTop.FillFormat.SolidFillColor.Color = color;
cell.BorderTop.Width = 5;
cell.BorderBottom.FillFormat.FillType = FillType.Solid;
cell.BorderBottom.FillFormat.SolidFillColor.Color = color;
cell.BorderBottom.Width = 5;
cell.BorderLeft.FillFormat.FillType = FillType.Solid;
cell.BorderLeft.FillFormat.SolidFillColor.Color = color;
cell.BorderLeft.Width = 5;
cell.BorderRight.FillFormat.FillType = FillType.Solid;
cell.BorderRight.FillFormat.SolidFillColor.Color = color;
cell.BorderRight.Width = 5;
}
}
}
private void RemoveRowsFromTable(ITable table, int rowsToRemove, bool removeFromEnd) {
int initialRowCount = table.Rows.Count - 1;
for (int i = 0; i < rowsToRemove; i++) {
int removeIdx = removeFromEnd ? initialRowCount - i : 0;
table.Rows.RemoveAt(removeIdx, false);
}
}
private void PopulateData(ITable table) {
for (int r = 0; r < table.Rows.Count; r++) {
for (int c = 0; c < table.Rows[r].Count; c++) {
table[c,r].TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontHeight = 10;
table[c,r].TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.FillType = FillType.Solid;
table[c,r].TextFrame.Paragraphs[0].Portions[0].PortionFormat.FillFormat.SolidFillColor.Color = Color.Black;
table[c,r].TextFrame.Paragraphs[0].Portions[0].Text = string.Format("{0} , {1}", r, c);
}
}
}