Get comparison result as an object model


GroupDocs.Comparison allows you to get result document object model after comparison.

To get result document object, follow these steps:

  1. Instantiate the Comparer object. Specify the source document path or stream.
  2. Call the Add method. Specify the target document path or stream.
  3. Call the Compare method and assign its return value to a Document object.

You can retrieve the name, password, changes, stream and other document information using Document object. Also, our object model is fully compatible with the object models of Aspose libraries.

The following code snippets show how to get changes and create new Aspose Word document using result document object model:

Obtain the result document object and get changes

using System;
using GroupDocs.Comparison;
// ...

using (Comparer comparer = new Comparer("source.docx"))
{
      comparer.Add("target.docx");
      GroupDocs.Comparison.Document resultDocument = comparer.Compare("result.docx");
      foreach (var change in resultDocument.Changes)
      {
          Console.ForegroundColor = ConsoleColor.Green;
          Console.Write("Source test: ");
          Console.ForegroundColor = ConsoleColor.White;
          Console.WriteLine(change.SourceText);

          Console.ForegroundColor = ConsoleColor.Blue;
          Console.Write("Target test: ");
          Console.ForegroundColor = ConsoleColor.White;
          Console.WriteLine(change.TargetText + "\n");
      }
}

The result is as follows:

Create a new Word document based on the result document and modify its contents

You can use the Comparison Document object to generate Aspose documents and perform modifications seamlessly. The resultDocument.Stream will contain an Aspose document object corresponding to the format being compared.

using System;
using GroupDocs.Comparison;
using Aspose.Words;
// ...

using (Comparer comparer = new Comparer("source.docx"))
{
      // Add target document and save comparison result to Comparison.Document object
      comparer.Add("target.docx");
      GroupDocs.Comparison.Document resultDocument = comparer.Compare("result.docx");

      // Create a new Aspose Document object of corresponding format.
      Aspose.Words.Document asposeDocument = new Aspose.Words.Document(resultDocument.Stream);

      // Access the Document's builder to add content.
      DocumentBuilder builder = new DocumentBuilder(asposeDocument);

      // Add some text to the document.
      builder.Writeln("Hello, World!");
      builder.Write("This is an example of using Aspose.Words.");

      // Save the document to a file.
      asposeDocument.Save("output.docx");
}

The result is as follows: