在 C# 中比较两个 pdf 文件的最佳方法是什么?

发布于 2024-10-06 07:18:51 字数 31 浏览 0 评论 0原文

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

I want to check the text content of two PDF file in C#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

昔梦 2024-10-13 07:18:51

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

If they are identical you can do a binary comparison. If for contextual comparison you probably need a PDF library. Here are some libraries.

天煞孤星 2024-10-13 07:18:51

这并不容易,但我想第一步是获得一个像样的 PDF 库,可以从 PDF 中提取文本。我使用过的一个是 ITextSharp,可从 http://itextpdf.com/ (开源)获得。然后尝试使用 diff 库,例如 DIffer:可重用的 C# diffing 实用程序和类库。祝你好运!

Not going to be easy, but I guess first step would be to get a decent PDF library that can extract the text from PDFs. One I've used is ITextSharp available from http://itextpdf.com/ (open-source). Then try a diff library, such as DIffer: a reusable C# diffing utility and class library. Good luck!

囍笑 2024-10-13 07:18:51

已经有一段时间了,但这个功能对我有用(但不能保证......我不记得我是否在带有嵌入图像或其他内容的 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;
    }

It's been awhile, but this function worked for me (but no guarantees... I don't remember if I tried it on PDF's with embedded images or anything). There is a GUID or some sort of ID embedded in the file, you just need to remove that and compare everything else. Here's the code:

    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;
    }
盗琴音 2024-10-13 07:18:51

免责声明:我在 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 工具检测图像上的文本。

Disclaimer: I work for Atalasoft.

Atalasoft's DotImage SDK can be used to extract the text from PDFs in C#. If the PDFs are already searchable you can easily get to the text:

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);
   }
}

Otherwise, you could use the OCR tools to detect the text on the image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文