Previous coordinates on the same line are shifted after replacing text at the end of the line

I try to replace values (one, two, three, four) from end to start, but after “four” replacement I can’t find “three”, “two” and “one” by previous coordinates, because they are changed.

Code example:

Rectangle rectangleFour = new Rectangle(209.09, 706.8000000095367, 227.81383993530272, 718.943999967575);
Rectangle rectangleThree = new Rectangle(179.54, 706.8000000095367, 203.73967991638182, 718.943999967575);
Rectangle rectangleTwo = new Rectangle(156.98, 706.8000000095367, 174.31279994010924, 718.943999967575);
Rectangle rectangleOne = new Rectangle(134.66, 706.8000000095367, 151.79407994079588, 718.943999967575);

var map = new LinkedHashMap<String, Rectangle>();
map.put("four", rectangleFour);
map.put("three", rectangleThree);
map.put("two", rectangleTwo);
map.put("one", rectangleOne);

map.forEach((key, value) -> {
    TextFragmentAbsorber absorber = new TextFragmentAbsorber();
    absorber.setPhrase(key);
    absorber.setTextSearchOptions(new TextSearchOptions(value, false));
    absorber.setTextReplaceOptions(new TextReplaceOptions(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation, TextReplaceOptions.Scope.REPLACE_FIRST));
    pageObj.accept(absorber);


    TextFragmentCollection textFragments = absorber.getTextFragments();
    if (textFragments.size() == 0) {
        System.out.println("Value [" + key + "] not found");
    } else {
        textFragments.get_Item(1).setText("[***]");
    }
}

But if I try to find it without replacement - it works fine:

Rectangle rectangleFour = new Rectangle(209.09, 706.8000000095367, 227.81383993530272, 718.943999967575);
Rectangle rectangleThree = new Rectangle(179.54, 706.8000000095367, 203.73967991638182, 718.943999967575);
Rectangle rectangleTwo = new Rectangle(156.98, 706.8000000095367, 174.31279994010924, 718.943999967575);
Rectangle rectangleOne = new Rectangle(134.66, 706.8000000095367, 151.79407994079588, 718.943999967575);

var map = new LinkedHashMap<String, Rectangle>();
map.put("four", rectangleFour);
map.put("three", rectangleThree);
map.put("two", rectangleTwo);
map.put("one", rectangleOne);

map.forEach((key, value) -> {
    TextFragmentAbsorber absorber = new TextFragmentAbsorber();
    absorber.setPhrase(key);
    absorber.setTextSearchOptions(new TextSearchOptions(value, false));
    absorber.setTextReplaceOptions(new TextReplaceOptions(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation, TextReplaceOptions.Scope.REPLACE_FIRST));
    pageObj.accept(absorber);


    TextFragmentCollection textFragments = absorber.getTextFragments();
    if (textFragments.size() == 0) {
        System.out.println("Value [" + key + "] not found");
    }
}

Test.pdf (37.2 KB)

@pshchasny

We have noticed the similar behavior of the API in our environment as well. We need to investigate the text replacement functionality in asynchronous manner. For the purpose, an investigation ticket as PDFNET-50450 has been logged in our issue tracking system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.