Hi,
I want to remove content between two text lines. Please see attached file. I want to search “this is heading of level 1” and “this is bullet style 1”. Then I want to remove whatever content in between these two text, i.e. text, image etc.
Please let me know workaround using Java aspose.pdf or Java aspose.pdf.kit as soon as possible.
Thanks.
-Sonali
Hi Sonali,
Hi Sonali,
Thanks for your patience.
We are pleased to share that the feature requested earlier to search text strings and remove the contents between them. The feature will become available in the upcoming hotfix of Aspose.Pdf for Java 4.2.1. Please try using the following code snippet to accomplish your requirement.
[Java]
String path = "c:\\pdftest\\";
/// open document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document(path+"testHeading (2).pdf");
//create TextAbsorber object to find
//all instances of the input search phrase
String from="this is heading of level 1";
String till="this is bullet style 1";
com.aspose.pdf.TextFragmentAbsorber textFragmentAbsorber = new com.aspose.pdf.TextFragmentAbsorber(from+".*"+till,new com.aspose.pdf.TextSearchOptions(true));
///accept the absorber for first page of document
pdfDocument.getPages().accept(textFragmentAbsorber);
///get the extracted text fragments into collection
com.aspose.pdf.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
///loop through the Text fragments
for(com.aspose.pdf.TextFragment textFragment :
(Iterable<com.aspose.pdf.TextFragment>)textFragmentCollection)
{
// It is enough to remove all segments between
// the first and the last if they are separate segments.
int size = textFragment.getSegments().size();
size++;
//after each deleting size is decremented by 1
while(textFragment.getSegments().size()>2)
{
textFragment.getSegments().delete(2);//remove the second fragment and recalculates the remaining
fragments
}
}
pdfDocument.save(path+"testHeading_out.pdf");
The issues you have found earlier (filed as PDFNEWJAVA-33689) have been fixed in Aspose.Pdf for Java 4.2.1.
This message was posted using Notification2Forum from Downloads module by Aspose Notifier.
Hi ,
Thanks a lot.
-Sonali
Hi Sonali,
Hello, I reached this post from searching for a way to search a PDF for a certain string of text. May I know which part of the code above that is? Could you give me a sample code if I were to search for the string “OR Number” in a PDF?
Hi Junmil,