I want to manipulate text runs in the XWPFDocument using Apache POI 3.10 and the method XWPFRun.setText()
does not work the way I was expecting. This code:
XWPFDocument doc = new XWPFDocument(resp.getContent());
for (XWPFParagraph paragraph : doc.getParagraphs()) {
for (XWPFRun run : paragraph.getRuns()) {
int textPosition = run.getTextPosition();
if (run.getText(textPosition) != null) {
System.out.println("old value: " + run.getText(textPosition));
run.setText("value changed", textPosition);
System.out.println("new value: " + run.getText(textPosition));
}
}
}
produces the output:
old value: change me
new value: change me
What is the correct way of updating single run in XWPFDocument?
setText()
, so I want to read from memory withgetText()
. For me it is quite obvious while using methods similar to getters and setters. And if you replace text in Word, you can see the replaced value without saving. – Artur MalinowskiXWPFRun.toString()
to get the text out of the run, does that help? And have you made sure you're using the latest version of Apache POI? – Gagravarr