How to Convert Word DocX to TXT with Java

2 min readJun 8, 2022


Text (TXT) file are basically the antithesis of DocX files. They have no specific formatting, and can be used to transfer information easily across a variety of platforms. If you’re looking to build in a web-based DocX to TXT conversion in to your application, we have the perfect solution for you: Our DocX to TXT Conversion API will make the conversion automatically, and it’ll allow you to specify how whitespace in your document should be handled by the new file (the default option is “minimizeWhitespace”). Let’s take a look below at how you can quickly implement this API & structure your API call with Java.

Start with Maven installation. Add the below reference to the repository in pom.xml:


Then add one to the dependency:


Lastly, add in your import classes and call the API:

// 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.ConvertDocumentApi;
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)
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
String textFormattingMode = "textFormattingMode_example"; // String | Optional; specify how whitespace should be handled when converting the document to text. Possible values are 'preserveWhitespace' which will attempt to preserve whitespace in the document and relative positioning of text within the document, and 'minimizeWhitespace' which will not insert additional spaces into the document in most cases. Default is 'minimizeWhitespace'.
try {
TextConversionResult result = apiInstance.convertDocumentDocxToTxt(inputFile, textFormattingMode);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentDocxToTxt");

Not too hard, right? No further code is required. All you need is a Cloudmersive API key, which you can easily obtain by creating a free account on our website.




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