public static void pdftoText(File file, String outDir) {
PDFParser parser;
String parsedText = null;
PDFTextStripper pdfStripper = null;
PDDocument pdDoc = null;
COSDocument cosDoc = null;
boolean filefound=false;
String outFileName = null;
String result="";

try {
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
if (file.getName().toLowerCase().endsWith(".pdf")) {
filefound = true;

}

if (filefound) {

	outFileName = file.getName() + ".txt";	System.out.println("Processing :" + file.getAbsolutePath());


try {
parser = new PDFParser(new FileInputStream(file));
} catch (IOException e) {
System.err.println("Unable to open PDF Parser. "
+ e.getMessage());
}
try {
parser = new PDFParser(new FileInputStream(file));
parser.parse();
cosDoc = parser.getDocument();
pdfStripper = new PDFTextStripper();
pdDoc = new PDDocument(cosDoc);
pdfStripper.setStartPage(1);
pdfStripper.setEndPage(5);
parsedText = pdfStripper.getText(pdDoc);
} catch (Exception e) {
System.err
.println("An exception occured in parsing the PDF Document."
+ e.getMessage());
} finally {
try {
if (cosDoc != null)
cosDoc.close();
if (pdDoc != null)
pdDoc.close();
} catch (Exception e) {
e.printStackTrace();
}
}

result = parsedText;

File outFile = new File(outDir + "/" + outFileName);
Writer output = new BufferedWriter(new FileWriter(outFile));

output.write(result);
output.close();
} else {
System.out.println("Skipping  :" + file.getAbsolutePath()
+ " Not A Pdf Document");
}

} catch (Exception exep) {
exep.printStackTrace();
System.out.println("Exception  :");
}

}