Split Word Documents

MS Word documents are widely used to keep and share information. In some cases, you may need to split the data from a Word document that could be located in different sections or pages. Also, you may need to split the pages of a single document into multiple documents. In accordance with such scenarios, this article aims to show you how to split MS Word documents programmatically using C#.

C# API to Split MS Word Documents

Aspose.Words for .NET is a powerful word processing API that lets you create and manipulate MS Word documents using C# or VB.NET. Along with that, it also allows you to split MS Word documents by section, page, or page range. You can download the API or install it within your application using NuGet.

PM> Install-Package Aspose.Words

Split Word Documents by Section using C#

Sections refer to the parts of a document where different formatting can be applied. A section may be composed of a single page, a range of pages, or the whole document. The section breaks are used to split a document into multiple sections. The following are the steps to split a Word document based on its sections using Aspose.Words for .NET.

The following code sample shows how to split MS Word documents by sections using C#.

Split Word Documents Page by Page using C#

There might be the case when a Word document contains a similar type of information on each page such as invoices or receipts. In such a case, you can split the document’s pages in order to save each invoice as a separate document. For splitting the documents page by page, you can use the helper class DocumentPageSplitter based on Aspose.Words for .NET. You can simply copy the class within your project and split the Word documents page by page by following the below steps.

  • Load the Word document using Document class.
  • Create an object of DocumentPageSplitter class and initialize it with the Document object.
  • Iterate through the document’s pages.
  • Extract each page into a new Document object using DocumentPageSplitter.GetDocumentOfPage(int PageIndex) method.
  • Save each document using Document.Save(String) method.

The following code sample shows how to split the Word document by pages using C#.

Split Word Documents by Page Range using C#

You can also specify a page range to split it from the original document using DocumentPageSplitter class. For example, if you need to split the pages from 2 to 4, just specify the starting and ending page’s index in DocumentPageSplitter.GetDocumentOfPageRange(int StartIndex, int EndIndex) method.

The following code sample shows how to split a page range from Word document using C#.

Conclusion

In this article, you have learned how to split MS Word documents programmatically using C#. The scenarios of splitting Word documents by sections, pages, and page ranges have been discussed along with code samples. You can explore more about Aspose.Words for .NET using the documentation.

See Also