利用pdfbox和poi抽取pdf、doc以及docx格式的内容
使用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格式的内容的更多相关文章
- 基于java 合并.doc和docx格式的Word文件
注:摘录自 https://www.cnblogs.com/shenzhouyh/articles/7243805.html 之前用过jacob 合并.doc,但是是有jacob有弊端: 服务器必须是 ...
- 完美解决doc、docx格式word转换为Html
http://blog.csdn.net/renzhehongyi/article/details/48767597
- word文档转pdf,支持.doc和.docx,另附抽取pdf指定页数的方法
公司有个需求,需要将word转成pdf并且抽取首页用以展示,word文档有需要兼容.doc和.docx两种文档格式.其中.docx通过poi直接就可以将word转成pdf,.doc则无法这样实现,上网 ...
- 利用POI抽取word中的图片并保存在文件中
利用POI抽取word中的图片并保存在文件中 poi.apache.org/hwpf/quick-guide.html 1.抽取word doc中的图片 package parse; import j ...
- 使用Lucene对doc、docx、pdf、txt文档进行全文检索功能的实现
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76273859 本文出自[我是干勾鱼的博客] 这里讲一下使用Lucene对doc. ...
- [ASP.NET]利用itextsharp将GridView汇出PDF档
原文 [ASP.NET]利用itextsharp将GridView汇出PDF档 最近在讨论区看到有人说itextsharp可以把网页变成PDF 小弟就去抓一下itextsharp来玩玩,先教大家最实用 ...
- c#抽取pdf文档标题(2)
public class IETitle { public static List<WordInfo> WordsInfo = new List<WordInfo>(); pr ...
- 文件在线预览doc,docx转换pdf(一)
文件在线预览doc,docx转换pdf(一) 1. 前言 文档转换是一个是一块硬骨头,但是也是必不可少的,我们正好做的知识库产品中,也面临着同样的问题,文档转换,精准的全文搜索,知识的转换率,是知识库 ...
- 使用POI转换word doc文件
目录 1 转换为Html文件 2 转换为Xml文件 3 转换为Text文件 在POI中还存在有针对于word doc文件进行格式转换的功能.我们可以将word的内容 ...
随机推荐
- 【pytorch】学习笔记(一)-张量
pytorch入门 什么是pytorch PyTorch 是一个基于 Python 的科学计算包,主要定位两类人群: NumPy 的替代品,可以利用 GPU 的性能进行计算. 深度学习研究平台拥有足够 ...
- AppCan IDE中有时格式化代码后,代码就运行不了了。
AppCan IDE中有时格式化代码后,代码就运行不了了.
- Laravel-admin form 表单是增加或者修改
Laravel-admin 实现 form 表单是增加或者修改的三种方法,应用情景:1.新增或者修改 form 展示的表单不同:2.新增或者保存前后回调进行其他的操作 1. use Illuminat ...
- 前端-CSS-初探-注释-语法结构-引入方式-选择器-选择器优先级-01(待完善)
目录 CSS(Cascading Style Sheet) CSS注释 CSS语法结构 CSS的三种引入方式 选择器 伪类.伪元素选择器速查 CSS选择器优先级***** 选择器相同的情况下 选择器不 ...
- Ruby学习中(哈希变量/python的字典, 简单的类型转换)
一. 哈希变量(相当于Python中的字典) 详情参看:https://www.runoob.com/ruby/ruby-hash.html 1.值得注意的 (1). 创建Hash时需注意 # 创建一 ...
- intelij IDEA设置goole code style风格
1.安装google-java-format 插件 file ->Setings... ->pligins 输入上诉插件安装 2.下载IntelliJ Java Goog ...
- 雷赛DMC2410_入门篇
研究了一下雷赛的运动控制卡,还是花了一点时间,总算把步进电机转起来了,现在把整个过程分享给大家. 雷赛板卡型号很多,这里选择的是DMC2410,主要在于他的性价比,其他型号应该也差不多同样的原理,套装 ...
- 只读字段(readonly)和常量(const)
1.常量 一个包含不能修改的值的变量,通过const关键字定义.只能在声明的同时赋值 2.只读字段 通过readonly关键字定义. 可以在声明的同时赋值. 对于实例字段,在包含字段声明的类的实例构造 ...
- centos查看实时网络带宽占用情况方法【转】
Linux中查看网卡流量工具有iptraf.iftop以及nethogs等,iftop可以用来监控网卡的实时流量(可以指定网段).反向解析IP.显示端口信息等. centos安装iftop的命令如下: ...
- Vi编辑器中查找替换
1 第一个是替换当前行的yesterday为all 第二个是替换全局的 yesterday 为all :s/yesterday/all :s/yesterday/all/g