Friday, 20 November 2020

How to replace text in a Word document in Java

Introduction

This article will mainly introduce how to replace text in a Word document using Java code. According to different requirements, there will be divided into three aspects to demonstrate it as follows.

l  Replace all matched text with new text

l  Replace the first matched text with new text

l  Replace all matched text with an image

The third-party library I used in this tutorial is Free Spire.Doc for Java. It is a free and professional Java API that enables Java applications to create, manipulate, convert and print Word documents without using Microsoft Office.

Before typing codes, please make sure that the Jar file located at Free Spire.Doc for Java is inserted in your project. If you’re creating a Maven project, just add the following configurations to the pom.xml file.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Or you can download the package from the E-iceblue website, unzip it and find Spire.Doc.jar in the
“lib” folder. Finally manually add it to your project.
Using the code

Replace all matched text with new text
import com.spire.doc.*;
public class ReplaceTextWithNewText {
public static void main(String[] args) {
//Load the Word document
Document document = new Document("C:\\Users\\Test1\\Desktop\\Sample.docx");

//Replace the specified text with new text
document.replace("society", "NewText", false, true);

//Save the document
document.saveToFile("output/ReplaceAllMatchedText.docx", FileFormat.Docx_2013);
}
}

Output


Replace the first matched text with new text

import com.spire.doc.*;
public class ReplaceFirstText {
public static void main(String[] args) {
//Load the Word document
Document document = new Document("C:\\Users\\Test1\\Desktop\\Sample.docx");

//Set to replace only the first occurrence of the specified text
document.setReplaceFirst(true);

//Replace with new text
document.replace("Society", "NewText", false, true);

//Save the document
document.saveToFile("output/ReplaceFirstMatchedText.docx", FileFormat.Docx_2013);
}
}

Output


Replace all matched text with an Image

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
public class ReplaceTextWithImage {
public static void main(String[] args) {
//Load a sample Word file
Document document = new Document();
document.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");

//Find the string 'E-iceblue' in the document
TextSelection[] selections = document.findAllString("society", true, true);

//Replace the string with an image
int index = 0;
TextRange range = null;
for (Object obj : selections) {

TextSelection textSelection = (TextSelection)obj;
DocPicture pic = new DocPicture(document);
pic.loadImage("C:\\Users\\Test1\\Desktop\\Image.png");
range = textSelection.getAsOneRange();
index = range.getOwnerParagraph().getChildObjects().indexOf(range);
range.getOwnerParagraph().getChildObjects().insert(index,pic);
range.getOwnerParagraph().getChildObjects().remove(range);
}

//Save the document
document.saveToFile("output/ReplaceTextWithImage.docx", FileFormat.Docx_2013);
}
}

Output


Conclusion

In addition to replacing text, Free Spire.Doc for Java supports numerous functions of manipulating

Word documents. If there is any problem about codes or installation steps, please make a note on the

Comment or Forum.

 


No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...