Run 0 TextPositon 0: "some text" Run 0 TextPosition 1: "SEARCH" Run 1 TextPosition 0: "ED" calling sarchText("SEARCHED", new PositionInParagraph()) returns TextSegment with beginTextPosition == 0. because in every new run it reinitializes to 0 (XWPFParagraph.searchText source code): for (int runPos=startRun; runPos<rArray.length; runPos++) { int beginTextPos = 0,beginCharPos = 0, textPos = 0, charPos = 0; Similarly for beginCharPos, both should be initialized with beginRunPos above the cycle.
Sorry for the late response on this one, but it would make it easier to reproduce this if you can provide both a sample file and a self-contained unit-test, that would help a lot!
Hi, I created a testcase and a fixed version of the algorithm for this bug: https://github.com/fsomme2s/apache-poi-bug57727 Go to test class "BeginCharTest". Note: In this repo, I also demonstrate and fix a second bug in a second test case. The bug is that the searchText() method returns null (= not found) although the searched string is part of the paragraph. I will create a second bug for this one.
Could you do a pull request in https://github.com/apache/poi with the TODOs in your tests resolved?
Sorry I still have a bug in my "fix"... I work on it, when I'm done with everything I'll create the PR!