【问题标题】:Programmatically recognize text from scans in a PDF File [closed]以编程方式识别 PDF 文件中扫描的文本 [关闭]
【发布时间】:2010-09-14 13:57:31
【问题描述】:

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

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

【问题讨论】:

    标签: pdf ocr


    【解决方案1】:

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

    如果您正在寻找更多 DIY 的东西,这里有 iTextSharp 库 - Java 的 iTextPDFBox 的端口(是的,它说 Java - 但他们有一个 .NET 版本IKVM.NET)。这是一些关于在 C# 中使用 iTextSharpPDFBox 的 CodeProject 文章。

    而且,如果您真的是受虐狂,您可以通过 COM 互操作调用 Adob​​e 的 PDF IFilterIFilter specs 非常简单,但我猜互操作开销会很大。

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

    我以前以交互方式使用过MODI,效果不错。它是 COM,因此通过互操作从 C# 调用它也是 doable 和漂亮的 simple

    ' 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,但我有直接的经验。我听说过它的好坏两面,所以我想这在很大程度上取决于您的源质量。

    【讨论】:

    • 这是一个很好的资源列表......谢谢
    • 如果 PDF 来自扫描仪,那么它实际上只是一个图像,PDFbox 将无法从中获取文本:PDFBox faq about parsing text from PDF。另外,请参阅@jm4 的答案
    【解决方案2】:

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

    【讨论】:

    • PDF 可以是文本和图像的任意组合。如果已经是文本 ocr 是不必要的。
    【解决方案3】:

    我在我的一个博客中发布了关于解析 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-another-sample-for-grade-1-pupils.aspx

    好吧,以下内容基于网络上流行的示例。 它的作用是“读取”pdf文件并将其作为文本输出 表单中的富文本框控件。 PDFBox for .NET 库可以是 从 sourceforge 下载。

    您需要添加对 IKVM.GNU.Classpath & PDFBox-0.7.3 的引用。和 此外,还需要添加 FontBox-0.1.0-dev.dll 和 PDFBox-0.7.3.dll 应用程序的 bin 文件夹。出于某种原因我不记得了 (也许它来自其中一个教程),我也添加到了 bin IKVM.GNU.Classpath.dll。

    在旁注中,刚刚拿到了我的“Head First C#”副本(在 Keith 的 建议)来自亚马逊。书很酷!它真的是为 初学者。本版涵盖VS2008和框架3.5。

    给你...

    /* 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));
            }
    
         }
    }
    

    【讨论】:

    【解决方案4】:

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

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

    我认为您至少需要 Standard 或 Pro 版本,但他们有试用版,因此您可以查看它是否能满足您的需求。

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

        您可以使用像 perl 的 PDF 这样的模块来提取文本。并使用其他工具将相关信息导入数据库。

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

        【讨论】:

          【解决方案7】:

          我最近为 Python 找到了 ReportLab

          【讨论】:

            【解决方案8】:

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

            【讨论】:

              【解决方案9】:

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

              【讨论】:

                【解决方案10】:

                根据 Mark Brackett 的回答,我创建了一个 Nuget package 来包装 pdftotext

                它是 open source,针对 .net standard 1.6.net framework 4.5

                用法:

                using XpdfNet;
                
                var pdfHelper = new XpdfHelper();
                
                string content = pdfHelper.ToText("./pathToFile.pdf");
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2017-06-26
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2010-10-17
                  • 2011-03-07
                  • 1970-01-01
                  • 2010-09-25
                  相关资源
                  最近更新 更多