Free Support Forum - groupdocs.com

Compare two documents and show result in a C# Windows Form application

Hello, I am currently evaluating the Comparision .NET for our project. Is there a demo or code snippet on how to display the results of the comparison in a viewer?

Thank you!
Andy

1 Like

@Andy_Colmes

One of the simplest way is to compare documents using GroupDocs.Comparison for .NET API and render the output file using GroupDocs.Viewer for .NET.
How to achieve this?
First thing first, these APIs are totally back-end (UI-Agnostic). Hence, you can implement them in your existing or new .NET solution/application.
So, once you have the comparison output, you can use this open-source UI project to render the output in browser. You can use both APIs in a single project as well.

@atirtahir3

Thank you for taking the time to respond.

I have been reading some of the documentation and I understand that I can get a list of “changes” from the comparison by coordinates.

Instead of a browser, I would like to do it in a WinForm viewer with the same functionality like the MVC demo where one can select to accept or reject the change. It would help tremendously to have a WinForm demo for this so I can speed up the development time and therefore the evaluation.

If not, maybe some kind of guidance in the form of C# code snippets to achieve the appropriate result.

Thank you again.

Andy

1 Like

@Andy_Colmes

Suppose, we want to compare two Word files and view the result or output file in our WinForm. Considering this scenario, first thing is we’ll write comparison code:

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 });
}

What we did in the above code is, we passed a source DOCX file and then compared it with a target DOCX file. Then we retrieved a list of changes. Based on that we can accept or reject any particular change. Eventually, we save the output Word document. You will do this all in respond to a WinForm button click action.

Next step is to render the output DOCX to Image format.

string outputDirectory ="outputPath";
string pageFilePathFormat = Path.Combine(outputDirectory, "page_{0}.png");
using (Viewer viewer = new Viewer(result.docx))
{
    PngViewOptions options = new PngViewOptions(pageFilePathFormat);
    viewer.View(options);
}

This code will generate images of result.docx file.
Eventually, you can view these image(s) in WinForm using PictureBox control (one of the possible way to view images).
Hopefully it’ll help.

@atirtahir3

Thank you again for the code snippets. It helped me a lot to prepare for the project.

What would be your recommendation if I need to view the resulting document/file and be able to iterate through the changes from within the resulting document/file. I guess I would have to “draw” the coordinates on the document and be able to scroll to them when iterating through the changes, such as from a List. I guess my main question would be the drawing of the changes in the document, preferably in a rectangle box. The rectangle box would be clickable and can point to the index of the change in the List.

Thanking you in advance again!

Andy

1 Like

@Andy_Colmes

You are welcome.
Let’s see what is Get changes coordinates and how it could be useful?
Document comparison API allows you to detect changes between source and target documents and obtain changes coordinates at document preview images. These coordinates can be quite useful for highlighting detected changes at document preview images with some graphics (for example, place a red rectangle around each detected change). Please have a look at this documentation article

@atirtahir3

The article really narrows down my requirements. Thank you very much!

As a last question, I assume I would be using the GroupDocs Viewer to preview the resulting document/file. Can you point me to the right documentation resource on how to “jump/scroll” to a specific coordinate that is reported in the Get Changes List?

Thanks again!

Andy

1 Like

@Andy_Colmes

This is more like a UI dependent (client side) requirement/scenario.
However, one way to implement this scenario is, as you have the specific coordinates in GetChanges list. You can highlight those coordinates or take page/document control to the highlighted area once application renders the output document. GroupDocs.Viewer for .NET also allows you to get text coordinates. You may find this documentation helpful.