【问题标题】:How to programmatically search a PDF document in c# [closed]如何在 C# 中以编程方式搜索 PDF 文档 [关闭]
【发布时间】:2010-10-08 18:02:38
【问题描述】:

我需要搜索一个 pdf 文件以查看是否存在某个字符串。有问题的字符串肯定被编码为文本(即它不是图像或任何东西)。我试过只搜索文件,就好像它是纯文本一样,但这不起作用。

可以这样做吗?是否有任何 .net2.0 库可以为我从 pdf 文件中提取/解码所有文本?

【问题讨论】:

    标签: c# .net search pdf


    【解决方案1】:

    那里有一些可用的库。 查看http://www.codeproject.com/KB/cs/PDFToText.aspx 和http://itextsharp.sourceforge.net/

    这需要一点努力,但这是可能的。

    【讨论】:

    • +1 用于 iTextSharp。它应该能够做你需要的。
    • 请注意,iTextSharp 使用 AGPL 许可证,因此只有在您公开发布所有代码时才免费。否则,您需要购买商业许可证。
    【解决方案2】:

    在绝大多数情况下,无法通过在记事本中打开 PDF 来直接搜索其内容 - 即使在少数情况下(取决于 PDF 的构建方式),您也只能由于 PDF 在内部处理文本的方式,我们可以搜索单个单词。

    我的公司有一个商业解决方案,可以让您从 PDF 文件中提取文本。我在下面为您提供了一些示例代码as shown on this page,它演示了如何在 PDF 文件中的文本中搜索特定字符串。

    using System;
    using System.IO;
    using QuickPDFDLL0718;
    
    namespace QPLConsoleApp
    {
        public class QPL
        {
            public static void Main()
            {
                // This example uses the DLL edition of Quick PDF Library
                // Create an instance of the class and give it the path to the DLL
                PDFLibrary QP = new PDFLibrary("QuickPDFDLL0718.dll");
    
                // Check if the DLL was loaded successfully
                if (QP.LibraryLoaded())
                {
                    // Insert license key here / Check the license key
                    if (QP.UnlockKey("...") == 1)
                    {
                        QP.LoadFromFile(@"C:\Program Files\Quick PDF Library\DLL\GettingStarted.pdf");
    
                        int iPageCount = QP.PageCount();
                        int PageNumber = 1;
                        int MatchesFound = 0;
    
                        while (PageNumber <= iPageCount)
                        {
                            QP.SelectPage(PageNumber);
                            string PageText = QP.GetPageText(3);
    
                            using (StreamWriter TempFile = new StreamWriter(QP.GetTempPath() + "temp" + PageNumber + ".txt"))
                            {
                                TempFile.Write(PageText);
                            }
    
                            string[] lines = File.ReadAllLines(QP.GetTempPath() + "temp" + PageNumber + ".txt");
                            string[][] grid = new string[lines.Length][];
    
                            for (int i = 0; i < lines.Length; i++)
                            {
                                grid[i] = lines[i].Split(',');
                            }
    
                            foreach (string[] line in grid)
                            {
                                string FindMatch = line[11];
    
                                // Update this string to the word that you're searching for.
                                // It can be one or more words (i.e. "sunday" or "last sunday".
    
                                if (FindMatch.Contains("characters"))
                                {
                                    Console.WriteLine("Success! Word match found on page: " + PageNumber);
                                    MatchesFound++;
                                }
                            }
                            PageNumber++;
                        }
    
                        if (MatchesFound == 0)
                        {
                            Console.WriteLine("Sorry! No matches found.");
                        }
                        else
                        {
                            Console.WriteLine();
                            Console.WriteLine("Total: " + MatchesFound + " matches found!");
                        }
                        Console.ReadLine();
                    }
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用Docotic.Pdf library 在 PDF 文件中搜索文本。

      这是一个示例代码:

      static void searchForText(string path, string text)
      {
          using (PdfDocument pdf = new PdfDocument(path))
          {
              for (int i = 0; i < pdf.Pages.Count; i++)
              {
                  string pageText = pdf.Pages[i].GetText();
                  int index = pageText.IndexOf(text, 0, StringComparison.CurrentCultureIgnoreCase);
                  if (index != -1)
                      Console.WriteLine("'{0}' found on page {1}", text, i);
              }
          }
      }
      

      图书馆还可以从整个文档或任何文档页面extract formatted and plain text。

      免责声明:我为图书馆供应商 Bit Miracle 工作。

      【讨论】:

      • 注意:正如 Bobrovsky 提到的,这是一个商业产品。它的价格不菲(尽管适合它的用途)。
      猜你喜欢
      • 1970-01-01
      • 2014-07-19
      • 2011-01-25
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2013-12-11
      • 2011-03-07
      相关资源
      最近更新 更多