pdfbox 2.0.8 解析pdf获得文本内容


以前用的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;

	}









posted @ 2017-11-17 10:28  妖君你好  阅读(695)  评论(0编辑  收藏  举报