Find and Replace in Word

In today’s blog post, we will explore how to find and replace text in Word (DOC/DOCX) documents using Java. We will cover various aspects of this task, including working with the Java Word text redaction API, finding and replacing words or phrases, case-sensitive searches, and even replacing text using regular expressions (RegEx) for advanced replacements.

The following points will be covered in this article:

Java Word Text Redaction Library - Installation

To begin, we need to set up the Conholdate.Total for Java library. The installation procedure of this Java library is very simple. The Maven users can simply add the following configurations in the pom.xml file.

<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 the following commands.

mvn clean    
mvn install -U

To configure other Java frameworks, please visit releases.conholdate.com.

Get a Free License

You can download a free temporary license to test the library without evaluation limitations.

Ask a Question

You can post your questions or comments regarding the Java PDF page remover library in this forum.

Using Java Word Text Redaction Library

Say goodbye to the hassle of relying on MS Word for text redaction. With Java, we can handle it all ourselves, no external software is needed. We’ll explore various approaches to tackle finding and replacing text in Word documents using Java. We’ll leverage the power of Java to perform efficient search and replace operations. You’ll be amazed at how easily you can handle text modifications programmatically using this versatile programming language.

We will use the sample Word document as shown in the following screenshot:

Remove one page from pdf

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

Following are the steps explaining how to find and then replace any instances of a word/phrase in a Word document.

  1. First, load the DOC/DOCX file using the Redactor class.
  2. Then, find the target phrase or word using ExactPhraseRedaction and ReplacementOptions classes.
  3. Next, apply the redaction using the apply method of Redactor.
  4. After that, save the modified file to a different location using the output stream.
  5. Finally, save the redaction changes using the save method.

The following Java code finds the word “John Doe” in the above Word document and replaces all the instances of “John Doe” with the word “[censored]”.

The output of the code is as follows.

Search and Replace Word

Find and Replace Case-Sensitive Text

The exact letter case of the word appears to be important to you, and you only want to replace it with words that are pertinent to your case-sensitive search. The following code replaces the occurrences of the exact case match of the word “John Doe” in Java. The output of the code is as follows.

Find and Replace Case-Sensitive Text

Find and Replace using Regular Expressions (RegEx) in Java

If you want to update a pattern inside your document rather than a specific word. You can use regular expressions (RegEx) in Java programs to find and replace any pattern of text by following the steps below.

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

The following Java code shows how to perform the word search in an MS Word document using RegEx and replace it with some other text. The output of the code is as follows.

regex find and replace

Conclusion

By the end of this blog post, you will have a comprehensive understanding of how to find and replace text in Word documents using Java with the Conholdate.Total for Java library. You will be equipped with the knowledge and tools to automate this task efficiently, improving productivity and simplifying document manipulation workflows. So let’s get started and leverage the power of Conholdate.Total for Java to improve your Java-based Word document processing!

Finally, please keep visiting conholdate.com for regular updates.

See Also