0

I'm trying to replace placeholders in a docx file. I developed the code but I found something tricky. I have a XWPFRun and in debug I can see that the content is

N ${placeholder1}/${placeholder1} of ${placeholder3}

But, when I use run.getText(0), I obtain a substring that does not contain the third placeholder.

However, when I use run.text() and run.toString() methods I get the whole content:

N ${placeholder1}/${placeholder1}\t\t\t\tof ${placeholder3}

Now if I use the getText(0) method I can't replace the third placeholder, while when I tried to use the text() method I obtain

N ${placeholder1}/${placeholder1} of VALUE of ${placeholder3}`
run.setText(run.text().contains(placeholder) ?
    run.text().replace(placeholder, placeholderValue) :
    run.text(),0);

I use apache poi 3.17

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
vince
  • 1
  • Not bad for a first question. Try to use backticks ` rather than bold ** for code snippets and tripple-backticks ``` to introduce a code-block. If you edit your post you'll see how I reformatted it. Like this its a lot more readable. Good luck finding an answer. – Software Engineer Feb 06 '21 at 11:48
  • See https://stackoverflow.com/questions/65275097/apache-poi-my-placeholder-is-treated-as-three-different-runs/65289246#65289246 – Axel Richter Feb 06 '21 at 11:53
  • @AxelRichter I use apache poi 3.17 – vince Feb 06 '21 at 16:06
  • And why are you using that old version? Why not upgrading? – Axel Richter Feb 06 '21 at 16:23
  • Just tested. My code works using final version of `apache poi 3.17` too. But in `3.17` `TextSegment` was incorrectly spelled `TextSegement`. So instead of `TextSegment foundTextSegment = null;` in `3.17` it must be `TextSegement foundTextSegment = null;`. – Axel Richter Feb 07 '21 at 16:26

0 Answers0