Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用itextsharp在ftp服务器上提取pdf文件_C#_.net_Indexing_Itext_Lucene.net - Fatal编程技术网

C# 使用itextsharp在ftp服务器上提取pdf文件

C# 使用itextsharp在ftp服务器上提取pdf文件,c#,.net,indexing,itext,lucene.net,C#,.net,Indexing,Itext,Lucene.net,我正在从事文档管理项目,我想从pdf中提取文本。我怎样才能做到这一点。我正在使用Itextsharp在本地系统上提取pdf 这是我用于此目的的函数。路径是FTP服务器路径 public static string ExtractTextFromPdf(string path) { using (PdfReader reader = new PdfReader(path)) { StringBuilder text = new St

我正在从事文档管理项目,我想从pdf中提取文本。我怎样才能做到这一点。我正在使用Itextsharp在本地系统上提取pdf

这是我用于此目的的函数。路径是FTP服务器路径

 public static string ExtractTextFromPdf(string path)
    {
        using (PdfReader reader = new PdfReader(path))
        {
            StringBuilder text = new StringBuilder();

            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
            }

            return text.ToString();
        }
    } 

[####是我的ftp服务器]

PdfReader
有一系列构造函数重载,但大多数都依赖于将传入的内容转换为
流格式。当您传递
字符串时,它是磁盘上的文件,如果不是,则检查它是否可以转换为
Uri
,作为
文件://
http://
https://
链接之一。这是您的第一个失败点,因为这些检查都不能处理ftp协议,您最终会遇到一个不适合您的问题

您可以尝试将
字符串
转换为显式
Uri
,但这实际上也行不通:

//This won't work
new PdfReader(new Uri(path))
这不起作用的原因是,当加载远程资源时,FTP世界中并不存在这种概念

长话短说,当使用FTP时,您希望自己下载文件。根据它们的大小,您可以将它们下载到磁盘或下载到字节数组中。以下是后者的一个示例:

Byte[] bytes;
if( path.StartsWith(@"ftp://")) {
    var wc = WebRequest.Create(path);
    using (var response = wc.GetResponse()) {
        using (var responseStream = response.GetResponseStream()) {
            bytes = iTextSharp.text.io.StreamUtil.InputStreamToArray(responseStream);
        }
    }
}

然后,您可以将本地文件或字节数组传递给
PdfReader
构造函数。

请澄清您的具体问题。要在ftp上从pdf提取文本。。问题是,当我编写PdfReader=new PdfReader(path)时,它会出现异常“文件未作为文件或资源找到”。请在问题中添加其他信息。好的。已编辑@amedeevangasse检查您是否可以在浏览器中打开URL,或在控制台上执行
wget
。此外,你的斜杠向后看。“不应该”是“/”吗?谢谢你@chirs Hass。。实际问题是:我想从其他服务器提取pdf。。我不知道如何做到这一点。请在这方面给我一些建议。谢谢我在上面发布的代码将允许您通过FTP从另一台服务器下载文件(PDF,无论什么)(HTTP也可以)。这是第一步,与iText完全无关,它只是直接的.Net。第二步是从第一步获取字节数组,并将其传递给
PdfReader
构造函数,而不是
path
Byte[] bytes;
if( path.StartsWith(@"ftp://")) {
    var wc = WebRequest.Create(path);
    using (var response = wc.GetResponse()) {
        using (var responseStream = response.GetResponseStream()) {
            bytes = iTextSharp.text.io.StreamUtil.InputStreamToArray(responseStream);
        }
    }
}