pdfbox 2.0.8 解析pdf获得文本内容
2017-11-17 10:27
6100 查看
以前用的pdfbox 获得pdf对象都是一个fileInputStream搞定的。
升级到2.0.8版本后不能用了 。 由于才更新一两个月,网上也没有实例代码。就自己看了下 做个记录
也就是把原来的流变成pdfbox里面的RandomAccessRead 随机读写流就可以了
/**
* @param pdfFilePath
* pdf文件的全路径
* @return
* @throws Exception
*
* SEVERE: Could not load font file: C:\Windows\FONTS\mstmc.ttf
* 可能报这样的警告信息。倒是内容能够正确读到
*/
public static String getTextFromPDF(String pdfFilePath) throws Exception {
RandomAccessRead accessRead = new RandomAccessFile(new File(
"C:\\Users\\TOSHIBA\\Desktop\\Helloworld.pdf"), "rw");
PDFParser parser = new PDFParser(accessRead); // 创建PDF解析器
parser.parse(); // 执行PDF解析过程
PDDocument pdfdocument = parser.getPDDocument(); // 获取解析器的PDF文档对象
PDFTextStripper pdfstripper = new PDFTextStripper(); // 生成PDF文档内容剥离器
String contenttxt = pdfstripper.getText(pdfdocument); // 利用剥离器获取文档
System.out.println(contenttxt);
accessRead.close();
pdfdocument.close();
return contenttxt;
}
升级到2.0.8版本后不能用了 。 由于才更新一两个月,网上也没有实例代码。就自己看了下 做个记录
也就是把原来的流变成pdfbox里面的RandomAccessRead 随机读写流就可以了
/**
* @param pdfFilePath
* pdf文件的全路径
* @return
* @throws Exception
*
* SEVERE: Could not load font file: C:\Windows\FONTS\mstmc.ttf
* 可能报这样的警告信息。倒是内容能够正确读到
*/
public static String getTextFromPDF(String pdfFilePath) throws Exception {
RandomAccessRead accessRead = new RandomAccessFile(new File(
"C:\\Users\\TOSHIBA\\Desktop\\Helloworld.pdf"), "rw");
PDFParser parser = new PDFParser(accessRead); // 创建PDF解析器
parser.parse(); // 执行PDF解析过程
PDDocument pdfdocument = parser.getPDDocument(); // 获取解析器的PDF文档对象
PDFTextStripper pdfstripper = new PDFTextStripper(); // 生成PDF文档内容剥离器
String contenttxt = pdfstripper.getText(pdfdocument); // 利用剥离器获取文档
System.out.println(contenttxt);
accessRead.close();
pdfdocument.close();
return contenttxt;
}
相关文章推荐
- 利用lucene和pdfBox对PDF文本进行内容的解析
- 利用PDFBox提取pdf文件文本内容的分析
- Apache PDFbox开发指南之PDF文本内容挖掘
- 使用pdfbox解析pdf文档信息(属性,内容,图片)
- PDFBox解析PDF文档信息(属性、内容、图片)
- Apache PDFbox开发指南之PDF文本内容挖掘
- Apache PDFbox开发指南之PDF文本内容挖掘
- 提取文档中的文本内容(使用PDFBox处理PDF文档)
- java-pdfbox2.0.8读取pdf文本和图片
- 使用PDFBox解析PDF内容
- 将网页上word、pdf、txt文件下载下来,解析成文本内容
- 解析pdf,获得内容
- 使用PDFBox解析PDF内容
- 使用pdfbox解析pdf文档信息(属性,内容,图片)
- lucene.net,包括对htm,word,doc,pdf,等等式进行parse解析,取得内容
- java解析json文本,去除//后和/**/中的内容
- JAVA中如何获得word,excel,pdf等文档的内容
- pdfbox 解析 PDF转HTML、Text文件
- android中使用httpclient方法获得网页内容并对json对象解析
- 使用pdfbox实现pdf文本提取和合并功能示例