Recognize a paragraph that changes position without treating it as new text.
This example uses plain text. Digital PDFs need the PDF adapter; .docx files are refused at this release.
dotnet add package RecursiveAI.OneDiff.DocumentsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneDiff.Documents; using OneDiff.Documents.Model; var old = CompareDocument.FromText( "Alpha.\n\nBeta.\n\nGamma."); var next = CompareDocument.FromText( "Beta.\n\nGamma.\n\nAlpha."); var settings = new CompareSettings { DetectMoves = true }; var result = Comparison.Of( old, next, settings); foreach (var change in result.Changes) Console.WriteLine($"{change.Kind}: " + change.Old.Text);
Move: Alpha.Unzip the example, then dotnet run.
The full program
The full program only adds the completeness check to the snippet at the top. When a paragraph cannot be matched to its other version, it can appear as deleted and inserted text instead.
using OneDiff.Documents; using OneDiff.Documents.Model; var old = CompareDocument.FromText( "Alpha.\n\nBeta.\n\nGamma."); var next = CompareDocument.FromText( "Beta.\n\nGamma.\n\nAlpha."); var settings = new CompareSettings { DetectMoves = true }; var result = Comparison.Of( old, next, settings); foreach (var change in result.Changes) Console.WriteLine($"{change.Kind}: " + change.Old.Text); Console.WriteLine("Complete: " + result.Verification.Passed);
dotnet run prints:
Move: Alpha. Complete: True
Variations
Disable move detection for the same paragraphs. This is a complete replacement program.
using OneDiff.Documents; using OneDiff.Documents.Model; var old = CompareDocument.FromText( "Alpha.\n\nBeta.\n\nGamma."); var next = CompareDocument.FromText( "Beta.\n\nGamma.\n\nAlpha."); var settings = new CompareSettings { DetectMoves = false }; var result = Comparison.Of( old, next, settings); foreach (var change in result.Changes) Console.WriteLine($"{change.Kind}: " + $"{change.Old.Text} -> {change.New.Text}");
Delete: Alpha. -> Insert: -> Alpha.
Related recipes
- Write a line diff Markdown report in C#
- Detect numeric changes in documents in C#
- Apply diff-match-patch text in C#
The short answer
Keep paragraph breaks in both texts and compare them with DetectMoves = true. A paragraph that has moved can then appear as a Move instead of an unrelated deletion and insertion. In the example, Alpha. moves from the start to the end while Beta. and Gamma. retain their relative order.
How do you compare changes in two documents?
Use Comparison.Of on two CompareDocument values. The double line breaks in the sample separate the three paragraphs. Keep those boundaries when preparing real inputs: this is a document alignment task, not simply a character comparison over a concatenated string. The full program also prints the returned completeness status; its value is True for these inputs.
The comparison first matches unchanged content in order. With move detection enabled, it then looks for unchanged content elsewhere in the other document. As the alignment documentation explains, surrounding content matters too. Repeated paragraphs are not interchangeable just because they have identical text; their position within a section or table can help decide which copies correspond.
Read Change.Kind to identify a reported move. The library considers whether content changed its place within a section or relative to the paragraphs around it. The top snippet prints the reported kind and the old text, so it directly identifies the moved paragraph.
The variation turns move detection off for the same inputs. It prints a deletion and an insertion instead. That gives you a concrete choice between a relocation-aware report and a sequence-only reading of this example. It does not mean either document lost content; it changes how the relationship between the versions is represented.
Moving content and editing content are distinct facts. A moved node can have edits to its text reported as separate changes on the same link. A consumer that wants a full redline should therefore retain all changes, not select only moves and assume the remaining document is unchanged. Moves are substantive under the library’s classification rules, even when their text is unchanged.
The completeness check concerns the compared model: applying the recorded edits reconstructs the new compared content, and removing them reconstructs the old. It does not certify that every ambiguous pairing is the one a human editor intended. If automatic alignment needs correction, the alignment API supports pinning or forcing links before running subsequent stages.
The example compares plain text paragraphs, not .docx files. This release refuses Office inputs; export to PDF and use the PDF adapter for a document-file workflow. For plain text, using the two strings shown here avoids an adapter entirely. Keep the returned diagnostics in an application: if the library reaches its configured search-work limit, it can still account for all changes, but some edited paragraphs appear as deletions and insertions instead of matched edits.