Compare documents with GroupDocs.Comparison

GroupDocs.Comparison finds the differences between two versions of a document and writes a result file with the changes marked up — insertions, deletions and style changes, down to word and character level.

The examples below compare two revisions of the same contract. contract-v1.docx and contract-v2.docx differ in exactly three places: the delivery date, the contract value, and a late-delivery clause added in v2.

The change count comes out higher than three, because GroupDocs.Comparison reports at word level and records a reworded sentence as a deletion plus an insertion rather than as one edit.

Compare two documents

The source document goes to the constructor, the revision to Add, and Compare writes the marked-up result.

using GroupDocs.Comparison;

using (Comparer comparer = new Comparer("contract-v1.docx"))
{
    comparer.Add("contract-v2.docx");

    comparer.Compare("comparison-compare-documents.docx");
}

contract-v1.docx and contract-v2.docx are the sample files used in this example. Download contract-v1.docx and contract-v2.docx.

Binary file (DOCX, 16 KB)

Download full output

In the result, inserted text is blue, deleted text is red and struck through, and style-only changes are green.

List every change

GetChanges returns the differences as data rather than a document, which is what you want when the comparison feeds a review UI or an audit log rather than a human reader.

using System;
using GroupDocs.Comparison;
using GroupDocs.Comparison.Result;

using (Comparer comparer = new Comparer("contract-v1.docx"))
{
    comparer.Add("contract-v2.docx");
    comparer.Compare();

    ChangeInfo[] changes = comparer.GetChanges();

    Console.WriteLine("Changes found: " + changes.Length);

    foreach (ChangeInfo change in changes)
    {
        Console.WriteLine(change.Id + ". " + change.Type
                          + " | source: '" + change.SourceText + "'"
                          + " | target: '" + change.TargetText + "'");
    }
}

contract-v1.docx and contract-v2.docx are the sample files used in this example. Download contract-v1.docx and contract-v2.docx.

Changes found: 7
0. Deleted | source: 'The vendor shall deliver the completed remediation plan by 30 June 2026.' | target: 'The vendor shall deliver the completed remediation plan by 31 August 2026.'
1. Inserted | source: 'The vendor shall deliver the completed remediation plan by 30 June 2026.' | target: 'The vendor shall deliver the completed remediation plan by 31 August 2026.'
2. Inserted | source: 'The vendor shall deliver the completed remediation plan by 30 June 2026.' | target: 'The vend
[TRUNCATED]

Download full output

Accept or reject individual changes

Each change carries a ComparisonAction. Set it to Reject and re-apply, and the result keeps the source wording for that change while accepting the rest — the API behind a review workflow.

using System;
using GroupDocs.Comparison;
using GroupDocs.Comparison.Result;

using (Comparer comparer = new Comparer("contract-v1.docx"))
{
    comparer.Add("contract-v2.docx");
    comparer.Compare();

    ChangeInfo[] changes = comparer.GetChanges();

    // Accept everything except the first change, which we reject
    foreach (ChangeInfo change in changes)
    {
        change.ComparisonAction = ComparisonAction.Accept;
    }
    if (changes.Length > 0)
    {
        changes[0].ComparisonAction = ComparisonAction.Reject;
        Console.WriteLine("Rejected: '" + changes[0].TargetText + "'");
    }

    comparer.ApplyChanges("comparison-accept-reject.docx", new ApplyChangeOptions
    {
        Changes = changes
    });
}

contract-v1.docx and contract-v2.docx are the sample files used in this example. Download contract-v1.docx and contract-v2.docx.

Binary file (DOCX, 16 KB)

Download full output

Learn more

GroupDocs.Comparison also compares more than two revisions at once, compares whole directories, produces a summary page, compares password-protected documents, and lets you tune sensitivity and the styling of each change type.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.