You cannot directly modify text in XWPFParagraph. An XWPFParagraph consists of one or more instances of XWPFRun. They provide a way to set text.
To change the text, your code will look something like this:
public void changeText(XWPFParagraph p, String newText) { List<XWPFRun> runs = p.getRuns(); for(int i = runs.size() - 1; i > 0; i--) { p.removeRun(i); } XWPFRun run = runs.get(0); run.setText(newText, 0); }
This ensures that you have only one text run (first) and will replace all text with what you provided.
source share