The GroupDocs.Comparison for .NET is a C# library which allows you to compare documents and find differences. Compare and merge Microsoft Word, Excel, PowerPoint, OpenDocument, PDF, Text, HTML and many other documents, retrieve a list of changes between source and target document(s), apply or reject changes and save results with GroupDocs.Comparison API. In addition to this, GroupDocs.Comparison can identify styling and formatting changes - like bold, italic, underlines, strikethroughs, font types, etc.

Changes detection algorithms used by GroupDocs.Comparison allows to detect differences in different document parts and blocks:

  • Text blocks - paragraphs, words and characters;
  • Tables;
  • Images;
  • Shapes etc.

Here are simple steps to compare two text files and show differences: 

  • Instantiate Comparer object with source document path or stream;
  • Call Add method and specify the target document path or stream;
  • Call Compare method.

The following code snippet demonstrates the simplest case of documents comparison using a couple lines of code. 

Compare documents from local file

using (Comparer comparer = new Comparer(“source.docx”))
{
    comparer.Add(“target.docx”);
    comparer.Compare(“result.docx”);
}

Compare documents from the stream

using (Comparer comparer = new Comparer(File.OpenRead(“source.docx”)))
{
    comparer.Add(File.OpenRead(“target.docx”));
    comparer.Compare(File.Create(“result.docx”));
}

Let’s say you have two contracts in DOCX format that were concluded in different years. If you use the above code to compare these contracts, you get a DOCX file where the deleted elements are marked in red, the added in blue, and the modified in green as shown below:

Accept or Reject detected differences

GroupDocs.Comparison provides an ability to apply or discard specific changes between source and target documents and save the resultant document with (or without) selected changes.

The following are the steps to apply/reject changes to the resultant document.

The following code sample shows how to accept/reject detected differences.

using (Comparer comparer = new Comparer(“source.docx”))
{
    comparer.Add(“target.docx”);
    comparer.Compare();
    ChangeInfo[] changes = comparer.GetChanges();
    changes[0].ComparisonAction = ComparisonAction.Reject;
    comparer.ApplyChanges(File.Create(“result.docx”), new SaveOptions(), new ApplyChangeOptions() { Changes = changes });
}

Generate document pages preview

GroupDocs.Comparison allows to generate page previews for source, target and resultant document(s) using GeneratePreview method of a Document class.

PreviewOptions class is used to manage preview generation process - specify desired page numbers, image format etc.

The following are the steps to generate a document preview with GroupDocs.Comparison API:

  • Create a new instance of Comparer class and pass the source document path as a constructor parameter;
  • Add target document(s) to comparison using Add method;
  • Source and Targets properties of Comparer object allows to access source and target documents and provides GeneratePreview method;
  • Instantiate the PreviewOptions object with:
    • delegate for each page stream creation (see event handler CreatePageStream); 
    • image preview format - PNG / JPG / BMP;
    • page numbers to process;
    • custom size of preview images (if needed).
  • Call GeneratePreview method of Source and Targets document and pass PreviewOptions to it.

Get page previews for resultant document

using (Comparer comparer = new Comparer(“source.docx”))
{
    comparer.Add(“target.docx”);
    comparer.Compare(“result.docx”);
    Document document = new Document(File.OpenRead(“result.docx”));
    PreviewOptions previewOptions = new PreviewOptions(pageNumber =>
    {
        var pagePath = Path.Combine(“C:\\”, $"result\_{pageNumber}.png");
        return File.Create(pagePath);
    });
    previewOptions.PreviewFormat = PreviewFormats.PNG;
    previewOptions.PageNumbers = new int[] { 1, 2 };
    document.GeneratePreview(previewOptions);
}

Compare multiple documents

GroupDocs.Comparison allows comparing more than two documents. The following code sample shows how to compare multiple documents programmatically.

using (Comparer comparer = new Comparer(“source.docx”)
{
    comparer.Add(“target1.docx”);
    comparer.Add(“target2.docx”);
    comparer.Add(“target3.docx”);
    comparer.Compare(“result.docx”);
}

Installation

NuGet is the easiest way to download and install GroupDocs.Comparison for .NET. Please get a temporary license to test the library without any functional restrictions.

Please check the documentation to learn more about the library. We also offer free technical support so please feel free to contact us - we will be happy to help.