public void KeepTableTogether()
{
Document doc = new Document(MyDir + "Table.TableAcrossPage.doc");
// Retrieve the first table in the document.
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
//ExStart
//ExFor:ParagraphFormat.KeepWithNext
//ExFor:Row.IsLastRow
//ExFor:Paragraph.IsEndOfCell
//ExFor:Cell.ParentRow
//ExFor:Cell.Paragraphs
//ExId:KeepTableTogether
//ExSummary:Shows how to set a table to stay together on the same page.
// To keep a table from breaking across a page we need to enable KeepWithNext
// for every paragraph in the table except for the last paragraphs in the last
// row of the table.
foreach (Cell cell in table.GetChildNodes(NodeType.Cell, true))
foreach (Paragraph para in cell.Paragraphs)
if (!(cell.ParentRow.IsLastRow && para.IsEndOfCell))
para.ParagraphFormat.KeepWithNext = true;
//ExEnd
doc.Save(MyDir + @"\Artifacts\Table.KeepTableTogether.doc");
// Verify the correct paragraphs were set properly.
foreach (Paragraph para in table.GetChildNodes(NodeType.Paragraph, true))
if (para.IsEndOfCell && ((Cell)para.ParentNode).ParentRow.IsLastRow)
Assert.False(para.ParagraphFormat.KeepWithNext);
else
Assert.True(para.ParagraphFormat.KeepWithNext);
}