Java识别PDF文字内容的方法

方法一:PDFBox

(一个BSD许可下的源码开放项目)是一个为开发人员读取和创建PDF文档而准备的纯Java类库。它提供如下特性:

提取文本,包括Unicode字符。和Jakarta Lucene等文本搜索引擎的整合过程十分简单。加密/解密PDF文档。从PDF和XFDF格式中导入或导出表单数据。向已有PDF文档中追加内容。将一个PDF文档切分为多个文档,覆盖PDF文档。

官网:http://pdfbox.apache.org/index.html    截止当前最新版本1.8.8
 

/**

 * PdfboxUtil.java

 * Create on 2015-1-5

 */

package charlie.utils.pdf;

 

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.InputStream;

 

import org.apache.pdfbox.pdfparser.PDFParser;

import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.util.PDFTextStripper;

 

/**

 * @author CharlieChen

 * @DateTime 2015-1-5 上午9:55:38

 * @version 1.0

 */

public class PdfboxUtil {

 

   /**

    * @param args

    */

   public static void main(String[] args) {

      String pdfPath = "D:/temp/成交单-PDF格式.pdf";

      String txtfilePath = "D:/temp/成交单-PDF格式-pdfbox.txt";

      PdfboxUtil pdfutil = new PdfboxUtil();

      try {

         String content = pdfutil.getTextFromPdf(pdfPath);

         pdfutil.toTextFile(content, txtfilePath);

         System.out.println("Finished !");      

      } catch (Exception e) {

         e.printStackTrace();

      }

 

   }

  

   /**

    * 读取PDF文件的文字内容

    * @param pdfPath

    * @throws Exception

    */

   public String getTextFromPdf(String pdfPath) throws Exception {

      // 是否排序

      boolean sort = false;

      // 开始提取页数

      int startPage = 1;

      // 结束提取页数

      int endPage = Integer.MAX_VALUE;    

     

      String content = null;

      InputStream input = null;

      File pdfFile = new File(pdfPath);

      PDDocument document = null;

      try {

         input = new FileInputStream(pdfFile);

         // 加载 pdf 文档

         PDFParser parser = new PDFParser(input);

         parser.parse();

         document = parser.getPDDocument();

         // 获取内容信息

         PDFTextStripper pts = new PDFTextStripper();

         pts.setSortByPosition(sort);

         endPage = document.getNumberOfPages();

         System.out.println("Total Page: " + endPage);

         pts.setStartPage(startPage);

         pts.setEndPage(endPage);

         try {

            content = pts.getText(document);

         } catch (Exception e) {

            throw e;

         }

         System.out.println("Get PDF Content ...");

      } catch (Exception e) {

         throw e;

      } finally {

         if (null != input)

            input.close();

         if (null != document)

            document.close();

      }

     

      return content;

   }

 

   /**

    * 把PDF文件内容写入到txt文件中

    * @param pdfContent

    * @param filePath

    */

   public void toTextFile(String pdfContent,String filePath) {     

      try {

         File f = new File(filePath);

         if (!f.exists()) {

            f.createNewFile();

         }

         System.out.println("Write PDF Content to txt file ...");

         BufferedWriter output = new BufferedWriter(new FileWriter(f));

         output.write(pdfContent);

         output.close();

      } catch (Exception e) {

         e.printStackTrace();

      }

   }

}

https://blog.csdn.net/charliechen1989/article/details/42425617

另需要commons-logging和fontbox-1.8.8的jar包

 

pdfbox只能做到把文字分析出来,并无法很好的控制分析的顺序,格式,字体等信息。

排序sort为true后,PDF按行读取,保持了顺序,但是遇到分栏,分页就会需要额外处理

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值