Insert an XSLFTetxRun in a specified position using Apache POI

202 views Asked by At

I am currently working on a Document Management System using Apache POI in which I have to replace certain placeholders from XSLFParagraph text. Previously using XWPFParagraph, I was able to insert an XWPFRun on a specified position e.g.,

XWPFRun beforePlaceholderRun = paragraph.insertNewRun(start);

The above code insert run in a paragraph on a specified position i.e., start. I couldn't find any functions that inserts an XSLFTextRun in a specified position of XSLFTextParagraph. E.g., the below code insert an XSLFTextRun in the end of the paragraph

XSLFTextRun placeholderRun = paragraph.addNewTextRun();

But what I want to achieve is to insert an XSLFTextRun on a specified position of an XSLFParagraph when the XSLFParagraph is comprised of multiple XSLFTextRun. Can anybody please guide me how can I achieve this?

1

There are 1 answers

0
Zuhlek On

I can't provide you with an exact solution for your problem, but some hints might help you out (I had a pretty similar issue).

The question is what kind of element you are showing in your screenshot. If you say it is neither a paragraph nor a table, then it might be an embedded file? In the documentation there is a method to fetch those:

getAllEmbeddedParts()

Also, check out this repo which I found when I was solving a quite similar problem (maybe you find something helpful for your case as well):

https://github.com/deividasstr/docx-word-replacer