Articles in this section
Category / Section

How to avoid table splitting across the pages in Word document

7 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can avoid the table splitting across the pages in your Word document by enabling the KeepFollow API.

Steps to avoid table splitting across the pages in the Word document using C# and VB.NET

  1. Create a new C# console application project.

Create Console application in Visual Studio

 

  1. Install the Syncfusion.DocIO.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.

Graphical user interface, text, application, email

Description automatically generated

 

  1. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

 

VB.NET

Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS

 

  1. Use the following code snippet to avoid the table splitting across the pages in the Word document.

C#

//Load the template Word document.
using (WordDocument wordDocument = new WordDocument(@"Adventure.docx", FormatType.Docx))
{
     //Process the body content for each section in the Word document.
     foreach (WSection section in wordDocument.Sections)
     {
            //Access the Body of the section where all the contents of the document are set apart.
            IterateTextBody(section.Body);
            WHeadersFooters headersFooters = section.HeadersFooters;
 
            IterateTextBody(headersFooters.Header);
            IterateTextBody(headersFooters.Footer);
     }
     wordDocument.Save("Sample.docx", FormatType.Docx);
}
System.Diagnostics.Process.Start("Sample.docx");

 

VB.NET

'Load the template Word document.
Using wordDocument As WordDocument = New WordDocument("Adventure.docx", FormatType.Docx)
            'Process the body content for each section in the Word document.
          For Each section As WSection In wordDocument.Sections
             'Access the Body of the section where all the contents of the document are set apart.
                  IterateTextBody(section.Body)
                 Dim headersFooters As WHeadersFooters = section.HeadersFooters
                 'Iterate through the TextBody of the Header and Footer.
                 IterateTextBody(headersFooters.Header)
                 IterateTextBody(headersFooters.Footer)
          Next
          wordDocument.Save("Sample.docx", FormatType.Docx)
End Using
System.Diagnostics.Process.Start("Sample.docx")

 

  1. Use KeepFollow API of Essential DocIO to preserve the table in the same page.
    private static void IterateTextBody(WTextBody textBody)
    {
    // Iterate through each of the child items of  the WTextBody.
       for (int i = 0; i < textBody.ChildEntities.Count; i++)
       {
           // IEntity is the basic unit in the DocIO DOM.
           // Access the body items (should be either paragraph, table, or block content control) as IEntity.
           IEntity bodyItemEntity = textBody.ChildEntities[i];
           // A Text body has 3 types of elements - Paragraph, Table and Block Content Control.
           // Decide the element type by using EntityType.
        switch (bodyItemEntity.EntityType)
           {
            case EntityType.Paragraph:
                WParagraph paragraph = bodyItemEntity as WParagraph;
                // Check whether the paragraph is in cell.
            if (paragraph.IsInCell)
                // Whether there is a need to keep paragraph together on a page or in a column.
                paragraph.ParagraphFormat.KeepFollow = true;
             break;
             case EntityType.Table:
             // A Table is a collection of rows and cells.
             // Iterate through the table's DOM.
             IterateTable(bodyItemEntity as WTable);
             break;
             case EntityType.BlockContentControl:
             BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
             // Iterate to the body items of Block Content Control.
             IterateTextBody(blockContentControl.TextBody);
             break;
        }
    }
    }
    

 

private static void IterateTable(WTable table)
{
    // Disable the "is break across pages" property on the table.
    table.TableFormat.IsBreakAcrossPages = false;
    // Iterate the row collection in a table.
    foreach (WTableRow row in table.Rows)
    {
        // Disable the "is break across pages" property on the row.
        row.RowFormat.IsBreakAcrossPages = false;
 
        //Iterate the cell collection in a table row.
        foreach (WTableCell cell in row.Cells)
        {
             //Table cell is derived from (also a) TextBody.
             //Reuse the code that is meant for iterating TextBody.
             IterateTextBody(cell);
        }
    }
}

 

VB.NET

Private Shared Sub IterateTextBody(ByVal textBody As WTextBody)
        ' iterate through each of the child items of the WTextBody.
        Dim i As Integer = 0
        Do While (i < textBody.ChildEntities.Count)
            ' IEntity is the basic unit in the DocIO DOM.
            ' Access the body items (should be either paragraph, table or block content control) as IEntity.
            Dim bodyItemEntity As IEntity = textBody.ChildEntities(i)
            ' A Text body has 3 types of elements - Paragraph, Table and Block Content Control.
            ' Decide the element type by using EntityType
            Select Case (bodyItemEntity.EntityType)
                Case EntityType.Paragraph
                    Dim paragraph As WParagraph = CType(bodyItemEntity, WParagraph)
                    ' Check whether the paragraph is in cell.
                    If paragraph.IsInCell Then
                        paragraph.ParagraphFormat.KeepFollow = True
                    End If
 
                Case EntityType.Table
                    ' A Table is a collection of rows and cells.
                    ' Iterate through the table's DOM.
                    IterateTable(CType(bodyItemEntity, WTable))
                Case EntityType.BlockContentControl
                    Dim blockContentControl As BlockContentControl = CType(bodyItemEntity, BlockContentControl)
                    ' Iterate to the body items of the Block Content Control.
                    IterateTextBody(blockContentControl.TextBody)
            End Select
 
            i = (i + 1)
        Loop
 
End Sub

 

Private Shared Sub IterateTable(ByVal table As WTable)
        ' Disable the "is break across pages" property on the table.
        table.TableFormat.IsBreakAcrossPages = False
        ' Iterate the row collection in a table.
        For Each row As WTableRow In table.Rows
            ' Disable the "is break across pages" property on the row.
            row.RowFormat.IsBreakAcrossPages = False
            'Iterate the cell collection in a table row.
            For Each cell As WTableCell In row.Cells
                'Table cell is derived from (also a) TextBody.
                'Reuse the code that is meant for iterating TextBody.
                IterateTextBody(cell)
            Next
        Next
End Sub

 

By executing the program, you will get the output Word document as follows.

Graphical user interface, text, application, Word

Description automatically generated



A complete working example of how to avoid the table splitting across the pages in the Word document using C# can be downloaded from Avoid table splitting across the pages.zip.

Take a moment to peruse the document where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and  importantly, the PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about how to generate and register a Syncfusion license key in your application to use the components without a trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied