---
title: "Manually align paragraphs before comparing documents in C#"
url: https://recursiveai.org/onediff/cookbook/pin-document-paragraph-alignment/
description: "Use OneDiff to pin an old paragraph to a new one before diffing their text. Run the C# example with plain document nodes and no Word package."
version: "1.0.1"
---

# Manually align paragraphs before comparing documents 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/pin-document-paragraph-alignment/pin-document-paragraph-alignment.zip)

`Stages.Align` pairs paragraphs automatically, including [moved paragraphs](https://recursiveai.org/onediff/cookbook/detect-moved-document-paragraphs/) and [splits and merges](https://recursiveai.org/onediff/cookbook/detect-paragraph-splits-and-merges/); use `Pin` when you know which two paragraphs belong together and the automatic pass didn’t pair them.

```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 oldDoc = CompareDocument.FromText("Red.");
var newDoc = CompareDocument.FromText("Blue.");
var pairs = Stages.Align(oldDoc, newDoc);
// node 1 = first paragraph (node 0 is the section)
var edits = Stages.DiffPairs(pairs.Pin(1, 1));
var changes = Stages.Group(edits);
Console.WriteLine(changes.Changes[0].Kind);
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/pin-document-paragraph-alignment/pin-document-paragraph-alignment.zip), then `dotnet run`.

Prints:

```text
Replace
```

## The full program

```csharp
using OneDiff.Documents;
using OneDiff.Documents.Model;

var oldDoc = CompareDocument.FromText(
    "Red.\n\nEnd of notice.");
var newDoc = CompareDocument.FromText(
    "Blue.\n\nEnd of notice.");
var automatic = Stages.Align(oldDoc, newDoc);
var pinned = automatic.Pin(1, 1);
Console.WriteLine($"Automatic: {automatic.LinkOfOld(1)?.Kind.ToString() ?? "unpaired"}");
Console.WriteLine($"Pinned: {oldDoc[1].Text} -> {newDoc[1].Text}");

var edits = Stages.DiffPairs(pinned);
var changes = Stages.Group(edits);
foreach (var change in changes.Changes)
    Console.WriteLine($"{change.Kind}: {change.Old.Text} -> {change.New.Text}");
```

`dotnet run` prints:

```text
Automatic: unpaired
Pinned: Red. -> Blue.
Replace: Red -> Blue
```

In these plain-text documents, node `0` is the section and node `1` is the first paragraph. The automatic pass leaves “Red.” unpaired. The pin makes “Red.” and “Blue.” one pair, so the diff reports a replacement. `Pin` throws `ArgumentException` if an ID doesn’t exist or the nodes are of different kinds.

## Variations

**Remove the pin and try automatic alignment again.** In the full program, add this after creating `pinned`:

```csharp
var link = pinned.LinkOfOld(1)!;
var seed = pinned.Unpin(link.Id);
var retried = Stages.Align(
    oldDoc, newDoc, seed: seed);
Console.WriteLine(
    retried.LinkOfOld(1) is null);
```

```text
True
```

## Related recipes

- [Detect moved paragraphs](https://recursiveai.org/onediff/cookbook/detect-moved-document-paragraphs/)
- [Detect paragraph splits and merges](https://recursiveai.org/onediff/cookbook/detect-paragraph-splits-and-merges/)

## The short answer

Run `Stages.Align` on two `CompareDocument` values, then call `Pin(oldNodeId, newNodeId)` on the returned alignment. Pass the result, not the original alignment, to `Stages.DiffPairs`. Pinning tells OneDiff which blocks to compare. It doesn’t make their text equal or suppress the edits inside the pair.

## How do you compare two text paragraphs after matching them by hand?

Use node IDs from the corresponding document, not paragraph positions on the page. `CompareDocument.FromText` puts a section at the root and separates paragraphs at blank lines. If you build the documents yourself, `CompareDocumentBuilder.Add` returns the ID of the node it added. Keep those IDs if, for example, your app already associates paragraphs by a stable key and a reviewer needs to correct one pair.

`Pin` returns a new alignment. It removes any link that used either selected node; the other nodes in those removed links become unpaired. If you want to give those nodes another chance to match, pass the corrected alignment as `seed:` to `Stages.Align` on the same documents. [The alignment pass](https://recursiveai.org/onediff/docs/alignment/) keeps pinned links and aligns the rest afresh. An out-of-order pinned pair can be reported as a move rather than an in-order match.

## How do you highlight differences between two documents after correcting a match?

Keep the corrected alignment through the rest of the pipeline. `Stages.DiffPairs` finds edits inside the selected pairs; `Stages.Group` turns those edits into a change list. To write an HTML redline, a copy with edits marked, finish the comparison and save it. Add this after the full program’s `changes` line:

```csharp
var result = Stages.Verify(
    Stages.Classify(changes));
result.SaveAs("redline.html");
```

Running this writes `redline.html` beside the project. Starting a fresh comparison instead would run alignment again without your correction.
