以编程方式从PDF文件中的扫描识别文本

时间:2020-03-06 14:59:05  来源:igfitidea点击:

我有一个PDF文件,其中包含我们需要导入数据库中的数据。这些文件似乎是打印的字母数字文本的pdf扫描。看起来像10点。英语字体格式一种。

是否有任何工具或者组件可以让我识别和解析此文本?

解决方案

谷歌快速搜索显示了这个有希望的结果。
http://www.pdftron.com/net/index.html

我们可以使用perl的PDF之类的模块来提取文本。并使用另一种工具将相关信息导入数据库。

我确定有.NET的PDF组件,但是我还没有尝试过,所以我不知道有什么好处。

在我曾经工作过的一家公司,我们使用ActivePDF工具包取得了一些成功:

http://www.activepdf.com/products/serverproducts/toolkit/index.cfm

我认为我们至少需要Standard或者Pro版本,但是它们已经过试用,因此我们可以查看它是否可以满足要求。

我最近找到了适用于Python的ReportLab。

如果PDF是对打印文本的扫描,则将很难自己进行(涉及图像处理,字符识别等)。 PDF通常会在内部将扫描的文档存储为JPEG。最好使用第三方工具(OCR工具)来执行此操作。

我们无法从PDF中提取扫描的文本。我们需要OCR软件。好消息是,我们可以尝试使用一些开源应用程序,并且OCR路由最有可能比使用PDF库提取文本更容易。查看Tesseract和GOCR。

我已经使用pdftohtml成功地将表格从PDF剥离为CSV。它基于Xpdf,后者是一种更通用的工具,其中包括pdftotext。我只是将其包装为C#中的Process.Start调用。

如果我们正在寻找更多DIY的东西,那么iTextSharp库是Java的iText和PDFBox的端口(是的,它说Java,但它们通过IKVM.NET具有.NET版本)。这是有关使用C#中的iTextSharp和PDFBox的一些CodeProject文章。

而且,如果我们确实是受虐狂,则可以通过COM互操作调用Adobe的PDF IFilter。 IFilter规格非常简单,但我猜想互操作开销会很大。

编辑:重新阅读问题和后续答案后,很明显OP正在处理其PDF中的图像。在这种情况下,我们需要提取图像(上面的PDF库能够很容易地做到这一点)并通过OCR引擎运行它。

我以前以交互方式使用过MODI,但效果不错。它是COM,因此从Cvia interop调用它也是可行且非常简单的:

' lifted from http://en.wikipedia.org/wiki/Microsoft_Office_Document_Imaging
Dim inputFile As String = "C:\test\multipage.tif"
Dim strRecText As String = ""
Dim Doc1 As MODI.Document

Doc1 = New MODI.Document
Doc1.Create(inputFile)
Doc1.OCR()  ' this will ocr all pages of a multi-page tiff file
Doc1.Save() ' this will save the deskewed reoriented images, and the OCR text, back to the inputFile

For imageCounter As Integer = 0 To (Doc1.Images.Count - 1) ' work your way through each page of results
   strRecText &= Doc1.Images(imageCounter).Layout.Text    ' this puts the ocr results into a string
Next

File.AppendAllText("C:\test\testmodi.txt", strRecText)     ' write the OCR file out to disk

Doc1.Close() ' clean up
Doc1 = Nothing

其他人也喜欢Tesseract,但我对此有直接的经验。我听说过它的好处和坏处,所以我想它很大程度上取决于信号源质量。

如果我做对,sheebz正在询问如何提取PDF字段并将数据加载到数据库中。我们看过iTextSharp吗? http://sourceforge.net/projects/itextsharp/

我已经在我的一个博客中发布了有关解析pdf的信息。点击此链接:

http://devpinoy.org/blogs/marl/archive/2008/03/04/pdf-to-text-using-open-source-library-pdfbox-another-sample-for-grade-1-pupils.aspx

编辑:链接不再有效。以下引用自http://web.archive.org/web/20130507084207/http://devpinoy.org/blogs/marl/archive/2008/03/04/pdf-to-text-using-open-source-library -pdfbox-另一个用于1年级学生的样本.aspx

Well, the following is based on popular examples available on the web.
  What this does is "read" the pdf file and output it as a text in the
  rich text box control in the form. The PDFBox for .NET library can be
  downloaded from sourceforge.
  
  You need to add reference to IKVM.GNU.Classpath & PDFBox-0.7.3. And
  also, FontBox-0.1.0-dev.dll and PDFBox-0.7.3.dll need to be added on
  the bin folder of your application. For some reason I can't recall
  (maybe it's from one of the tutorials), I also added to the bin
  IKVM.GNU.Classpath.dll. 
  
  On the side note, just got my copy of "Head First C#" (on Keith's
  suggestion) from Amazon. The book is cool! It is really written for
  beginners. This edition covers VS2008 and the framework 3.5.
  
  Here you go...
/* Marlon Ribunal
 * Convert PDF To Text
 * *******************/

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
using System.Text;
using System.ComponentModel.Design;
using System.ComponentModel;
using org.pdfbox.pdmodel;
using org.pdfbox.util;

namespace MarlonRibunal.iPdfToText
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent(); 
        }

        void Button1Click(object sender, EventArgs e)    
        {    
            PDDocument doc = PDDocument.load("C:\pdftoText\myPdfTest.pdf");
            PDFTextStripper stripper = new PDFTextStripper();
            richTextBox1.Text=(stripper.getText(doc));
        }

     }
}