JAVA讀取PDF、WORD文檔實例代碼
更新時間:2017年04月20日 16:58:38 投稿:wbb
本篇文章主要通過實例代碼介紹了JAVA讀取PDF、WORD文檔,需要的朋友可以參考下
讀取PDF文件jar引用
<dependency> <groupid>org.apache.pdfbox</groupid> pdfbox</artifactid> <version>1.8.13</version> </dependency>
讀取WORD文件jar引用
<dependency> <groupid>org.apache.poi</groupid> poi-scratchpad</artifactid> <version>3.16-beta1</version> </dependency> <dependency> <groupid>org.apache.poi</groupid> poi</artifactid> <version>3.16-beta1</version> </dependency>
讀取WORD文件方法
/**
*
* @Title: getTextFromWord
* @Description: 讀取word
* @param filePath
* 文件路徑
* @return: String 讀出的Word的內(nèi)容
*/
public static String getTextFromWord(String filePath) {
String result = null;
File file = new File(filePath);
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
@SuppressWarnings("resource")
WordExtractor wordExtractor = new WordExtractor(fis);
result = wordExtractor.getText();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
讀取PDF文件方法
/**
*
* @Title: getTextFromPdf
* @Description: 讀取pdf文件內(nèi)容
* @param filePath
* @return: 讀出的pdf的內(nèi)容
*/
public static String getTextFromPdf(String filePath) {
String result = null;
FileInputStream is = null;
PDDocument document = null;
try {
is = new FileInputStream(filePath);
PDFParser parser = new PDFParser(is);
parser.parse();
document = parser.getPDDocument();
PDFTextStripper stripper = new PDFTextStripper();
result = stripper.getText(document);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (document != null) {
try {
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
希望本篇實例代碼可以幫到您
相關(guān)文章
SpringBoot集成shiro,MyRealm中無法@Autowired注入Service的問題
今天小編就為大家分享一篇關(guān)于SpringBoot集成shiro,MyRealm中無法@Autowired注入Service的問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
詳解直接插入排序算法與相關(guān)的Java版代碼實現(xiàn)
這篇文章主要介紹了直接插入排序算法與相關(guān)的Java版代碼實現(xiàn),需要的朋友可以參考下2016-05-05

