JAVA读取PDF、WORD文档实例代码
读取PDF文件jar引用
org.apache.pdfbox pdfbox1.8.13
读取WORD文件jar引用
org.apache.poi poi-scratchpad3.16-beta1 org.apache.poi poi3.16-beta1
读取WORD文件方法
/**
*
*@Title:getTextFromWord
*@Description:读取word
*@paramfilePath
*文件路径
*@return:String读出的Word的内容
*/
publicstaticStringgetTextFromWord(StringfilePath){
Stringresult=null;
Filefile=newFile(filePath);
FileInputStreamfis=null;
try{
fis=newFileInputStream(file);
@SuppressWarnings("resource")
WordExtractorwordExtractor=newWordExtractor(fis);
result=wordExtractor.getText();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(fis!=null){
try{
fis.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
returnresult;
}
读取PDF文件方法
/**
*
*@Title:getTextFromPdf
*@Description:读取pdf文件内容
*@paramfilePath
*@return:读出的pdf的内容
*/
publicstaticStringgetTextFromPdf(StringfilePath){
Stringresult=null;
FileInputStreamis=null;
PDDocumentdocument=null;
try{
is=newFileInputStream(filePath);
PDFParserparser=newPDFParser(is);
parser.parse();
document=parser.getPDDocument();
PDFTextStripperstripper=newPDFTextStripper();
result=stripper.getText(document);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(is!=null){
try{
is.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(document!=null){
try{
document.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
returnresult;
}
希望本篇实例代码可以帮到您
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志