利用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的内容 ...
随机推荐
- 【Usaco2014Open银组】双导航(gpsdual)
题目 [题目描述] FJ 最近网购了一台小车.但是由于他的草率,在选择加装物品时偶然地点击了两次"Submit" ,结果最后他的小车装了两台GPS 导航系统!更糟的是,这两个系统对 ...
- file_put_contents实现内容追加
file_put_contents("test.txt", "This is another something.", FILE_APPEND); FILE_A ...
- JAVA重载和数组
Java 重载:相同的方法名,但参数个数或者类型不一样的情况下,自动执行不同的方法 数组: int[] array=new int[5]; System.out.println(array); ...
- 如何看待yandex开源clickhouse这个列式文档数据库?
如何看待yandex开源clickhouse这个列式文档数据库? 大数据云计算 water 5天前 24℃ 0评论 欧阳辰<Druid实时大数据分析>作者,”互联居”作者编辑推荐1 ...
- Vue2.X 通过 ajax 获取 API 数据(非 axios)
不多废话,笔记如下 1. javascript: let vm = new Vue({ el: '#card-text', data: { info: '' }, beforeCreate: func ...
- 多列表zip合并的csv持久化储存
有时xpath爬取数据之后会返回多个列表,这些列表的长度一样,这时候可以用zip()合并,然后返回一个zip对象,直接传入储存函数,进行持久化储存 例如: name=['张三','李四','王五'] ...
- 移动端 app
上传到蒲公英
- Seeker:一款可获取高精度地理和设备信息的工具分析
Seeker是一款可以获取高精度地理和设备信息的工具.其利用HTML5,Javascript,JQuery和PHP来抓取设备信息,以及Geolocation接口实现对设备高精度地理位置的获取. See ...
- qq游戏IE组件停止工作
你可以下载一个腾讯电脑管家,利用电脑诊所里的腾讯游戏专区里的“网页游 游戏玩不了”这一项修复一下即可.我遇见一次,修复之后就解决了.个人认为是Adobe Flash出问题了.祝你玩的开心.
- SpringBoot布道系列 | 目录汇总 | 2019持续更新ing
SpringBoot 基础教程 | 三大推荐理由 1.文章内容均为原创,结合官方文档和实战经验编写. 2.文章结构经过细致整理,对新人学习更加友好. 3.精选常用技术,不求全面,但求精华!! Spri ...