{{< figure align=center src=“images/Split-Word-Documents.jpg” alt=“Split Word Documents Java”>}}
In various cases, you need to split an MS Word document into multiple documents. For example, you may need to create a separate document for each page, section, or collection of pages in a Word document. In order to automate the document splitting, this article covers how to split MS Word DOCX programmatically using Java. The following sections provide a step‑by‑step tutorial and code samples of the above‑mentioned splitting criteria.
- Java API to Split Word Documents
- Split a Word DOCX/DOC using Java
- Use Page Range to Split Word Document
- Split Word Document by Sections
- Get Free API License
Java API to Split Word DOCX
Aspose.Words for Java is a powerful and feature‑rich document manipulation API that lets you create and process MS Word documents. In addition to basic and advanced Word automation features, the API also allows you to split a Word document into multiple documents. You can either download the API or install it within your Maven‑based application using the following configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>21.1</version>
<classifier>jdk17</classifier>
</dependency>
Word Document Splitter - Helper Class
Before you start splitting the documents, add the following helper class to your project. It implements a Java document splitter based on Aspose.Words for Java. After adding the class, you can split documents using the code samples in the sections below.
Split a Word DOCX using Java
First, see how to split an MS Word document by page. Each page of the source document becomes a separate Word document. Follow these steps:
- Load the Word document using the Document class.
- Create a PageSplitter object and initialize it with the Document instance.
- Loop through the pages in the document.
- Call PageSplitter.getDocumentOfPage(Int pageIndex) to retrieve each page as a Document object.
- Save the document with the Document.save(String) method.
The code sample below demonstrates page‑by‑page splitting.
{{< gist aspose-com-gists 163f03e6cd53fd757db6b4dc041e797c “split-word-document-by-page.java” >}}
Use Page Range to Split Word DOCX in Java
You can also split a specific page range from the source document. Follow these steps:
- Load the Word document using the Document class.
- Create a PageSplitter object and initialize it with the Document instance.
- Use PageSplitter.getDocumentOfPageRange(Int, Int) to retrieve the selected pages as a Document object.
- Save the result with the Document.save(String) method.
The following code shows how to split a document by page range.
{{< gist aspose-com-gists 163f03e6cd53fd757db6b4dc041e797c “split-word-document-by-page-range.java” >}}
Split a Word Document by Sections using Java
Aspose.Words for Java also lets you split a document at section breaks. Perform these steps:
- Load the Word document using the Document class.
- Iterate through each section with Document.getSections().
- Clone a section into a Section object using Document.getSections().get(index).deepClone().
- Create a new Document and add the cloned section via Document.getSections().add(Section).
- Save the new document with Document.save(String).
The code sample below illustrates section‑by‑section splitting.
{{< gist aspose-com-gists 163f03e6cd53fd757db6b4dc041e797c “split-word-document-by-section.java” >}}
Get a Free API License
You can get a free temporary license to try the API without evaluation limitations.
Conclusion
In this article, you learned how to split MS Word DOCX/DOC files using Java. The step‑by‑step guide and code samples demonstrated splitting by sections, individual pages, and page ranges. Explore more features of the Java Word API in the documentation.