Split MS Word Documents using C++

Split Word Documents C++

MS Word is a popular format for storing and sharing information. There might be scenarios where you want to split a Word document into multiple files. For example, a document may contain different sections that need to be shared with different people or contain some confidential information that needs to be separated. In such cases, splitting the Word document proves to be helpful. In this article, you will learn how to split MS Word documents using C++.

C++ API for Splitting Word Documents

Aspose.Words for C++ is a native C++ library that allows you to generate, change and convert Microsoft Word documents. In addition, it also supports splitting Word documents into multiple files. You can either install the API through NuGet or download it directly from the Downloads section.

PM> Install-Package Aspose.Words.Cpp

Split Word Documents by Page using C++

The following are the steps to split Word files using C++.

  • Load the Word document using the Document class.
  • Create an instance of the DocumentPageSplitter class and initialize it with the Document object created in the previous step.
  • Loop through the pages of the Document.
  • Extract each page with DocumentPageSplitter.GetDocumentOfPage(int32_t PageIndex) method.
  • Save the documents using the Document->Save (System::String fileName) method.

The following is the sample code to split a Word document by page.

Use Page Range to Split a Word Document

Instead of separating each and every page, you can also split the Word document by providing a page range. The following are the steps to split a Word document using page range.

  • Firstly, load the Word document using the Document class.
  • Create an instance of the DocumentPageSplitter class and initialize it with the previous step’s Document object.
  • Retrieve the pages in the required range using the DocumentPageSplitter.GetDocumentOfPageRange(int32_t pageIndex, int32_t pageIndex) method.
  • Lastly, save the Word document using the Document->Save (System::String fileName) method.

The following is the sample code to split a Word document using page range.

Split Word Document by Sections using C++

Word files can contain one or more sections that might have different formatting and may consist of any number of pages. To add a new section in the Word file, use the Layout>Breaks>Section Breaks option. The following are the steps to split Word files by sections using Aspose.Words for C++ API.

The following is the sample code to split Word documents by sections.

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to split Word documents into multiple files using C++. You have learned how to use Aspose.Words for C++ API to split word documents by page, page range, and sections. You can explore the API in detail by using the official documentation. If you have any questions, please feel free to contact us on the forum.

See Also