从在线pdf文件中提取文本(sharepoint)

时间:2018-01-16 13:44:22

标签: c# pdf sharepoint text extract

我知道有类似的问题已经被问及答案,但我无法找到解决问题的方法。 所以基本上我想使用C#从在线pdf文件中提取文本,我可以用[itextsharp]库实现。然而,这适用于我可以通过在谷歌搜索它们找到的一些随机pdf文件。我的目标是对私有sharepoint帐户执行相同的操作,其中存储了多个PDF文件。我的chrome web浏览器设置为记住用户和密码,但我仍然无法用我的代码实现它。对我来说,验证似乎没有问题,但我可能错了。这是代码:

public static string pdfTX(string path)
{
    CookieContainer cookieJar = new CookieContainer();
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);

    request.Proxy.Credentials = CredentialCache.DefaultCredentials;

    request.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";
    request.Credentials = CredentialCache.DefaultCredentials;
    //  request.Credentials = new NetworkCredential(uName, pWord);

    Thread.Sleep(3000);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream stream = request.GetResponse().GetResponseStream();

    string textPDF = string.Empty;

    PdfReader reader = new PdfReader(stream); //error here (iTextSharp.text.exceptions.InvalidPdfException: 'PDF header signature not found.')

    for (int page = 1; page <= reader.NumberOfPages; page++)
    {
       textPDF += PdfTextExtractor.GetTextFromPage(reader, page);
    }

    reader.Close();
    response.Close();
    //  readStream.Close();
    // public string passtext = text;

    return textPDF;
}

我会很高兴获得任何帮助或信息! 感谢您的时间和精力!

1 个答案:

答案 0 :(得分:0)

以下是我使用CSOM的测试代码(如果我使用流来读取文件,我会遇到同样的错误)。

static void Main(string[] args)
        {
            string login = "lee@domain.onmicrosoft.com"; //give your username here  

            using (var context = new ClientContext("https://domain.sharepoint.com/sites/tst"))
            {
                string password = "pw";
                SecureString sec_pass = new SecureString();
                Array.ForEach(password.ToArray(), sec_pass.AppendChar);
                sec_pass.MakeReadOnly();
                context.Credentials = new SharePointOnlineCredentials(login, sec_pass);

                var path = "/sites/tst/mydoc3/beginning_sharepoint_2013_development.pdf";
                var file = context.Web.GetFileByServerRelativeUrl(path);
                context.Load(file);
                context.ExecuteQuery();

                ClientResult<System.IO.Stream> data = file.OpenBinaryStream();
                context.Load(file);
                context.ExecuteQuery();                

                string textPDF = string.Empty;
                using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
                {
                    if (data != null)
                    {
                        data.Value.CopyTo(mStream);
                        byte[] array = mStream.ToArray();
                        PdfReader reader = new PdfReader(array);

                        for (int page = 1; page <= reader.NumberOfPages; page++)
                        {
                            textPDF += PdfTextExtractor.GetTextFromPage(reader, page);
                        }

                        reader.Close();
                    }
                }
            }            
            Console.WriteLine("done");
            Console.ReadKey();

        }

enter image description here