How to Split a Word DOCX Document into Separate Documents (by Page) using Java

Cloudmersive
2 min readAug 31, 2022

--

It’s not uncommon that each page in a consolidated DOCX file stands alone as its own pamphlet of unique information; such documents can be stored in both consolidated and separated forms to facilitate efficient access to relevant info. With our DOCX splitter API, you can easily create new documents out of each page of a DOCX File, and specify whether those documents should be returned as new files or URL links (you can configure this with a Boolean in the optional parameter ‘returnDocumentContents’).

To take advantage of this API, all you need to do is follow steps below to structure your API call using Java code examples provided from our API console. You’ll also needs to obtain a secure API key by registering a free account on our website (this account will provide a limit of 800 API calls per month).

To begin installing the SDK with Maven, let’s first add a reference to the pom.xml repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

And let’s next add a reference to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Now we can call the API, beginning by including our imports at the top of our file & configuring our API key in the input below that:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.SplitDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
SplitDocumentApi apiInstance = new SplitDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
Boolean returnDocumentContents = true; // Boolean | Set to true to return the contents of each Worksheet directly, set to false to only return URLs to each resulting document. Default is true.
try {
SplitDocxDocumentResult result = apiInstance.splitDocumentDocx(inputFile, returnDocumentContents);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling SplitDocumentApi#splitDocumentDocx");
e.printStackTrace();
}

All that’s left now is to configure your file path input, and you’re good to go.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet