0
votes

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?

1
What happens if you try to remove the runs from end to beginning for(int i = numberOfRuns-1; i >=0; i--) { p.removeRun(i); }Clemens Klein-Robbenhaar
@ClemensKlein-Robbenhaar It worked! Is there an explanation on why?Audix
You modify (shorten) a list while iterating over it. Iterating in "forward"-direction causes you to hop over every second item in the list. (If wanted I can provide a separate answer for details.) It seems that p.removeRun(i); does not throw IndexOutOfBoundsException if i is larger then the size of the list or the problem would have been obvious.Clemens Klein-Robbenhaar

1 Answers

0
votes

I tried this. It works.

    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 = numberOfRuns; i >=0 ; 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));
}
}