Unable to delete page from Aspose pdf Document

I need to delete any page in the aspose pdf document with a particular text tag (<test_attachments>) in it. I tried to use the TextFragmentObserver to find the text in the pages and then delete it.

However the textfragment observer is unable to find the text tag, the returned TextFragmentCollection is always empty.

Also tried using the delete functionality with a specific page number, when I verified the size of the pdf document I do see the pdf file size as 10 but has only 3 pages in the object, which is why I get this error
‘com.aspose.pdf.exceptions.IndexOutOfRangeException’ exception.

Please help me understand if I am missing anything while trying to delete a page from the pdf document

Language : Java 8
Aspose pdf : 22.6
Here is the code

Blockquote

private void deleteTagAndPages(ByteArrayOutputStream pdfOutputStream, SortedSet<Integer> attachmentTagLocations)
		throws Exception {
	com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(pdfOutputStream.toByteArray());
	TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("<test_attachments>");
	pdfDocument.getPages().accept(textFragmentAbsorber);
	TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
	textFragmentAbsorber.getTextSearchOptions();

	for (TextFragment textFragment : textFragmentCollection) {
		log.info("Pdf attachment page to be deleted " + textFragment.getPage().getNumber());
		pdfDocument.getPages().delete(textFragment.getPage().getNumber());
		//pdfDocument.getPages().delete(attachmentTagLocations.first());
	}

}

Blockquote

@ravishat

We suggest you please upgrade to the latest version of Aspose.PDF for Java 22.8 and let us know how it goes on your side.

If you still face problem, please attach the following resources here for testing:

  • Your input PDF document.
  • Please create a simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.