1
GroupDocs.Comparison for .NET  
GroupDocs.Comparison for .NET is a standalone library, middleware, designed to perform a comparison process  
between two documents of the same type, detect all differences (changes) between them and apply or dismiss each  
difference for the resulting document. GroupDocs.Comparison for .NET is a standard .NET assembly, written with  
1
00% managed code, and it can be used in any .NET project with any CLR-compatible language.  
However the main feature of the GroupDocs.Comparison for .NET is focusing on the web applications — the library  
delivers special API methods and web interface for the ASP.NET projects. You can perform comparison and display its  
result on a web-page using a couple lines of code.  
In this article and demo project project I’ll explain you, how the GroupDocs.Comparison for .NET works and how it  
can be used in the real project.  
Main destination and basics of the GroupDocs.Comparison for .NET  
As it was told above, GroupDocs.Comparison for .NET performs a comparison between two documents of the same  
type. You specify a source and a target document, and start a comparison process. I want to focus your attention —  
input documents are not equivalent and interchangeable — there is a source document, which is considered by the  
GroupDocs.Comparison for .NET as a “base”, or “root”, and the target document, which is considering as a some sort  
of “alternative version”. When comparing, GroupDocs.Comparison for .NET detects all differences, which are present  
in the source document and absent in the target document, and vice versa.  
Depending on a document type, GroupDocs.Comparison for .NET can detect not only content-related differences,  
but also document structure changes and style changes. Enumeration “Groupdocs.Comparison.Common.  
ChangeType” contains all possible types of differences, which can be processed by the GroupDocs.Comparison for  
.NET.  
But document comparison is not the only feature of the GroupDocs.Comparison for .NET — it also can perform a  
merge between two documents. When comparison is ready, GroupDocs.Comparison for .NET prepares a set of all  
detected differences. You can leave, apply or discard every single difference, and generate a new document, which  
will be based on this manipulation.  
For ASP.NET (WebForms and MVC version 3+ are supported) GroupDocs.Comparison for .NET contains a special  
widget. You place it on a web-page, and when rendering a page, this widget transforms to the web-content, where  
result document is displayed; this page also contains a web GUI, which allows you to perform further document  
manipulation.  
All of these will be shown below.  
Requirements  
GroupDocs.Comparison for .NET is a .NET assembly, so it requires an installed .NET Framework version 4.0 or higher.  
In order to use it in the ASP.NET application, you should have installed ASP.NET WebForms and ASP.NET MVC version  
3
or higher. When using in the ASP.NET application, GroupDocs.Comparison for .NET also requires  
System.Web.Optimization.dll version 1.1.0.0 and WebGrease.dll version 1.6.5135.21930. All these libraries are  
present in the sample project.  
Of course, you need to have a web-server in order to run the sample project. You can open it using MS Visual Studio  
2
012 or above and run it using the IIS Express or you can create a web-site using the IIS Manager and run it under the  
2
IIS. Please take into account that the ASP.NET Development Server, also known as a Cassini, is not supported by the  
GroupDocs.Comparison for .NET.  
Description of the project  
Project structure  
In the package you will find a “ComparisonDemoSolution” solution, which contains only one ASP.NET MVC 3 project  
“ComparisonDemo”. “libs” folder contains all necessary libraries along with the  
Groupdocs.Web.UI.Comparison.dll” — this is the assembly name of the GroupDocs.Comparison for .NET.  
App_Data” folder contains a test documents; it also serves as storage for output documents and temporary data  
(
this will be discussed below).  
All other files and folders are standard for a simple MVC project.  
web.config file is a very, very simple. I deliberately removed all the extra things that are present there when  
theempty project is generated by the Visual Studio (like different providers, authorization and authentication, Entity  
Framework configuration and so on).  
Initialization of the GroupDocs.Comparison  
Please open the Global.asax file and take a look at the “Application_Start” method. There are 3 lines of code:  
1
2
3
. Specifying a root storage path (“GroupdocsComparison.SetRootStoragePath static method). This is  
absolutely necessary, because GroupDocs.Comparison requires a place where it can store temporary and  
output files.  
. Specifying a license file (“GroupdocsComparison.SetLicensePath”) method. This step is optional. When  
license file is specified, it is present, valid and not expired, GroupDocs.Comparison works in licensed  
unlimited full-featured mode. Otherwise, GroupDocs.Comparison works in a trial mode.  
. Initializing the GroupDocs.Comparison using the “GroupdocsComparison.Init static method. You should  
initialize GroupDocs.Comparison before using it, but at this moment root storage should be already  
initialized. Otherwise the exception will be thrown.  
Basic usage of the GroupDocs.Comparison  
Open the “HomeController” and take a look at the “Index” action. In this action the “ComparisonService” class is  
instantiated. Then the source file, which represents the document, and the target document are specified. At the  
next line the result filename is specified. When comparison will be done, the new file with this name will appear in  
the “App_Data” folder.  
Finally, the “service.Compare” file is called. This method creates a result file (it is usually called a “Redline” in the  
internal GroupDocs.Comparison terminology) and returns the array of the instances of the “ChangeInfo” class. Let’s  
take a look at this class more precisely.  
Groupdocs.Comparison.Common.ChangeInfo” instance class may be considered as a container for each separate  
difference (change) between the source and the target document. It contains the data (textual content, which was  
changed; type of change like insertion or deletion; info about style change, if it was present; page number; part of  
the document, where change is present) and the “comparison action”.  
What is a “comparison action”? This is the method, which allows you to define, what you want to do with the one  
particular difference. You can select one of three values, which are defined by the “Groupdocs.Comparison.Common.  
ComparisonAction” enumeration:  
1
. ComparisonAction. None Merge old value with the new value, so in the result document both values will  
be present.  
3
2
3
. ComparisonAction. Accept  Accept the new value from the target document and replace the old value  
from the source document.  
. ComparisonAction.Reject  Cancel the new value from the target document and leave the old value from  
the source document.  
You can select different actions for different changes and then apply them using the “service.UpdateChanges”  
method.  
In the “Index” action only comparison and obtaining changes are present. Then these changes are sent to the view,  
where they are displayed one after another.  
Using the “ComparisonService” class you can perform comparison and change selection in programmatically, within  
application of any type, even it doesn’t have a GUI at all (for example, console application). But what if you want to  
use GroupDocs.Comparison in the ASP.NET web-site and don’t want to create a web-interface by yourself?  
Web-oriented usage of the GroupDocs.Comparison  
Take a look at the “StandardGui” action at the same “HomeController”. You can find a  
"
Groupdocs.Web.UI.Comparison.GroupdocsComparison", which do almost the same: you specify a source and target  
document, make some other things, and as a result you have the next:  
4
This is a standard GroupDocs.Comparison-enabled web-interface. 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 ID "comparison-wrapper". “ImmediateCompare()” method performs a comparison before the page is  
rendered; if this method is not invoked, comparison will be done on demand, when you will see an empty (with GUI,  
but without document content) page in the browser.  
GroupdocsComparison” should be considered as an advanced, very high-level wrapper around the  
ComparisonService” class, which was discussed above. "GroupdocsComparison" internally invokes a low-level "  
ComparisonService", obtains a result and generates a web-layout/markup, and shows it on a web-page with  
standard GUI.  
In this example the usage of the streams is also shown. In some situations you cannot work with documents as with  
the files, and the only acceptable form is a byte stream (System.IO.Stream). GroupDocs.Comparison allows you to  
specify source, target and result (redline) documents as streams.  
Specifying a source and the target documents as streams is quite easy — you specify an actual stream (don’t forget  
to check a position!) and the file name, which should be compatible with the file system and is considered as a  
unique identifier for the stream. But specifying a stream for the redline document is much harder.  
You should use a "ComparisonSaving" event handler with attached "ComparisonSaveHandler" delegate. There are  
several very important things here.  
Take a look on the "StrHandler" method, which implements a signature of the "ComparisonSaveHandler" delegate  
type. Here is its full signature:  
void ComparisonSaveHandler(System.IO.Stream redlineStream, out string fileName)  
First parameter - "redlineStream" - contains a stream with all content inside. The second output parameter —  
fileName” — contains a filename - GroupDocs.Comparison uses it for saving a stream to the file. Please take into  
account that when using the "ComparisonSaveHandler" delegate instance, there is no "ResultFileName" method.  
GroupDocs.Comparison will create a temporary redline file with the filename from the “fileName” parameter, so  
there is no need to specify a result filename somewhere else. But, of course, if you don’t use stream, you should do  
it.  
Conclusion  
This project and article give you only basic principles, concept of the GroupDocs.Comparison for .NET. This library  
contains much more features, and can do more than described here. Developers constantly improve it and make it  
better, increasing quality of the comparison, adding new document types ad new features. So please stay tuned and  
check the groupdocs.com for actual info.  
Thank you for your attention.