How to split a single Word DOCX into Separate Documents by Page in Java

Splitting documents programmatically is Java is chore of epic proportions. If you have ever tried parsing a DOCX file, then you know what I am talking about. If you would rather not spend the rest of your weeks dealing with the problem, then I have just the solution for you. We will use an API library that can get this done for us in no time at all.

First we need a respository reference for Jitpack.

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

Next comes our dependency for our client.

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>

And lastly, our function call.

// 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: ApikeyApiKeyAuth 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/file"); // 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();}

Done! Talk about easy.

Image for post
Image for post

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store