Aspose.Words.Examples.CSharp.Programming_Documents.Working_with_Tables.KeepTablesAndRowsBreaking.KeepTableTogether C# (CSharp) Method

KeepTableTogether() public static method

public static KeepTableTogether ( string dataDir ) : void
dataDir string
return void
        public static void KeepTableTogether(string dataDir)
        {
            // ExStart:KeepTableTogether
           Document doc = new Document(dataDir + "Table.TableAcrossPage.doc");
            // Retrieve the first table in the document.
            Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

            // 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))
            {
                // Call this method if table's cell is created on the fly
                // Newly created cell does not have paragraph inside
                cell.EnsureMinimum();
                foreach (Paragraph para in cell.Paragraphs)
                    if (!(cell.ParentRow.IsLastRow && para.IsEndOfCell))
                        para.ParagraphFormat.KeepWithNext = true;
            }
            dataDir = dataDir + "Table.KeepTableTogether_out.doc";
            doc.Save(dataDir);
            // ExEnd:KeepTableTogether
            Console.WriteLine("\nTable setup successfully to stay together on the same page.\nFile saved at " + dataDir);            
        }