We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replacing text using regular expressions causes wrapping and formatting issues

I see that this issue had to be addressed in 21.5
PDFNET-49428 Replacing text using regular expressions causes wrapping and formatting issues
However, in 21.7 I have similar problem even literal value is used (check this thread Null Pointer Exception during setText )
PdfMaskResult.pdf (456.4 KB)
problem.PNG (21.9 KB)
Sample_pure.pdf (422.2 KB)


I request you to try setting TextReplaceOptions with following code and share your feedback.

Document document = new Document(dataDir + "Sample_pure.pdf");
String pattern = "[+]?\\d{0,3}[ ]?\\d{10}|[+]?\\d{0,3}[ ]?(?:\\d{3}-){2}\\d{4}|[+]?\\d{0,3}[ ]?(\\d{3})[ ]?\\d{3}-?\\d{4}";
String replace = "322-233-322333335547789";
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(pattern , new TextSearchOptions(true));
TextReplaceOptions options = new TextReplaceOptions();
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
for (TextFragment textFragment : textFragmentCollection)
 catch (Exception e) { //TODO: remove this catch once the library is fixed - there is weird NPE Log.a(PdfDocumentReplace.class).error(e); 
document.save(dataDir + "Sample_pure_replace-options.pdf");

It works with replacement string you provided. But with more real it does not. See attached.
option_NOT_working.PNG (11.8 KB)
option_working.PNG (12.4 KB)


I request you to try using ReplaceAdjustment.AdjustSpaceWidth instead of WholeWordsHyphenation and share your feedback.
option_working_fine.PNG.jpg (97.2 KB)

That works and text does not collide, but consequent match is not replaced. See atached.aspose-second-part.PNG (21.4 KB)


I request you to share your current code for our investigations.