I’m guessing this is a problem from being a port from .net, but the iterator returned by TextFragmentCollection does not work like it is supposed to. hasNext consumes the current item and next will show you the same item over and over. I’m guessing these are thin wrappers around current and moveNext in IEnumerator but they break the Iterable interface. Calling next multiple times will give you the same element over and over. Calling hasNext multiple times will skip over items without ever showing them.
Hello Will,
public static void replaceTextOnAllPages(String src, String dest) {
// Open document
Document pdfDocument = new Document(src);
// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Will");
// Accept the absorber for first page of document
pdfDocument.getPages().accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
Iterator<TextFragment> iter = textFragmentCollection.iterator(); //assume textFragmentCollection has 2 results
iter.next(); //will give you first TextFragment
iter.next(); //will give you first textFragment
iter.hasNext(); //will be true
iter.hasNext(); //will be false
// Save the updated PDF file
pdfDocument.save(dest);
}
Hello Will,
String dataDir = “/Users/fahadadeel/Downloads/resources/”;
Document pdfDocument = new Document(dataDir + “pdf-sample.pdf”);
// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(“PDF”);
// Accept the absorber for first page of document
pdfDocument.getPages().accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
Iterator iter = textFragmentCollection.iterator(); //assume textFragmentCollection has 2 results
while(iter.hasNext()){
System.out.println(( iter.next()).getText());
}
// Save the updated PDF file
pdfDocument.save("/Users/fahadadeel/Downloads/resources/outputfile.pdf");
I agree that that code works, what if I want to check if the iterator is empty before looping through it, something like
Hello Will,
I have tested the scenario and have managed to reproduce the problem that TextFragment Collection breaks Iterable interface. For the sake of correction, I have logged it as PDFJAVA-36468 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction.
Please be patient and spare us little time. We are sorry for this inconvenience.
Best Regards,
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Arial; -webkit-text-stroke: #000000} span.s1 {font-kerning: none}Great, am I able to track that ticket somewhere?
Hello Will,
The issues you have found earlier (filed as PDFJAVA-36468) have been fixed in Aspose.Pdf for Java 17.3.0 Release Notes.
This message was posted using Notification2Forum from Downloads module by Aspose Notifier.