首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java -使用OCR从PDF中提取文本

Java -使用OCR从PDF中提取文本
EN

Stack Overflow用户
提问于 2016-04-16 15:58:29
回答 1查看 10.6K关注 0票数 4

我有一个pdf文件(下面给出了它的一部分),并想从中提取文本。我用过PDFTextStream,但它不适用于这个文件。(然而,它适用于其他具有简单文本的文件)。

还有哪些OCR库能够做到这一点?

请帮帮忙。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-04-16 19:16:28

我尝试使用PDFBox,得到了令人满意的结果。

下面是使用PDFBox从PDF中提取文本的代码:

代码语言:javascript
复制
import java.io.*;

import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.util.*;

public class PDFTest {

 public static void main(String[] args){
 PDDocument pd;
 BufferedWriter wr;
 try {
         File input = new File("C:/BillOCR/data/bill.pdf");  // The PDF file from where you would like to extract
         File output = new File("D:/SampleText.txt"); // The text file where you are going to store the extracted data
         pd = PDDocument.load(input);
         System.out.println(pd.getNumberOfPages());
         System.out.println(pd.isEncrypted());
         pd.save("CopyOfBill.pdf"); // Creates a copy called "CopyOfInvoice.pdf"
         PDFTextStripper stripper = new PDFTextStripper();
         stripper.setStartPage(1); //Start extracting from page 3
         stripper.setEndPage(1); //Extract till page 5
         wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
         stripper.writeText(pd, wr);
         if (pd != null) {
             pd.close();
         }
        // I use close() to flush the stream.
        wr.close();
 } catch (Exception e){
         e.printStackTrace();
        }
     }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36661683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档