---
title: "Detect moved document paragraphs in C#"
url: https://recursiveai.org/onediff/cookbook/detect-moved-document-paragraphs/
description: "Recognize a paragraph that changes position without treating it as new text. Includes C# code, printed results, and a downloadable project."
version: "1.0.1"
---

# Detect moved document paragraphs in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDiff.Documents) · [GitHub](https://github.com/recursive-ai-oss/onediff-csharp) · [Docs](https://recursiveai.org/onediff/docs/) · [Download example (.zip)](https://recursiveai.org/onediff/cookbook/detect-moved-document-paragraphs/detect-moved-document-paragraphs.zip)

Recognize a paragraph that changes position without treating it as new text.

This example uses plain text. Digital PDFs need the [PDF adapter](https://recursiveai.org/onediff/docs/pdf/); `.docx` files are refused at this release.

``` shell
dotnet add package RecursiveAI.OneDiff.Documents
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

``` csharp
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);
```

``` text
Move: Alpha.
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/detect-moved-document-paragraphs/detect-moved-document-paragraphs.zip), 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.

```csharp
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:

``` text
Move: Alpha.
Complete: True
```

## Variations

Disable move detection for the same paragraphs. This is a complete replacement program.

``` csharp
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}");
```

``` text
Delete: Alpha. -> 
Insert:  -> Alpha.
```

## Related recipes

- [Write a line diff Markdown report in C#](https://recursiveai.org/onediff/cookbook/write-line-diff-markdown-report/)
- [Detect numeric changes in documents in C#](https://recursiveai.org/onediff/cookbook/detect-semantic-value-changes/)
- [Apply diff-match-patch text in C#](https://recursiveai.org/onediff/cookbook/apply-diff-match-patch/)

<!-- article -->

## 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](https://recursiveai.org/onediff/docs/alignment/) 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.
