How to manage pagination on word document in C#

2016-06-21 08:21:32 Written by  support iceblue
Rate this item
(0 votes)

When we want to manage the pagination for the paragraphs, we can insert a page break directly. But later we may find that it is hard to add or remove text above the page break and then we have to remove the whole page break. With Microsoft word, we can also use the Paragraph dialog to manage the pagination flexible for the word paragraph as below:

How to manage pagination on word document in C#

We have already shown you how to insert page break to the word document, this article we will show you how to manage the pagination by using the Paragraph.Format offered by Spire.Doc. Here comes to the steps of how to manage the pagination for the word document paragraph in C#:

Step 1: Create a new word document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("sample.docx");

Step 2: Get the first section and the paragraph we want to manage the pagination

Section sec = doc.Sections[0];
Paragraph para = sec.Paragraphs[4];

Step 3: Set the pagination format as Format.PageBreakBefore for the checked paragraph.

para.Format.PageBreakBefore = true; 

Step 4: Save the document to file.

doc.SaveToFile("result.docx",FileFormat.Docx2010);

Effective screenshot after managing the pagination for the word document:

How to manage pagination on word document in C#

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
namespace ManagePage
{
 class Program
    {

     static void Main(string[] args)
     {
         Document doc = new Document();
         doc.LoadFromFile("sample.docx");

         Section sec = doc.Sections[0];
         Paragraph para = sec.Paragraphs[4];
         para.Format.PageBreakBefore = true;

         doc.SaveToFile("result.docx", FileFormat.Docx2010);

     }

    }
}

Additional Info

  • tutorial_title: Manage pagination on word document in C#
Last modified on Friday, 03 September 2021 03:34