C#-Two Ways to Manage Page Breaks in Word Table

Sometimes, we’re forced to start a table near the bottom of a page. Microsoft Word automatically places a page break in a table if a single row or several rows are too long to fit between the top and bottom margins of the page. So we’d rather the same row can be placed on one page or have the entire table on the next page instead of being broken over two pages. In this article, I’ll introduce you two ways to avoid page breaks in Word tables via a C# Word component.

Assume that we have a Word table like this (Row 2 splits to different pages), we may want to optimize the layout by the following two methods.

4

Method 1: Keep the entire table on the same page.

Step 1: Create a new Word document and load the test file.

           Document doc = new Document(“Test.docx”);

Step 2: Get the table from Word document.

           Table table = doc.Sections[0].Tables[0] as Table;

Step 3: Change the paragraph setting to keep them together.

       foreach (TableRow row in table.Rows)

       {    foreach (TableCell cell in row.Cells)

             {   foreach (Paragraph p in cell.Paragraphs)

                 {                     

                      p.Format.KeepFollow = true;

                  }

             }

       }

Step 4: Save and launch the file.

       doc.SaveToFile(“Result.docx”, FileFormat.Docx2010);

       System.Diagnostics.Process.Start(“Result.docx”);

Result:

5

Method 2: Prevent Word from breaking a table row across two pages.

There still exists a situation where the table is not small enough to fit on one page, you can prevent Word from breaking a table row that contains multiple paragraphs across two pages. In this example, the second row has been splitted in different pages, we can set property of TableFormat with following sentence to avoid this problem.

         table.TableFormat.IsBreakAcrossPages = false;

Use this sentence to replace Step 3, you’ll get the layout as below:

7

Full C# Code:

[Method 1]


static
void Main(string[] args)

{

   Document doc = new Document(“Test.docx”);

   Table table = doc.Sections[0].Tables[0] as Table;           

   foreach (TableRow row in table.Rows)

   {

       foreach (TableCell cell in row.Cells)

       {   

             foreach (Paragraph p in cell.Paragraphs)

             {

                  p.Format.KeepFollow = true;

             }

        }

    } 

doc.SaveToFile(“Result.docx”,FileFormat.Docx2010);

System.Diagnostics.Process.Start(“Result.docx”);

}

[Method 2]

static void Main(string[] args)

{

    Document doc = new Document(“Test.docx”);

    Table table = doc.Sections[0].Tables[0] as Table;

    table.TableFormat.IsBreakAcrossPages = false;

    doc.SaveToFile(“Result.docx”, FileFormat.Docx2010);

    System.Diagnostics.Process.Start(“Result.docx”);

        }

Note: Before using this sample code to achieve the purpose, you need to add Spire.Doc dll as reference in project.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s