Replacing a text in Apache POI XWPF not working

replace text in word document using java
apache poi edit word document example
xwpfrun
apache poi javadoc eclipse
apache poi documentation
java microsoft word api
apache poi documentation excel
apache poi jar for java 8

I'm currently trying to work on the code mentioned on a previous post called Replacing a text in Apache POI XWPF.

I have tried the below and it works but I don't know if I am missing anything. When I run the code the text is not replaced but added onto the end of what was searched. For example I have created a basic word document and entered the text "test". In the below code when I run it I eventually get the new document with the text "testDOG".

I have had to change the original code from String text = r.getText(0) to String text = r.toString() because I kept getting a NullError while running the code.

import java.io.*;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;


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(OPCPackage.open(filepath));
    for (XWPFParagraph p : doc.getParagraphs()){
        for (XWPFRun r : p.getRuns()){
            String text = r.toString();
            if(text.contains("test")) {
                text = text.replace("test", "DOG");
                r.setText(text);
            }
        }
    }
   doc.write(new FileOutputStream(outpath));
}

EDIT: Thanks for your help everyone. I browsed around and found a solution on Replace table column value in Apache POI

This method replace search Strings in paragraphs and is able to work with Strings spanning over more than one Run.

  private long replaceInParagraphs(Map<String, String> replacements, List<XWPFParagraph> xwpfParagraphs) {
    long count = 0;
    for (XWPFParagraph paragraph : xwpfParagraphs) {
      List<XWPFRun> runs = paragraph.getRuns();

      for (Map.Entry<String, String> replPair : replacements.entrySet()) {    
        String find = replPair.getKey();
        String repl = replPair.getValue();
        TextSegement found = paragraph.searchText(find, new PositionInParagraph());
        if ( found != null ) {
          count++;
          if ( found.getBeginRun() == found.getEndRun() ) {
            // whole search string is in one Run
            XWPFRun run = runs.get(found.getBeginRun());
            String runText = run.getText(run.getTextPosition());
            String replaced = runText.replace(find, repl);
            run.setText(replaced, 0);
          } else {
            // The search string spans over more than one Run
            // Put the Strings together
            StringBuilder b = new StringBuilder();
            for (int runPos = found.getBeginRun(); runPos <= found.getEndRun(); runPos++) {
              XWPFRun run = runs.get(runPos);
              b.append(run.getText(run.getTextPosition()));
            }                       
            String connectedRuns = b.toString();
            String replaced = connectedRuns.replace(find, repl);

            // The first Run receives the replaced String of all connected Runs
            XWPFRun partOne = runs.get(found.getBeginRun());
            partOne.setText(replaced, 0);
            // Removing the text in the other Runs.
            for (int runPos = found.getBeginRun()+1; runPos <= found.getEndRun(); runPos++) {
              XWPFRun partNext = runs.get(runPos);
              partNext.setText("", 0);
            }                          
          }
        }
      }      
    }
    return count;
  }

Replacing a text in Apache POI XWPF not working, I'm currently trying to work on the code mentioned on a previous post called Replacing a text in Apache POI XWPF. I have tried the below and it works but I don't  Replacing a text with multiple words in Apache POI XWPF not working - org.apache.xmlbeans.impl.values.XmlValueDisconnectedException 2 Replace text in text box of docx by using Apache POI

Your logic is not quite right. You need to collate all the text in the runs first and then do the replace. You also need to remove all runs for the paragraph and add a new single run if a match on "test" is found.

Try this instead:

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));
    }
}

java Replacing a text in Apache POI XWPF?, XWPFParagraph; import org.apache.poi.xwpf.usermodel. runs, and adding a new run with the adjusted text seems to solve the problem of text replacement. Apache POI XWPF does not replace but concatenates 1 Replacing a text with multiple words in Apache POI XWPF not working - org.apache.xmlbeans.impl.values.XmlValueDisconnectedException

just change text for every run in your paragraph, and then save the file. this code worked for mi

XWPFDocument doc = new XWPFDocument(new FileInputStream(filepath));
for (XWPFParagraph p : doc.getParagraphs()) {
    StringBuilder sb = new StringBuilder();
    for (XWPFRun r : p.getRuns()) {
    String text = r.getText(0);
    if (text != null && text.contains("variable1")) {
        text = text.replace("variable1", "valeur1");
        r.setText(text, 0);
    }
    if (text != null && text.contains("variable2")) {
        text = text.replace("variable2", "valeur2");
        r.setText(text, 0);
    }
    if (text != null && text.contains("variable3")) {
        text = text.replace("variable3", "valeur3");
        r.setText(text, 0);
    }
    }

}

doc.write(new FileOutputStream(outpath));

