The wrote the code below is based on samples for the Aspose.Pdf.Kit samples. I have a temporary license, however, the search doesn’t work correctly. When I run a search for “of” three times on the text.pdf it works the first time, but fails the other two. If I change “of” to anything else, it won’t run.
If I switch out text.pdf with 13pages.pdf (both are example pdfs) it doesn’t work at all, with any search strings.
I noticed a know issues for the latest version of pdfSearcher is “PdfSearcher does not work in some cases.”
Could you please clarify with any information?
License lic = new License();
try
{
lic.setLicense(new FileInputStream(new File(“c:\publishing\Aspose.Pdf.Kit.lic”)));
}
catch (Exception e) {
System.out.println(e.getMessage());
}
String wordsToTry[] = { “of”, “of”, “of” };
String filesToTry[] = {“C:\Documents and Settings\dmadmin\My Documents\Aspose.Pdf.Kit\examples\resources\text.pdf”,
“C:\Documents and Settings\dmadmin\My Documents\Aspose.Pdf.Kit\examples\resources\13page.pdf”,
};
int successes = 0;
int failures = 0;
boolean failed = false;
for( int documentIterator = 0; documentIterator < filesToTry.length; documentIterator++ ){
for( int wordIterator = 0; wordIterator < wordsToTry.length; wordIterator++ ){
failed = false;
PdfSearcher pdfSearcher = new PdfSearcher();
pdfSearcher.bindPdf(filesToTry[documentIterator]);
java.awt.Rectangle rec = new java.awt.Rectangle(0, 0, 1000, 1000);
pdfSearcher.setStartPage(1);
pdfSearcher.setEndPage(2);
System.out.println("Trying file " + filesToTry[documentIterator] + " for word: " + wordsToTry[wordIterator] );
try{
pdfSearcher.searchTextInRectangle(rec, wordsToTry[wordIterator], false);
}
catch (Exception e) {
//System.out.println( e.getMessage() );
failed = true;
}
if( failed ){
System.out.println(“Failed”);
failures++;
}
else{
System.out.println(“Success”);
successes++;
}
}
}
System.out.println( "Success: " + successes + " Failures: " + failures );
Output
Trying file C:\Documents and Settings\dmadmin\My Documents\Aspose.Pdf.Kit\examples\resources\text.pdf for word: of
Success
Trying file C:\Documents and Settings\dmadmin\My Documents\Aspose.Pdf.Kit\examples\resources\text.pdf for word: test
null
Failed
Trying file C:\Documents and Settings\dmadmin\My Documents\Aspose.Pdf.Kit\examples\resources\text.pdf for word: foo
null
Failed
Trying file C:\Documents and Settings\dmadmin\My Documents\Aspose.Pdf.Kit\examples\resources\13page.pdf for word: of
null
Failed
Trying file C:\Documents and Settings\dmadmin\My Documents\Aspose.Pdf.Kit\examples\resources\13page.pdf for word: test
null
Failed
Trying file C:\Documents and Settings\dmadmin\My Documents\Aspose.Pdf.Kit\examples\resources\13page.pdf for word: foo
null
Failed
Success: 1 Failures: 5
####
```