public void readPDF(String fileName) {
File file = new File(fileName);
FileInputStream in = null;
try {
in = new FileInputStream(fileName);
//新建一個PDF解析器物件
PDFParser parser = new PDFParser(in);
//對PDF檔案進行解析
parser.parse();
//獲取解析後得到的PDF文件物件
PDDocument pdfdocument = parser.getPDDocument();
//新建一個PDF文字剝離器
PDFTextStripper stripper = new PDFTextStripper();
//從PDF文件物件中剝離文字
String result = stripper.getText(pdfdocument);
System.out.println("PDF檔案" + file.getAbsolutePath() + "的文字內容如下:");
System.out.println(result);
} catch (Exception e) {
System.out.println("讀取PDF檔案"+ file.getAbsolutePath() + "生失敗!" + e);
e.printStackTrace();
} finally {
if (in != null){
in.close();
} catch (IOException e1) {
}
public void readPDF(String fileName) {
File file = new File(fileName);
FileInputStream in = null;
try {
in = new FileInputStream(fileName);
//新建一個PDF解析器物件
PDFParser parser = new PDFParser(in);
//對PDF檔案進行解析
parser.parse();
//獲取解析後得到的PDF文件物件
PDDocument pdfdocument = parser.getPDDocument();
//新建一個PDF文字剝離器
PDFTextStripper stripper = new PDFTextStripper();
//從PDF文件物件中剝離文字
String result = stripper.getText(pdfdocument);
System.out.println("PDF檔案" + file.getAbsolutePath() + "的文字內容如下:");
System.out.println(result);
} catch (Exception e) {
System.out.println("讀取PDF檔案"+ file.getAbsolutePath() + "生失敗!" + e);
e.printStackTrace();
} finally {
if (in != null){
try {
in.close();
} catch (IOException e1) {
}
}
}