C#/VB.NET: Compare Two Word Documents

It is not uncommon at work that we may receive two versions of a Word document and face the need to find the differences between them. Document comparison is particularly important and popular in the fields of laws, regulations and education. In this article, you will learn how to compare two Word documents in C# and VB.NET by using Spire.Doc for .NET.

Below is a screenshot of the two Word documents that’ll be compared.

C#/VB.NET: Compare Two Word Documents

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Compare Two Documents and Save Result in a Third Word Document

Saving the comparison result in a separate Word document allows us to see all the changes made to the original document, including insertions, deletions as well as modifications on formatting. The following are the steps to compare two documents and save the result in a third Word document using Spire.Doc for .NET.

  • Load two Word documents separately while initialing the Document objects.
  • Compare these two documents using Document.Compare() method.
  • Save the result in a third Word document using ;Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace CompareDocuments
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load one Word document
            Document doc1 = new Document("C:\\Users\\Administrator\\Desktop\\original.docx");

            //Load the other Word document
            Document doc2 = new Document("C:\\Users\\Administrator\\Desktop\\revised.docx");

            //Compare two documents
            doc1.Compare(doc2, "John");

            //Save the differences in a third document
            doc1.SaveToFile("Differences.docx", FileFormat.Docx2013);
            doc1.Dispose();
        }
    }
}

C#/VB.NET: Compare Two Word Documents

Compare Two Documents and Return Insertions and Deletions in Lists

Developers may only want to obtain the insertions and deletions instead of the whole differences. The following are the steps to get insertions and deletions in two separate lists.

  • Load two Word documents separately while initialing the Document objects.
  • Compare two documents using Document.Compare() method.
  • Get the revisions using the constructor function of the DifferRevisions ;class.
  • Get a list of insertions through DifferRevisions.InsertRevisions property.
  • Get a list of deletions through DifferRevisions.DeleteRevisions property.
  • Loop through the elements in the two lists to get the specific insertion and deletion.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Fields;
using System;

namespace GetDifferencesInList
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load one Word document
            Document doc1 = new Document("C:\\Users\\Administrator\\Desktop\\original.docx");

            //Load the other Word document
            Document doc2 = new Document("C:\\Users\\Administrator\\Desktop\\revised.docx");

            //Compare the two Word documents
            doc1.Compare(doc2, "Author");

            //Get the revisions
            DifferRevisions differRevisions = new DifferRevisions(doc1);

            //Return the insertion revisions in a list
            var insetRevisionsList = differRevisions.InsertRevisions;

            //Return the deletion revisions in a list
            var deletRevisionsList = differRevisions.DeleteRevisions;

            //Create two int variables
            int m = 0;
            int n = 0;

            //Loop through the insertion revision list 
            for (int i = 0; i < insetRevisionsList.Count; i++)
            {
                if (insetRevisionsList[i] is TextRange)
                {
                    m += 1;
                    //Get the specific revision and get its content
                    TextRange textRange = insetRevisionsList[i] as TextRange;
                    Console.WriteLine("Insertion #" + m + ":" + textRange.Text);
                }
            }
            Console.WriteLine("=====================");

            //Loop through the deletion revision list 
            for (int i = 0; i < deletRevisionsList.Count; i++)
            {
                if (deletRevisionsList[i] is TextRange)
                {
                    n += 1;
                    //Get the specific revision and get its content
                    TextRange textRange = deletRevisionsList[i] as TextRange;
                    Console.WriteLine("Deletion #" + n + ":" + textRange.Text);
                }
            }
            Console.ReadKey();
        }
    }
}

C#/VB.NET: Compare Two Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.