利用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的内容 ...
随机推荐
- mybatis整体流程
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...
- # ACM奇淫技巧
目录 ACM奇淫技巧 差分操作 坐标旋转 ACM 卡常优化 vsc代码块(头文件模板) 读入输出优化 逗号表达式 内联函数inline 寄存器变量register 条件判断加减代替取模 自增运算符优化 ...
- Robot Framework(一)安装笔记
参考网址:https://www.cnblogs.com/yinrw/p/5837828.html因为自己安装了py,网上教程都是统一安装py2.7开始的. 所以这里总结下安装笔记:cmd命令界面进行 ...
- WPF ListView多行显示
//前台 <ListView Margin="14,152,12,74" Name="lvList" SelectionMode="Multip ...
- O021、创建 Image
参考https://www.cnblogs.com/CloudMan6/p/5393376.html 本节演示如何通过 Web GUI 和 CLI 两种方法创建image. OpenStack ...
- python多线程、多进程、协程笔记
import threading import time import multiprocessing import asyncio movie_list = ['斗破.avi', '复仇者联盟.mp ...
- 关于rpm包的安装卸载等
在Linux操作系统中,有一个系统软件包,它的功能类似于Windows里面的“添加/删除程序”,但是功能又比“添加/删除程序”强很多,它就是Red Hat Package Manager(简称RPM) ...
- signal,blinker:信号(看我脸色行事)
signal 什么是信号(signal)? 信号在linux中被用来进行进程间的通信和异步处理,简单地可以理解会为回调函数,当发送一个信号时,会触发相应的操作.python中的signal模块便是用来 ...
- 2019.9.27PHP基础
PHP 基础语法规范: 1 <?php 开头 ?>结尾 2 php可以单独存在也可以和html等结合使用 3后缀名一般以.php结尾 php4,php5,php6,php7,phtml. ...
- Open cup #2
A D:用前面的H去消去后面的K 然后求最长连续的M F:在每一列/行里面求最大的数然后组成最大的和ans[]里的比求出最大的 L:并查集 J:DP背锅题 01背包 先求出M种里每种的size和las ...