Find First occurrence of any text and stop

Hi !
I have been working on searching the documents based on any query/string.
I have been able to do it and get the counts.
But, what I need is, I don’t need any counts, I just want to check if the searched query exists in the document. If it does (no matter once, or N times). I just want it stop there and search for further docs in the directory.
Following is the code that I have used. Can you please help me optimize it as per my requirement, so that I could get a better performance, in terms of speed. Here goes the code :

public static void main(String[]args) throws Exception {
com.aspose.pdf.License license = new com.aspose.pdf.License();
license.setLicense(“Aspose.Total.Java-License.lic”);
int count =0;
File[] files = new File(“D:\docs”).listFiles();

		for (File file : files) {
		    if (file.isFile()) {
		        String folderName = file.getParent();
		        String fileName =  file.getName();
		        String extensionName = fileName.substring(fileName.lastIndexOf("."));
		        if (extensionName.equals(".pdf")) {
		            //System.out.println("Processing document: " + fileName);
		        	Document pdfDocument = new Document(file.getAbsolutePath());
		        	TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("(?i)sample"); // like 1999-2000
		        	TextSearchOptions textSearchOptions = new TextSearchOptions(true);
					textFragmentAbsorber.setTextSearchOptions(textSearchOptions);
					pdfDocument.getPages().accept(textFragmentAbsorber);
					TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
					for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentCollection) {
						if(textFragment.getText() != "") {
						count++;
						}
					}
		    		if(count > 0) {
		    			//System.out.println("E:\\"+file.getName());
		    			System.out.println("E:\\"+file.getName()+" || Count="+count);
		    		}
		    		count=0;
		        }
	
		    } 
		}
	}

Please suggest the code changes. Thanks ! :slight_smile:

@Kushal.20

Instead of iterating through textFragmentCollection and comparing text with empty string, you can simply check the size of the collection and proceed if it is greater than zero:

TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
if(textFragmentCollection.size() > 0){
            
}

Furthermore, you can search text at page level instead of searching it on whole document. So in the cases where targeted text is present over first page, your code will be executed more faster.

for(Page page : doc.getPages()){
 TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("");(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
 page.accept(textFragmentAbsorber);
}

In case you face any issue, please share some issue details along with sample PDF document. We will further proceed to assist you accordingly.