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 !