Find and Replace in Word

In this post we explore how to find and replace text in Word (DOC/DOCX) documents using Java. We cover the Java Word text redaction API, searching and replacing words or phrases, case‑sensitive searches, and using regular expressions (RegEx) for advanced replacements.

The article includes:

Java Word Text Redaction Library - Installation

To start, add the Conholdate.Total for Java library to your project. Maven users can insert the following configuration into pom.xml.

<repository>
	<id>ConholdateJavaAPI</id>
	<name>Conholdate Java API</name>
	<url>https://releases.conholdate.com/java/repo</url>
</repository>        
<dependency>
	<groupId>com.conholdate</groupId>
	<artifactId>conholdate-total</artifactId>
	<version>23.6</version>
	<type>pom</type>
</dependency>

Then run:

mvn clean    
mvn install -U

For other Java frameworks, visit releases.conholdate.com.

Get a Free License

Download a free temporary license to test the library without evaluation limits.

Ask a Question

Post questions or comments about the Java PDF page remover library in this forum.

Using Java Word Text Redaction Library

You no longer need MS Word for text redaction. Java handles the entire process, eliminating external software. Below we demonstrate several ways to search and replace text in Word files using Java.

We will use the sample Word document shown below:

Remove one page from pdf

Search and Replace Words or Phrases: Steps to Write Java Program

Follow these steps to locate and replace a word or phrase in a Word document.

  1. Load the DOC/DOCX file with the Redactor class.
  2. Define the target phrase using ExactPhraseRedaction and set replacement options with ReplacementOptions.
  3. Apply the redaction via the apply method.
  4. Save the modified file to a new location using an output stream.
  5. Persist the changes with the save method.

The Java code below finds “John Doe” and replaces every occurrence with “[censored]”.

The result looks like this:

Search and Replace Word

Find and Replace Case‑Sensitive Text

If the exact letter case matters, use the following code to replace only the case‑matching instances of “John Doe”.

Output example:

Find and Replace Case-Sensitive Text

Find and Replace using Regular Expressions (RegEx) in Java

To modify a pattern rather than a single word, apply RegEx. Follow these steps:

  1. Load the document with the Redactor class.
  2. Create a RegEx pattern using RegexRedaction.
  3. Set the replacement text with ReplacementOptions.
  4. Apply the redaction via the apply method.
  5. Save the updated document using the save method.

The code below demonstrates a RegEx search and replace in a Word file.

Resulting image:

regex find and replace

Conclusion

After reading this post, you will understand how to find and replace text in Word documents using Java and the Conholdate.Total for Java library. You’ll be able to automate this task, increase productivity, and simplify document workflows. Start using Conholdate.Total for Java to enhance your Java‑based Word processing today!

Finally, keep visiting conholdate.com for regular updates.

See Also