Tried in two version Java aspose.pdf-21.3 and aspose.pdf-21.7
The problem is the same
java.lang.NullPointerException: null at com.aspose.pdf.internal.ms.System.Text.RegularExpressions.java.lj.lI(Unknown Source) at com.aspose.pdf.internal.l57y.lv.lf(Unknown Source) at com.aspose.pdf.internal.l57y.lv.lI(Unknown Source) at com.aspose.pdf.internal.l57y.lv.lj(Unknown Source) at com.aspose.pdf.internal.l57y.lv.lt(Unknown Source) at com.aspose.pdf.TextFragmentAbsorber.lI(Unknown Source) at com.aspose.pdf.TextFragmentAbsorber.lI(Unknown Source) at com.aspose.pdf.TextFragmentAbsorber$4.lI(Unknown Source) at com.aspose.pdf.TextFragmentAbsorber$4.lI(Unknown Source) at com.aspose.pdf.TextSegment$3$1.lI(Unknown Source) at com.aspose.pdf.TextSegment$3$1.lI(Unknown Source) at com.aspose.pdf.TextSegment.lI(Unknown Source) at com.aspose.pdf.TextSegment.lb(Unknown Source) at com.aspose.pdf.TextFragment.lu(Unknown Source) at com.aspose.pdf.TextFragment.setText(Unknown Source)
The code is:
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(pattern); document.getPages().accept(textFragmentAbsorber); TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments(); for (TextFragment textFragment : textFragmentCollection) { try { textFragment.setText(replace); } catch (Exception e) { //TODO: remove this catch once the library is fixed - there is weird NPE Log.a(PdfDocumentReplace.class).error(e); } textFragment.getTextState().setBackgroundColor(Color.getYellow()); }
Input patterns and replacement values:
([\w-+]+(?>.[\w]+)@[\w-]+(.[\w]+)(?>.[a-z]{2,}))
anonymous@great.com
[0-9]{3}[-]?[0-9]{2}[-]?[0-9]{4}
111-99-8765
[+]?\d{0,3}[ ]?\d{10}|[+]?\d{0,3}[ ]?(?:\d{3}-){2}\d{4}|[+]?\d{0,3}[ ]?(\d{3})[ ]?\d{3}-?\d{4}
322-233-3223
As you can see, for the last pattern there are two occurrences. The first one is replaced without any problem, but the second one fails to set text, however does not fail to change background color.Input.PNG (18.1 KB)
Output.PNG (13.6 KB)
PdfMaskResult_pure.pdf (451.5 KB)
Sample_pure.pdf (422.2 KB)