The code snippet below show you how you can replace string in Microsoft Word document using the Apache POI library. The class below have three method, the openDocument()
, saveDocument()
and replaceText()
.
The routine for replacing text is implemented in the replaceText()
method. This method take the HWPFDocument
, the String
to find and the String
to replace it as parameters. The openDocument()
opens the Word document. When the text replacement is done the Word document will be saved by the saveDocument()
method.
And here is the complete code snippet. It will replace every dolor
texts into d0l0r
texts in the source document, the lipsum.doc
, and save the result in a new document called new-lipsum.doc
.
package org.kodejava.poi;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Section;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
public class WordReplaceText {
private static final String SOURCE_FILE = "lipsum.doc";
private static final String OUTPUT_FILE = "new-lipsum.doc";
public static void main(String[] args) throws Exception {
WordReplaceText instance = new WordReplaceText();
try (HWPFDocument doc = instance.openDocument(SOURCE_FILE)) {
if (doc != null) {
HWPFDocument newDoc = instance.replaceText(doc, "dolor", "d0l0r");
instance.saveDocument(newDoc, OUTPUT_FILE);
}
}
}
private HWPFDocument replaceText(HWPFDocument doc, String findText, String replaceText) {
Range range = doc.getRange();
for (int numSec = 0; numSec < range.numSections(); ++numSec) {
Section sec = range.getSection(numSec);
for (int numPara = 0; numPara < sec.numParagraphs(); numPara++) {
Paragraph para = sec.getParagraph(numPara);
for (int numCharRun = 0; numCharRun < para.numCharacterRuns(); numCharRun++) {
CharacterRun charRun = para.getCharacterRun(numCharRun);
String text = charRun.text();
if (text.contains(findText)) {
charRun.replaceText(findText, replaceText);
}
}
}
}
return doc;
}
private HWPFDocument openDocument(String file) throws Exception {
URL res = getClass().getClassLoader().getResource(file);
HWPFDocument document = null;
if (res != null) {
document = new HWPFDocument(new POIFSFileSystem(
new File(res.getPath())));
}
return document;
}
private void saveDocument(HWPFDocument doc, String file) {
try (FileOutputStream out = new FileOutputStream(file)) {
doc.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Maven Dependencies
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>5.2.5</version>
</dependency>
</dependencies>
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
Your method
replaceText(...)
will not fulfill its contract. It will work on char by char replacements, but not words (String), since a word can and will be stored in different runs. So you should change the signature of your method toreplaceText(HWPFDocument doc, char a, char b)
.I try your example code and I am having problems,
WordReplaceText
cannot be resolved to a type.Hi Hoacuong,
Can you show me your complete code?
why its not working to me?
Hi, thanks for this good document for developers. On the net I found compatible API for Java and Android called JWord for Microsoft Word. There are a number of use cases and one of the options is replacement. Look at http://www.independentsoft.de/jword/tutorial/replacetext.html API is available and for .NET which is great! API developed by Independentsoft company.
Doesn’t work for split runs in document.