c#从pdf文件中提取数据

c#从pdf文件中提取数据,c#,.net,itext,C#,.net,Itext,我试图使用iTextSharp从pdf文件中提取数据,但出现了两个错误。实际上,我想从pdf文件中提取数据并将其存储到数据库中 这是我的密码: using System; using System.Data; using System.Configuration; using System.Collections; using System.Collections.Generic; using System.Web; using System.Web.Security; using System

我试图使用
iTextSharp
从pdf文件中提取数据,但出现了两个错误。实际上,我想从pdf文件中提取数据并将其存储到数据库中

这是我的密码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.util.collections;
using System.Linq;

public partial class frm_CreatePDF : System.Web.UI.Page
{
    public string P_InputStream3 = "~/My Documents/List Of Holidays 2012";

    protected void Page_Load(object sender, EventArgs e)
    {
        ExtractText();
    }

    private string ExtractText()
    {
        PdfReader reader = new PdfReader(Server.MapPath(P_InputStream3));
        string txt = PdfTextExtractor.GetTextFromPage(reader, 2, new LocationTextExtractionStrategy());
        return txt;
    }
}
错误是:

  • 当前上下文中不存在名称“pdftextractor”
  • 类型或命名空间名称“LocationTextExtractionStrategy”

  • 通过使用指令添加
    ,确保已将定义这两个类的命名空间引入范围:

    using iTextSharp.text.pdf.parser;
    

    我想您需要使用定义LocationTextExtractionStrategy的另一个名称空间

    首先您需要添加对ItextSharp dll的引用,之后您可以添加一个using语句来访问包含该静态类的名称空间


    这是一个迟来的答案,但我发现了你的问题:

    您缺少
    iTextSharp.text.pdf.parser
    using语句。使用iTextSharp.text.pdf将此添加到您的
    下面代码:

    using iTextSharp.text.pdf.parser;
    
    这应该可以很好地找到您的
    位置文本提取策略

    如果需要,您也可以直接在
    LocationTextExtractionsStrategy
    后面添加
    iTextSharp.text.pdf.parser

    string txt =  PdfTextExtractor.GetTextFromPage(reader, 2, new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy());
    

    但我推荐前者。它更干净、更易于阅读。

    是否添加了对dll的引用?首先,您需要添加对ItextSharp dll的引用,然后您可以添加using语句来访问包含该静态类的命名空间。“命名空间“ItextSharp.text.pdf”中不存在类型或命名空间名称“parser”(是否缺少程序集引用?),,这是我遇到的错误你能找到这是什么错误吗..或者你想让我解释这个错误..我猜你使用的是一些旧版本的iTextSharp。下载最新版本。谢谢你的建议朋友我尝试过使用iTextSharp,它给我的不是PDF的内容,而是这个文本(注意:我可以直接在我的电脑上打开PDF并查看它,因此此消息是假的):请稍候…如果此消息最终未被文档的正确内容替换,您的PDF查看器可能无法显示此类文档。您可以通过访问升级到最新版本的Adobe Reader for Windows®、Mac或LinuX®。有关Adobe Reader的更多帮助,请访问acrreader.html…我知道如何添加参考资料。你能告诉我我错过了什么参考资料吗?你能吗???首先检查一下iTextSharp文档示例,没有人能通过心灵感应阅读你的确切问题是什么。我已经给出了我的代码和错误。所以没有人想通过心灵感应阅读问题。