In this article, I’ll demonstrate how to find and replace text in Word (DOC/DOCX) documents programmatically using Java. The step by step guide and code samples will cover various scenarios of finding and replacing text in Word documents.

Find and replace text in Word documents

MS Word provides an easy way to find and replace text in the documents. One of the popular use cases of finding and replacing text could be removing or replacing sensitive information within the documents before they are shared among various entities. However, the manual process may require you to install MS Word and update every document separately. In such situations, it would be handy and time-saving especially when you have integrated find and replace features within your desktop or web application. So let’s begin and see how to find and replace text in Word documents using Java in various scenarios.

Java API to Find and Replace Text in Word Documents

In order to implement the find and replace feature, we’ll use Aspose.Words for Java which is a powerful, feature-rich, and easy to use Word processing API for Java platform. You can either download its JAR or install it within your Maven-based application using the following configurations.

Repository:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

Dependency:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>20.5</version>
    <classifier>jdk17</classifier>
</dependency>

Find and Replace Text in Word Documents (DOC/DOCX) using Java

Let’s start by addressing a simple find and replace scenario where we will find the word “Sad” within the input Word document. The following are the steps to perform this operation.

The following code sample shows how to find and replace text in Word DOCX documents using Java.

Below is the input Word document that we have used in this article.

find and replace text in DOCX using Java

The following is the output after finding and replacing the word “sad”.

find and replace a word

Find and Replace Similar Words in Word DOC/DOCX using Java

You can also customize the API to find and replace text based on the similarity. For example, the words “sad”, “mad” and “bad” are following a similar pattern that ends on “ad”. Email IDs are another example of such a text. In such cases, you can define a regex pattern to find and replace all the text occurrences having a particular pattern. The following are the steps to achieve this.

The following code sample shows how to find and replace similar words based on a particular pattern using Java.

The following is the screenshot of the Word document after updating similar words.

find and replace similar words using Java

Aspose.Words also allows you to find and replace text only in the header/footer of the Word document. The following are the steps to perform this operation.

The following code sample shows how to find and replace text in the header/footer of Word document using Java.

The following screenshot shows the updated text in the footer of the Word document.

find and replace text in footer

Find and Replace Text with Meta-Characters in Word DOCX using Java

There could be the case when you need to find and replace a phrase that is divided into multiple lines or paragraphs. In such cases, you will have to take care of the paragraph, section, or line breaks. Aspose.Words for Java makes it simple for you to handle such cases quite easily. The following are the meta-characters that you can use for different breaks:

  • &p: paragraph break
  • &b: section break
  • &m: page break
  • &l: line break

The following code sample demonstrates how to find and replace the text with a paragraph break in a Word document.

The following is the screenshot of the output Word document.

find and replace text with meta characters

Conclusion

In this article, you have seen how to find and replace text in Word DOC/DOCX documents programmatically using Java. Various scenarios of finding and replacing text in MS Word DOCX files have been addressed with the help of code samples. You can learn more about Aspose.Words for Java from the documentation.

See Also