【问题标题】:What is the best way of comparing two pdf file in c#?在 c# 中比较两个 pdf 文件的最佳方法是什么?
【发布时间】:2011-05-21 14:36:37
【问题描述】:

我想在 C# 中检查两个 PDF 文件的文本内容。

【问题讨论】:

  • 如何比较它们?看他们是一样的吗?找出差异?还有什么?

标签: c# .net-3.5


【解决方案1】:

如果它们相同,您可以进行二进制比较。如果要进行上下文比较,您可能需要一个 PDF 库。 Here 是一些库。

【讨论】:

    【解决方案2】:

    这并不容易,但我想第一步是获得一个可以从 PDF 中提取文本的体面的 PDF 库。我使用的一个是 ITextSharp,可从http://itextpdf.com/(开源)获得。然后尝试一个差异库,例如DIffer: a reusable C# diffing utility and class library。祝你好运!

    【讨论】:

      【解决方案3】:

      已经有一段时间了,但这个功能对我有用(但不能保证......我不记得我是否在带有嵌入图像或任何东西的 PDF 上尝试过)。文件中嵌入了 GUID 或某种 ID,您只需将其删除并比较其他所有内容。代码如下:

          static bool ComparePDFs(string file1, string file2)
          {
              if (!File.Exists(file2))
                  return false;
      
              int i;
              string f1 = File.ReadAllText(file1);
              string f2 = File.ReadAllText(file2);
      
              if (f1.Length != f2.Length)
                  return false;
      
              // Remove PDF ID from file1
              i = f1.LastIndexOf("/ID [<");
              if (i < 0)
                  Console.WriteLine("Error: File is not a valid PDF file: " + file1);
              else
                  f1 = f1.Substring(0, i) + f1.Substring(i + 75);
      
              // Remove PDF ID from file2
              i = f2.LastIndexOf("/ID [<");
              if (i < 0)
                  Console.WriteLine("Error: File is not a valid PDF file: " + file2);
              else
                  f2 = f2.Substring(0, i) + f2.Substring(i + 75);
      
              return f1 == f2;
          }
      

      【讨论】:

        【解决方案4】:

        免责声明:我为 Atalasoft 工作。

        Atalasoft 的DotImage SDK 可用于从 C# 中的 PDF 中提取文本。如果 PDF 已经可以搜索,您可以轻松找到文本:

        public String GetText(Stream s, int pageNum, int charIndex, int count)
        {
           using (PdfTextDocument doc = new PdfTextDocument(s))
           {
               PdfTextPage textPage = doc.GetPage(pageNum);                    
               return textPage.GetText(charIndex, count);
           }
        }
        

        否则,您可以使用 OCR 工具来检测图像上的文字。

        【讨论】:

          猜你喜欢
          • 2022-01-01
          • 1970-01-01
          • 2015-08-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-15
          相关资源
          最近更新 更多