How to Easily Compare Word DOCX Documents in C# .NET Core

Cloudmersive
3 min readJun 1, 2024

--

Automating Word document comparisons is useful, but writing code to that end can be a bit of an unwieldy challenge.

Fortunately, there’s good news on that front: we don’t actually need to write a ton of new code by ourselves.

To make easy & high quality DOCX comparisons, we can simply lean on a free DOCX Comparison API.

With a few lines of ready-to-run code examples, we can flexibly incorporate our API call into our .NET Core application. In doing so, we won’t need to worry about burdening our servers with a multi-file processing operation either.

We’ll abstract all that extra work to a cloud server that 1) performs the comparison in-memory and 2) dumps all data upon task completion (i.e., absolutely no data saved to disk).

To better understand this solution, let’s look at the results of a simple DOCX comparison with the aforementioned free-to-use API.

We’ll compare two slightly different versions of the same Lorem Ipsum passage. These passages come from documents named “Lorem Ipsum v1” and “Lorem Ipsum v2” respectively.

Here we have passage 1 (a Lorem Ipsum classic):

And here we have a slightly adjusted version of passage 1, tainted with English (changes are highlighted for convenience):

When we compare these documents using our DOCX comparison API, we’ll end up with a new DOCX file that displays the differences between document 2 and 1 clearly:

To structure our API call, we can begin by installing the client SDK via NuGet. Let’s run the following command in our Package Manager console:

Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1

Next, let’s turn our attention to authorization. To call our DOCX comparison API for free, we’ll just need a free Cloudmersive API key (this will allow us to make a limit of 800 API calls per month with zero commitments).

Now we can use the remaining examples to add in our namespace imports and structure our DOCX comparison request:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;

namespace Example
{
public class CompareDocumentDocxExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new CompareDocumentApi();
var inputFile1 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | First input file to perform the operation on.
var inputFile2 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Second input file to perform the operation on (more than 2 can be supplied).

try
{
// Compare Two Word DOCX
byte[] result = apiInstance.CompareDocumentDocx(inputFile1, inputFile2);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling CompareDocumentApi.CompareDocumentDocx: " + e.Message );
}
}
}
}

Quick note — as mentioned in the code comments, we can expand our comparison operation beyond two documents if we want.

That’s all there is to it — now we can easily leverage a powerful low-code DOCX comparison API in our .NET Core applications.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet