zoukankan      html  css  js  c++  java
  • 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:WindowsFONTSmstmc.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;
    
    	}









  • 相关阅读:
    电源
    SM2947
    网表
    cadence设计思路
    青山依旧在,几度夕阳红
    乐观锁与悲观锁
    笔记
    强弱软虚引用
    缓存相关
    dubbo
  • 原文地址:https://www.cnblogs.com/fangyuandoit/p/13713909.html
Copyright © 2011-2022 走看看