Articles in this section
Category / Section

How to keep a group of rows always together in a Word document?

7 mins read

If your requirement is to keep a group of rows on the same page, and the group of rows doesn’t split across pages in the Word document, you can achieve it using Syncfusion Essential DocIO.

Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies.

Steps to keep a group of rows always together in the Word document

  1. Create a new C# .NET Core console application project. Create .NET Core console application in Visual Studio in ASP.NET Core Word
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org. Add DocIO.Net.Core NuGet packages of ASP.NET Core Word
  3. Include the following namespace in the Program.cs file:

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
  1. To keep a group of rows on the same page and the group of rows doesn’t split across pages in the Word document.
  • Create a table with specified number of rows.
  • Fetch the group of rows, for example three rows (Data1/Data2/Data2) and add that group of rows to each row in the outer table.
  • Then, disable the outer table’s border and IsBreakAcrossPages properties.

4.1 Use the following code example to keep a group of rows on the same page and the group of rows doesn’t split across pages.

C#

//Create a Word document.
using (WordDocument document = new WordDocument())
{
    //Add a section to the Word document.
    IWSection section = document.AddSection();
    //Set number of table rows.
    int rowCount = 200;
    //Set number of row set.
    int rowSet = 3;
    //Create a table with specified number of rows.
    IWTable innerTable = CreateTable(rowCount, rowSet, document);
    //Create the outer table.
    IWTable outerTable = section.AddTable();
    //Keep a group of rows in the same page when one of the row in group is placed on next page.
    KeepGroupOfRows(innerTable, outerTable, rowSet);
    //Create a file stream.
    using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite))
    {
        //Save the Word document to the file stream.
        document.Save(outputFileStream, FormatType.Docx);
    }
}

4.2 Use the following helper method to create a table and add rows to outer table by keeping them in groups.

C#

/// <summary>
/// Create a table.
/// </summary>
private static IWTable CreateTable(int rowCount, int rowSet, WordDocument document)
{
    //Create inner table.
    WTable table = new WTable(document);
    //Specify the total number of rows & columns.
    table.ResetCells(rowCount, 1);
    int dataNum = 1;
    //Add text to table's row.
    for (int tableRowIndex = 0; tableRowIndex < table.Rows.Count; tableRowIndex++)
    {
        table[tableRowIndex, 0].AddParagraph().AppendText("Data" + dataNum);
        dataNum++;
        if ((tableRowIndex + 1) % rowSet == 0)
        {
            dataNum = 1;
        }
    }
    return table;
}
/// <summary>
/// Add rows to the outer table by keeping them in groups.
/// </summary>
private static void KeepGroupOfRows(IWTable innerTable, IWTable outerTable, int rowSet)
{
    int innerTableRowIndex = 0;
    //Create number of tables based on row set and add it to outer table rows.
    IWTable table = outerTable.AddRow().AddCell().AddTable();
    while (innerTable.Rows.Count > 0)
    {
        table.Rows.Add(innerTable.Rows[0]);
        if ((innerTableRowIndex + 1) % rowSet == 0)
        {
            table = outerTable.AddRow().Cells[0].AddTable();
        }
        innerTableRowIndex++;
    }
    //Format the outer table.
    outerTable.TableFormat.Borders.BorderType = BorderStyle.None;
    outerTable.TableFormat.IsBreakAcrossPages = false;
    outerTable.TableFormat.Paddings.Left = 0;
    outerTable.TableFormat.Paddings.Right = 0;
}

A complete working sample to keep a group of rows from splitting across pages in the Word document in C# can be downloaded from GitHub.

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

Output document generated in ASP.NET Core Word

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

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

See Also:

How to avoid table splitting across the pages in Word document

How to create a simple table in a paragraph

Is it possible to add/remove column from the table

How to insert a row with the same formatting in a Word document

How to split a table without losing their format in Word document using C#?

Conclusion

I hope you enjoyed learning about how to keep a group of rows always together in a Word document.

You can refer to our ASP.NET Core DocIO’s feature tour page to know about its other groundbreaking feature representations. You can also explore our ASP.NET Core DocIO documentation to understand how to present and manipulate data.

For current customers, you can check out our ASP.NET Core components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our ASP.NET Core DocIO and other ASP.NET Core components.

If you have any queries or require clarifications, please let us know in the comment section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

 

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