Search and Replace a Text String in a Word Document in Java

When editing a Word document, finding and replacing text can be a tedious, manual task — but, it doesn’t have to be! In this tutorial we’re going to demonstrate how you can leverage API technology in Java to simplify the search and replace process.

Image for post
Image for post

Starting off, we’re going to employ Maven or Gradle to install our Java SDK. For Maven, add a reference to the repository in pom.xml:


Then, add a reference to the dependency:


If you prefer to use Gradle, you will first add the following to your root build.gradle at the end of repositories:

allprojects {
repositories {
maven { url '' }

After that, add the dependency in build.gradle:

dependencies {
implementation 'com.github.Cloudmersive:Cloudmersive.APIClient.Java:v3.54'

Now that the package is installed, we’re ready to instance our API and call the search and replace function:

// 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.EditDocumentApi;

And there you have it; out with the old text, and in with the new!

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