org.apache.poi.xwpf.usermodel.XWPFRun java code examples , Best Java code snippets using org.apache.poi.xwpf.usermodel.XWPFRun (​Showing top 20 Replacing a text in Apache POI XWPF not working. String outpath  I just found Apache POI library very useful for editing Word files using Java. Specifically, I want to edit a DOCX file using Apache POI's XWPF classes. I found no proper method / documentation following which I could do this. Can somebody please explain in steps, how to replace some text in a DOCX

Don't waste your time when you can keep things simple:

//download from http://www.independentsoft.de/jword/evaluation.html import com.independentsoft.office.word.WordDocument;

public class JWORD {

public static void main(String[] args) {

    String filepath = "C:\\Users\\setrivayne\\Downloads\\TEST.docx";
    String outpath = "C:\\Users\\setrivayne\\Downloads\\TEST.docx";

    try {

         WordDocument doc = new WordDocument(filepath);

         doc.replace("FIRST NAME", "first name");
         doc.replace("MIDDLE NAME", "middle name");
         doc.replace("LAST NAME", "last name");

         doc.save(outpath, true);

     } catch (Exception e) {
         System.out.println(e.getMessage());
         e.printStackTrace();
     }
}

}

HWPF and XWPF - Java API to Handle Microsoft Word , Currently, there are only a handful of example programs using HWPF and and change text of paragraph, character run range is now invalid and should not be  Replacing a text in Apache POI XWPF not working (6) I'm currently trying to work on the code mentioned on a previous post called Replacing a text in Apache POI XWPF. I have tried the below and it works but I don't know if I am missing anything.

Worth noticing that, run.getPosition() returns -1 most of the cases. But it does not effect when there is only one text postion per a run. But, technically it can have any number of textPositions and I've experienced such cases. So, the best way is to getCTR () for run and terate through each the run for count of textPositions. Number of textPositions are equal to ctrRun.sizeOfTArray()

A sample code

for (XWPFRun run : p.getRuns()){
     CTR ctrRun =  run.getCTR();
     int sizeOfCtr = ctrRun.sizeOfTArray();
     for(int textPosition=0; textPosition<sizeOfCtr){
            String text = run.getText(textPosition);
            if(text.contains("test")) {
                 text = text.replace("test", "DOG");
                 r.setText(text,textPosition);
            }
     }

 }

Replacing a text with multiple words in Apache POI XWPF not working, java - Replacing a text with multiple words in Apache POI XWPF not working - org​.apache.xmlbeans.impl.values.XmlValueDisconnectedException java. I looked at the apache POI documentation and created a function that redacts all the text in a powerpoint. Function works well in replacing texts in slides but not the texts found in grouped textboxes.

Enhancing Word Document Generation with Java Actions, We can start with replacing some static text in a word document, and then move onto making this action This example will replace the word “needle” with “​haystack”. IMendixObject; import org.apache.poi.xwpf.usermodel. the text of this text run or null if not set Just check if it is not null before calling contains() on it And btw if you want to replace the text you need to set it in position from which you get it, in this case r.setText(text, 0);. Otherwise text will be added not replaced

How do I replace text in Microsoft Word document using Apache POI , doc and save the result in a new document called new-lipsum.doc . package org.​kodejava.example.poi; import org.apache.poi.hwpf. For basic text extraction, make use of org.apache.poi.hwpf.extractor.WordExtractor.It accepts an input stream or a HWPFDocument.The getText() method can be used to get the text from all the paragraphs, or getParagraphText() can be used to fetch the text from each paragraph in turn.

mesutsaritas/replacing-text-apache-POI: Replacing a text in , Replacing a text in Apache POI XWPF. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build  The following are top voted examples for showing how to use org.apache.poi.xwpf.usermodel.XWPFDocument.These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

Comments
  • What version of Apache POI is with this with? And if it isn't the latest, have you tried upgrading?
  • Thanks for the reply, I'm using the latest stable version 3.10-FINAL. I'll give James idea a go. Thanks.
  • Very nice! Combined this implementation with logic from stackoverflow.com/a/30131160/4411975 and now we have a way to do a global search and replace :)
  • Worth noticing that, run.getPosition() returns -1 most of the cases. But it does not effect when there is only one text postion per a run. But, technically it can have any number of textPositions and I've experienced such cases. So, the best way is to getCTR () for run and terate through each the run for count of textPositions. Number of textPositions are equal to ctrRun.sizeOfTArray()
  • It works a bit but not completely. If for example you use the below text it doesn't work and the format goes all wrong. Jeget lacinia sem commodo ac. Fusce molestie sodales suscipit. Maecenas tortor sem, rutrum vel test eu, elementum fermentum sem.
  • Thanks for your help James. I found a solution on [link] (stackoverflow.com/questions/8398259/…)