使用pdfbox1.5.0抽取pdf格式文档内容,使用poi3.7抽取doc及docx文档内容:

 /**
* Created by yan.shi on 2017/9/25.
*/
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper; import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; /**
* 这里使用pdfbox解析pdf类型文档
* 使用poi解析doc与docx类型文档
*/
public class ExtractText { public static void main(String[] args) {
ExtractText text=new ExtractText();
String filePath="文件";
String content=text.getText(filePath);
if(null!=content)
System.out.println("content: "+content);
} public ExtractText(){
}
public ExtractText(String filePath){
} /**
* 根据不同的文档类型读取,这里只使用pdf、doc、docs类型
* @param filePath
* @return
*/
public String getText(String filePath){
File file = new File(filePath);
String fileName=file.getName();
String postfix=fileName.substring(fileName.lastIndexOf(".")+1);
String content=null;
if(postfix.equalsIgnoreCase("pdf")){
content=getPDFText(file);
}else if(postfix.equalsIgnoreCase("doc")){
content=getDocText(file);
}else if(postfix.equalsIgnoreCase("docx")){
content=getDocxText(filePath);
}else {
System.out.println("输入的文件格式不支持!");
return null;
}
if(null!=content && !"".equals(content))
return content;
else
return null;
} /**
* 利用pdfbox解析pdf内容
* @param file
* @return
*/
private String getPDFText(File file){
FileInputStream fileinput=null;
String text=null;
try {
fileinput=new FileInputStream(file);
PDFParser parser=new PDFParser(fileinput);//pdf解析器
parser.parse();//解析
PDDocument pdfdocument=parser.getPDDocument();//pdf文档
PDFTextStripper stripper=new PDFTextStripper();//文本剥离
//List allPages=pdfdocument.getDocumentCatalog().getAllPages();
text=stripper.getText(pdfdocument);//从pdf文档剥离文本
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileinput!=null){
try {
fileinput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return text;
} /**
* 读取doc文档类型
* @param file
* @return
*/
private String getDocText(File file){
FileInputStream fileinput=null;
String text=null; try {
fileinput=new FileInputStream(file);
WordExtractor we=new WordExtractor(fileinput);
//text=we.getText();
String s[]=we.getParagraphText();
for(String str:s){
str=str.trim();
if(str.equals("") || str==null)
continue;
//System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileinput!=null){
try {
fileinput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return text;
} /**
* 读取docx文档类型
* @param file
* @return
*/
private String getDocxText(String file){
String text=null;
try {
OPCPackage opcPackage=POIXMLDocument.openPackage(file);
POIXMLTextExtractor extractor=new XWPFWordExtractor(opcPackage);
text=extractor.getText();
//InputStream is=new FileInputStream(file);
//XWPFWordExtractor doc=new XWPFWordExtractor(OPCPackage.open(is));
//List<XWPFParagraph> paras=doc.get
//System.out.println(text);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlException e) {
e.printStackTrace();
} catch (OpenXML4JException e) {
e.printStackTrace();
}
return text;
} }

利用pdfbox和poi抽取pdf、doc以及docx格式的内容的更多相关文章

  1. 基于java 合并.doc和docx格式的Word文件

    注:摘录自 https://www.cnblogs.com/shenzhouyh/articles/7243805.html 之前用过jacob 合并.doc,但是是有jacob有弊端: 服务器必须是 ...

  2. 完美解决doc、docx格式word转换为Html

    http://blog.csdn.net/renzhehongyi/article/details/48767597

  3. word文档转pdf,支持.doc和.docx,另附抽取pdf指定页数的方法

    公司有个需求,需要将word转成pdf并且抽取首页用以展示,word文档有需要兼容.doc和.docx两种文档格式.其中.docx通过poi直接就可以将word转成pdf,.doc则无法这样实现,上网 ...

  4. 利用POI抽取word中的图片并保存在文件中

    利用POI抽取word中的图片并保存在文件中 poi.apache.org/hwpf/quick-guide.html 1.抽取word doc中的图片 package parse; import j ...

  5. 使用Lucene对doc、docx、pdf、txt文档进行全文检索功能的实现

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76273859 本文出自[我是干勾鱼的博客] 这里讲一下使用Lucene对doc. ...

  6. [ASP.NET]利用itextsharp将GridView汇出PDF档

    原文 [ASP.NET]利用itextsharp将GridView汇出PDF档 最近在讨论区看到有人说itextsharp可以把网页变成PDF 小弟就去抓一下itextsharp来玩玩,先教大家最实用 ...

  7. c#抽取pdf文档标题(2)

    public class IETitle { public static List<WordInfo> WordsInfo = new List<WordInfo>(); pr ...

  8. 文件在线预览doc,docx转换pdf(一)

    文件在线预览doc,docx转换pdf(一) 1. 前言 文档转换是一个是一块硬骨头,但是也是必不可少的,我们正好做的知识库产品中,也面临着同样的问题,文档转换,精准的全文搜索,知识的转换率,是知识库 ...

  9. 使用POI转换word doc文件

    目录 1       转换为Html文件 2       转换为Xml文件 3       转换为Text文件 在POI中还存在有针对于word doc文件进行格式转换的功能.我们可以将word的内容 ...

随机推荐

  1. Map以及HashMap

    本文主要介绍java集合框架的Map集合,在日常生活中Map的运用也十分广泛. 与List集合.Set集合隶属于Collection不同,Map是一个独立的接口,与Collection相同级别的接口. ...

  2. PreparedStatement 以及事务的注意事项

    a).PreparedStatement 可以进行批量操作,但是与Statement有一定的区别 1. Statement可以进行不同sql语句的批量操作 即可以同时进行 crud 操作. Strin ...

  3. JAVA基础--JAVA API集合框架(其他集合类,集合原理)

    一.ArrayList介绍 1.ArrayList介绍 ArrayList它是List接口的真正的实现类.也是我们开发中真正需要使用集合容器对象. ArrayList类,它是List接口的实现.肯定拥 ...

  4. AcWing175电路维修

    这是一道在luogu的蓝题,在yxc大佬的讲解下AC掉了(百般调试) 首先这道题给了一个字符串矩阵,/ \表示相连哪两个节点,只可以走/ \所连接的两个点,但我们可以旋转每一个边,询问从1,1 走到 ...

  5. PHP使用CURL抓取页面

    cURL的基本原理 curl是利用URL语法在命令行方式下工作的开源文件传输工具,他能够从互联网上获得各种各样的网络资源.简单来说,curl就是抓取页面的升级版. <?php //1.初始化,创 ...

  6. python-day37(正式学习)

    前景回顾 抢票系统的代码优化,使用了Lock类 from multiprocessing import Process,Lock import os,time,json with open('user ...

  7. Hive 教程(四)-分区表与分桶表

    在 hive 中分区表是很常用的,分桶表可能没那么常用,本文主讲分区表. 概念 分区表 在 hive 中,表是可以分区的,hive 表的每个区其实是对应 hdfs 上的一个文件夹: 可以通过多层文件夹 ...

  8. idea 去除重复代码提醒

  9. Oracle sqlplus prelim 参数介绍

    SQL>conn / as sysdba ORA-00020: maximum number of processes (xxxx) exceeded 报错解决方法 解决 ORA-00020 错 ...

  10. powerDisgner 数据类型对比

    powerDisgner 16.5版本支持一下类型数据模型格式 以下数字数据类型可用: Standard data type DBMS-specific physical data type Cont ...