Recursive AI

Detect formatting-only changes in C#

OneDiff is a free, open-source diff library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Compare bold or italic styles supplied in C#, not extracted from DOCX files. For digital PDFs, use RecursiveAI.OneDiff.Pdf 1.0.1. For DOCX, build a CompareDocument with your own reader; no released OneDiff package reads DOCX.

Terminal
dotnet add package RecursiveAI.OneDiff.Documents

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneDiff.Documents;
using OneDiff.Documents.Model;
 
var result = Comparison.Of(
    Doc(false), Doc(true),
    CompareSettings.Strict);
foreach (var change in result.Changes)
foreach (var edit in change.Edits)
{
    if (!edit.IsFormatOnly) continue;
    Console.WriteLine(
        $"Format only: {edit.NewText}");
    Console.WriteLine(
        $"Old runs: {edit.OldRuns.Length}");
    foreach (var run in edit.NewRuns)
    {
        var bold = run.Format.Get(
            FormatKeys.Bold);
        Console.WriteLine(
            $"New bold: {bold}");
    }
}
 
static CompareDocument Doc(bool makeBold)
{
    var doc = new CompareDocumentBuilder();
    var style = FormatSet.Of(
        FormatKeys.Bold, "true");
    doc.Add(NodeKind.Paragraph, new()
    {
        Text = "Pay by Friday.",
        Runs = makeBold
            ? [new FormatRun(7, 6, style)]
            : []
    });
    return doc.Build();
}

dotnet run prints:

Text
Format only: Friday
Old runs: 0
New bold: true

A run applies a style over a range of characters. Here, (7, 6) covers Friday without its period. If neither input supplies formatting runs, the comparison cannot recover bold or italic from the text alone.

Unzip the example, then dotnet run.

Variations

To ignore character formatting in the visible changes, replace CompareSettings.Strict in the comparison with CompareSettings.TextOnly:

C#
var result = Comparison.Of(
    Doc(false), Doc(true),
    CompareSettings.TextOnly);

The program prints nothing for this pair. The hidden formatting difference is listed in result.Ledger.Entries.

The short answer

Store the same text in both documents and attach the character formatting for each version as FormatRun values. Run Comparison.Of, then select edits where IsFormatOnly is true. OldRuns and NewRuns tell you which formatting applied before and after. In the sample, the old run array is empty and run.Format.Get(FormatKeys.Bold) returns "true" for the new run over Friday.

How can I see formatting changes in this document?

Compare the text and its runs together. A plain text diff has no reason to report Pay by Friday. twice: the strings are identical. In the in-pair diff, the engine checks equal stretches of text for formatting changes and cuts them where either side’s formatting changes. That lets the example identify just Friday, rather than treating the whole paragraph as newly bold.

Empty OldRuns here means there was no explicit run over that edit in the old model. It doesn’t describe formatting that your source never supplied. If you build the model from a rich-text source, make the text and run offsets agree: (7, 6) means start at zero-based offset 7 and take 6 characters from the paragraph text, excluding the final period. A range calculated on a different version of that text can point to the wrong substring.

Is a paragraph style change the same as a bold edit?

Not in the model. Runs describe formatting over characters. Block-level properties such as paragraph style belong in node attributes and can produce attribute changes instead. The example filters the character edits in each change, so it won’t list a heading-style change that is represented only as a block attribute. If you need both, keep the attribute changes in your report too.

The settings also matter. Strict compares the modeled runs and attributes without hiding formatting. TextOnly hides character, paragraph, list, and section formatting in the ledger. Use the former for a style review and the latter when the visible changes should focus on the words.

All onediff recipes