What is the best way of comparing two pdf file in c#?
I want to check the text content of two PDF f开发者_如何学Cile in C#.
If they are identical you can do a binary comparison. If for contextual comparison you probably need a PDF library. Here are some libraries.
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!
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;
}
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.
精彩评论