0

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

Manchanda. P
  • 221
  • 1
  • 4
  • 19
  • We request you please create a topic in Aspose.PDF official support forum along with you sample file. Please note that our support forum is the right and the recommended place to post such issues. This way we will be able to carry out proper investigation with provided files and you will also be able to track the progress appropriately. Please create a topic in the Aspose support forum (https://forum.aspose.com/c/pdf) and we will further proceed to assist you accordingly. This is Asad and I am Developer Evangelist at Aspose. – Asad Ali Jul 31 '23 at 21:32
  • Thanks @Asad Ali for quick reply. The question has been posted to Aspose Support Forum: https://forum.aspose.com/t/aspose-pdf-java-getting-com-aspose-pdf-internal-ms-system-l7k-collection-is-of-a-fixed-size-exception-while-replacing-text-in-a-pdf-document/269177 – Manchanda. P Aug 01 '23 at 06:57
  • Yes, we have noticed it and have shared our feedback there as well. – Asad Ali Aug 02 '23 at 21:05

0 Answers0