Getting Started Guide

In the package there is a “ComparisonDemoSolution” that contains a single ASP.NET MVC 3 project - “ComparisonDemo”.

The “libs” folder contains all necessary libraries except the "Groupdocs.Web.UI.Comparison.dll" - this is the assembly name of the GroupDocs.Comparison for .NET library. Please download this missing library from the GroupDocs website using this link. Once downloaded, please add the library (Groupdocs.Web.UI.Comparison.dll) to the “libs” folder.

The “App_Data” folder contains sample documents. It also serves as storage for output documents and temporary data. All other files and folders are quite standard for a simple MVC project.

Initialization of GroupDocs.Comparison

Please open the “Global.asax” file. You will find the “Application_Start” method there with the following code:

  • GroupdocsComparison.SetRootStoragePath” static method - specifies a root storage path. This is absolutely necessary, since GroupDocs.Comparison has to store temporary and output files somewhere.
  • GroupdocsComparison.SetLicensePath” - specifies a license file path. This is optional. When a valid license file is specified, GroupDocs.Comparison works without any restriction. Otherwise, it works in an evaluation mode.
  • GroupdocsComparison.Init” static method - initializes GroupDocs.Comparison. You should initialize GroupDocs.Comparison before you can use it. But note that the root storage should be initialized first. Otherwise an exception will be thrown.

Comparing Documents on the Server

First, I’d like to note that the input documents you want to compare are not equivalent or interchangeable. One of the two documents is considered by the library as a source (or a base document), and the other – as a target document (or an alternative version). When comparing them, GroupDocs.Comparison searches for differences that are present in the source document (e.g. content) and absent in the target document, and vice versa.

Now, open the “HomeController” and find the “Index” action. There is the “ComparisonService” class instantiated there and the source and the target documents are specified. Also, the next line specifies the resulting file name. When comparison is done, a new file with this name will be created in the “App_Data” folder.

Next, the “service.Compare” file is called. This method creates a resulting file (it is usually called a “Redline” file) and returns an array of instances of the “ChangeInfo” class. Let’s take a look at this class more closely.

The “Groupdocs.Comparison.Common.ChangeInfo” instance class may be considered as a container for each separate difference (change) between the source and the target documents. It contains the “comparison action” along with details about the found differences (content that was changed; a type of change, for example, insertion or deletion; style changes details; page number and the part of the document where the difference was found).

But what is the “comparison action” for? This is the method that allows you to apply one of the available actions to each individual difference found within the compared documents. Currently there are the following values available (they are defined by the “Groupdocs.Comparison.Common.ComparisonAction” enumeration):

  1. ComparisonAction.Accept – allows you to replace the old value from the source document with the new value from the target document (accept change).
  2. ComparisonAction.Reject – allows you to leave the old value from the source document (reject change).
  3. ComparisonAction.None – allows you to merge the old value from the source document with the new value from the target document. In the resulting document both of the values will be included.

You can select different actions for each individual difference and then apply them using the “service.UpdateChanges” method.

Now, these changes are sent to the view, where they are displayed one after another the following way:

Document Comparison Summary

Purely by using the “ComparisonService” class, you can compare documents and accept/reject found differences programmatically within any type of application, even if it doesn’t have a GUI at all. But what if you want to use GroupDocs.Comparison in an ASP.NET web-site and enable end users to compare, accept/reject differences on their own? Here comes the most interesting part:

Diff View User Interface

Please open the “HomeController” and find the “StandardGui” action. There is the "Groupdocs.Web.UI.Comparison.GroupdocsComparison" class, which works almost the same as the “ComparisonService” described earlier - you specify source and target documents and get this in the output:

Compare Two Documents in C# - Diff View Library

This is a standard GroupDocs.Comparison web UI. Please take a look at the “Comparison("#comparison-wrapper")” method and its value in the “StandardGui” action. In the “StandardGui.cshtml” view you can find a DIV block with the "comparison-wrapper" ID. The “ImmediateCompare()” method compares documents before the page is rendered. If this method is not invoked, you will see the GroupDocs GUI in a browser with no documents in it and the comparison will be done on-demand.

GroupdocsComparison” can be considered as an advanced, high-level wrapper around the “ComparisonService”. It invokes the low-level "ComparisonService" internally, obtains a result, generates web layout/markup, and shows it on a web page with the standard GUI.

Comparing Documents in Streams

GroupDocs.Comparison for .NET allows you to specify source, target and result (redline) documents as streams. This sample demonstrates this as well.

Setting source and target documents as streams is easy - you just need to specify an actual stream (but please do not forget to check a position!) and a file name (which should be compatible with the file system and is considered as a unique identifier for the stream).

Setting a stream for the result (redline) document is a bit harder. You should use the "ComparisonSaving" event handler with the attached "ComparisonSaveHandler" delegate. There are several important things to note here:

Please take a look at the "StrHandler" method, which implements the signature of the "ComparisonSaveHandler" delegate type. Here is its full signature:

void ComparisonSaveHandler(System.IO.Stream redlineStream, out string fileName)

The first parameter - "redlineStream" - contains the stream with all the content inside. The second output parameter - “fileName” – specifies the file name used when saving the stream to a file.

Please note that when using the "ComparisonSaveHandler" delegate instance, there is no "ResultFileName" method. GroupDocs.Comparison creates a temporary result (redline) file with the filename set in the “fileName” parameter, so that there is no need to specify a name for the result (redline) file somewhere else. But, of course, you should do this if you don’t use a stream.

Useful Links


GroupDocs.Comparison for .NET Library - Product Home Page:

Support Forum:

Last edited Sep 16, 2015 at 12:18 PM by GroupDocs, version 12