I'm using Apache POI XWPF to manipulate a docx file, I need to update some words of paragraph and change the font style of it. For updating a single word, it's ok, let's assume that my docx content has the paragraphs bellow:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer pretium sodales nisl, ut ornare ligula vehicula vitae. Fusce non magna feugiat, sagittis massa at, fermentum nibh. Curabitur auctor leo vitae sem tempus, facilisis feugiat orci vestibulum. Mauris molestie sem sem, id venenatis arcu congue id. Duis nulla quam, commodo vel dolor eget, tempor varius sem. Pellentesque gravida, lectus eu mollis pretium, sapien nibh consectetur lacus, non pellentesque lectus ipsum ornare eros. Maecenas at magna nunc.
Nulla sagittis aliquam maximus. Cras faucibus id neque sed faucibus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Phasellus fermentum nulla sed nibh varius maximus. Pellentesque dui lorem, luctus non lorem a, blandit lacinia arcu. Nunc porttitor erat ut elit hendrerit malesuada. Sed ut ex ultricies, rutrum est ut, vulputate orci. Suspendisse vitae diam ullamcorper, pulvinar tellus vitae, feugiat ex. In hac habitasse platea dictumst. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Proin massa lectus, venenatis eget massa a, fringilla molestie nisl.
I just do something like it, in this case, the code works, I just want to update the word "ipsum" to "hello world":
File file = new File("document.docx");
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
XWPFDocument document = new XWPFDocument(fis);
List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph p: paragraphs) {
List<XWPFRun> runs = p.getRuns();
for (XWPFRun r: runs) {
String endText = r.getText(0).replaceFirst("ipsum" , "hello world");
r.setText(endText,0);
}
}
document.write(new FileOutputStream(uuid + ".docx"));
fis.close();
document.close();
But if I try to change the text style, like bold, or even change the font color, all of "run" will change, not only the world that I've searched. What I have to do to change the font style only of the word "ipsum" in the example text?