TX Text Control .NET Server

Document Comparison

Topic

Which paragraphs of a contract changed between two versions? TX Text Control .NET Server has no method that compares two documents, but its API provides the building blocks for it. The approaches that Text Control describes differ in what they find and in the engine they need. For a document that was compared in MS Word or reviewed in the Document Editor, your code processes its tracked changes on the server.

Tracked changes in the browser are the subject of Track Changes in the Document Editor section and Tracked Changes in the Document Viewer section.

Approaches to Comparing Documents

ApproachFindsWhere it is described
Compare the text of paragraphsParagraphs whose text differsThe example below, built from ServerTextControl members
Compare words and mark the differences as tracked changesInserted and deleted wordsA Windows Forms sample of the Text Control blog
Compare rendered pages as imagesVisual differences, such as changed fonts, alignment or colorsA sample of the Text Control blog that uses the Classic engine

Compare the Text of Paragraphs

ServerTextControl loads both documents one after the other, and the text of their paragraphs is compared. The following program prints the paragraphs of Documents/original.docx and Documents/revised.docx that differ:

using TXTextControl;

List<string> originalParagraphs;
List<string> revisedParagraphs;

using (ServerTextControl tx = new ServerTextControl())
{
    tx.Create();

    tx.Load("Documents/original.docx", StreamType.WordprocessingML);
    originalParagraphs = GetParagraphTexts(tx);

    // Loading the second document replaces the first one
    tx.Load("Documents/revised.docx", StreamType.WordprocessingML);
    revisedParagraphs = GetParagraphTexts(tx);
}

int count = Math.Max(originalParagraphs.Count, revisedParagraphs.Count);

for (int i = 0; i < count; i++)
{
    string original = i < originalParagraphs.Count ? originalParagraphs[i] : "(no paragraph)";
    string revised = i < revisedParagraphs.Count ? revisedParagraphs[i] : "(no paragraph)";

    if (original != revised)
    {
        Console.WriteLine($"Paragraph {i + 1} differs");
        Console.WriteLine($"  original: {original}");
        Console.WriteLine($"  revised:  {revised}");
    }
}

// Returns the text of every paragraph of the main text
static List<string> GetParagraphTexts(ServerTextControl tx)
{
    List<string> texts = new List<string>();

    foreach (Paragraph paragraph in tx.Paragraphs)
    {
        texts.Add(paragraph.Text.TrimEnd());
    }

    return texts;
}

The code works as follows:

  • Load replaces the complete contents, so one instance reads both documents.
  • ServerTextControl.Paragraphs contains the paragraphs of the main text, and Paragraph.Text is the text of each.
  • The paragraphs are paired by their position. When a paragraph is inserted or deleted in the revised document, all paragraphs after it are reported as different.

The comparison covers only the text of the paragraphs in the main text: formatting and the text of headers and footers aren't compared.

Compare Words and Mark the Differences

The blog article Word-based Document Comparison and Track Changes Using TX Text Control and C# compares two documents word by word. It walks through the paragraphs of both documents, splits them into sentences and words and marks each difference as a tracked change in the original document, so users review the result like a document edited with track changes. The article points out that this method has limitations.

The window of the comparison sample: the revised text on the right, and on the left the original text with the replaced words struck through in red and the new words inserted and underlined.

Its sample, TXTextControl.Windows.Forms.Comparison, is a Windows Forms application. It turns on track changes with TextControl.IsTrackChangesEnabled, a member of the Windows Forms control that ServerTextControl doesn't have, so the sample can't create tracked changes on the server as it is. The class that records tracked changes on the server is named in Record Changes on the Server.

Compare Pages as Images

The blog article Document Comparison using Image-Based Pixel Matching in .NET C# renders the pages of both documents as images and compares them pixel by pixel. This finds visual differences that a text comparison misses, such as a different font, alignment or color, and it reports which pages differ. When the documents have different numbers of pages, it stops and reports that.

In the article's example, the second version of a document has a smaller image on one page. The text of both versions is the same, but the page images differ, and the comparison reports the page:

A page of the compared document with a photo below a paragraph; a red circle marks the corner of the image whose size was reduced in the second version.

Its sample, TXTextControl.Compare.Pixel, creates a bitmap of each page with Page.GetImage and compares the bitmaps with System.Drawing. It references the Classic package TXTextControl.TextControl.ASP.SDK.

Process Tracked Changes on the Server

A document that was compared in MS Word, or edited with track changes in the Document Editor, contains its differences as tracked changes, each with its author and time. ServerTextControl.TrackedChanges returns them, and TrackedChangeCollection.Remove accepts or rejects each one. Track Changes lists the changes of a document, accepts and rejects them by author or by the decisions of a review and accepts all changes before a document is published.

Next Steps

  • Track Changes: accept and reject tracked changes on the server.
  • Track Changes in the Document Editor: let users review changes in the browser.
  • Pages: render the pages of a document as images.
  • Paragraphs: read and format the paragraphs of a document.
  • Loading Documents: load the documents you compare.