I am evaluating Aspose.PDF's Java API to redact PII data in my PDF files and replace them with obfuscated text. My PDF contains some data as follows:
- B, KATILDE
- A, RICHARD
- S, NARTURO
The order of appearance of these text values is same as in the PDF.
Here's my sample code:
private void replaceTextInDocument(String sourceDoc, String destDoc, String textToReplace, String replacementText) {
Document pdfDocument = new Document(sourceDoc);
System.out.println("replaceTextInDocument processing: " + textToReplace);
// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(textToReplace);
pdfDocument.getPages().accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
// Loop through the fragments
for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentCollection) {
// Update text and other properties
textFragment.setText(replacementText);
textFragment.getTextState().setFont(FontRepository.findFont("Verdana"));
textFragment.getTextState().setFontSize(8);
textFragment.getTextState().setForegroundColor(Color.getBlack());
textFragment.getTextState().setBackgroundColor(Color.getWhite());
}
// Save the updated PDF file
pdfDocument.save(destDoc);
pdfDocument.close();
}
Now interestingly, this code is able to replace to 'B, KATILDE' with replacementText. However, when I try to replace other two values (A, RICHARD or S, NARTURO), I get the following exception:
Exception in thread "main" com.aspose.pdf.internal.ms.System.l7k: **Collection is of a fixed size**
at com.aspose.pdf.internal.ms.System.ly$lb.removeAt(Unknown Source)
at com.aspose.pdf.ADocument.lI(Unknown Source)
at com.aspose.pdf.CharInfoCollection.copyTo(Unknown Source)
at com.aspose.pdf.l19y.lI(Unknown Source)
at com.aspose.pdf.l19y.lf(Unknown Source)
at com.aspose.pdf.TextFragment.lf(Unknown Source)
at com.aspose.pdf.TextFragment.setText(Unknown Source)
I am able to replace RICHARD or NARTURO successfully.
I am using v23.7 of the Aspose Library. Snippet from build.gradle:
implementation group: 'com.aspose', name: 'aspose-pdf', version: '23.7'
It's very difficult to debug the Aspose code using IDE. So, I am unable to get more information about this issue.
Any help or pointers to resolve this issue would be helpful.
Thanks