I'm new with Apache POI and I'm having trouble replacing text.
I copied my code here Replacing a text in Apache POI XWPF not working
It works but it does not replace the text but concatenates it. So if I have "Quick brown fox jumps over" and replace "over" with "under". I get "Quick brown fox jumps overQuick brown fox jumps under".
What is wrong?
So here's the code:
public class testPOI {
public static void main(String[] args) throws Exception{
String filepath = "F:\\MASTER_DOC.docx";
String outpath = "F:\\Test.docx";
XWPFDocument doc = new XWPFDocument(new FileInputStream(filepath));
for (XWPFParagraph p : doc.getParagraphs()){
int numberOfRuns = p.getRuns().size();
// Collate text of all runs
StringBuilder sb = new StringBuilder();
for (XWPFRun r : p.getRuns()){
int pos = r.getTextPosition();
if(r.getText(pos) != null) {
sb.append(r.getText(pos));
}
}
// Continue if there is text and contains "test"
if(sb.length() > 0 && sb.toString().contains("test")) {
// Remove all existing runs
for(int i = 0; i < numberOfRuns; i++) {
p.removeRun(i);
}
String text = sb.toString().replace("test", "DOG");
// Add new run with updated text
XWPFRun run = p.createRun();
run.setText(text);
p.addRun(run);
}
}
doc.write(new FileOutputStream(outpath));
}
}
EDIT 1: THIS IS WEIRD! I tried replacing on 2nd run works fine. Something is wrong with 1st run. Can anyone point it out?
for(int i = numberOfRuns-1; i >=0; i--) { p.removeRun(i); }
– Clemens Klein-Robbenhaarp.removeRun(i);
does not throwIndexOutOfBoundsException
ifi
is larger then the size of the list or the problem would have been obvious. – Clemens Klein-Robbenhaar