您的位置:首页 > 其它

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pdfbox 2.0.8