DEV Community

Cover image for Check if two documents are same
Atir Tahir
Atir Tahir

Posted on

Check if two documents are same

In order to check whether two documents are same or not, you gotta do a detailed comparison. I will tell you about a document comparison API that returns a list of differences programmatically in C#.

Implementation

It won't take more than 4-5 lines of code to find out:

  1. Difference count
  2. Difference summary
  3. Inserted/deleted items or even a style change
using (Comparer comparer = new Comparer(@"D:/source.docx"))
{
        comparer.Add(@"D:/target.docx");
        comparer.Compare(@"D:/result.docx");
        ChangeInfo[] changes = comparer.GetChanges();
}
Enter fullscreen mode Exit fullscreen mode

You can even compare or get a list of changes from stream:

using (Comparer comparer = new Comparer(File.OpenRead("source.docx")))
{
        comparer.Add(File.OpenRead("target.docx));
        comparer.Compare("save resultant file");
        ChangeInfo[] changes = comparer.GetChanges();
}
Enter fullscreen mode Exit fullscreen mode

You can compare PDF, Excel, Presentation or any other supported files. All you have to do is to download this DLL and add it as a reference in your .NET project (existing or new). Post your issues here to get free support.

Top comments (1)

Collapse
 
sabarishcodes profile image
Sabarish Rajamohan

Cool stuff! 😍