---
title: "Compare scanned documents in C#"
url: https://recursiveai.org/onediff/cookbook/compare-scanned-documents/
description: "Compare text recognized in two page images and write an HTML redline. OCR (optical character recognition) reads the words; this reader supports Latin-script scans."
version: "1.0.1"
---

# Compare scanned 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.Ocr) · [GitHub](https://github.com/recursive-ai-oss/onediff-csharp) · [Docs](https://recursiveai.org/onediff/docs/) · [Download example (.zip)](https://recursiveai.org/onediff/cookbook/compare-scanned-documents/compare-scanned-documents.zip)

Compare text recognized in two page images and write an HTML redline. OCR (optical character recognition) reads the words; this reader supports Latin-script scans.

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

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.Ocr;

// old.png and new.png are in the zip.
OcrRedline.Register();
var result = Comparison.Of("old.png", "new.png");
result.SaveAs("redline.html");
Console.WriteLine($"Changes: {result.Changes.Length}");
```

Writes `redline.html`.

```text
Changes: 1
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/compare-scanned-documents/compare-scanned-documents.zip), then `dotnet run`.

## Full program

The zip’s generated images read “Payment is due in 30 days.” (`old.png`) and “Payment is due in 60 days.” (`new.png`). They are original sample images, not scans of someone else’s document. The program writes `redline.html` and prints the changed text with its uncertainty flag. Unrecognized text produces an `ocr.nothingRecognized` source warning; strict settings throw on it.

```csharp
using OneDiff.Documents;
using OneDiff.Ocr;

OcrRedline.Register();
var comparison = Comparison.Of("old.png", "new.png");
comparison.SaveAs("redline.html");
Console.WriteLine($"Changes: {comparison.Changes.Length}");
foreach (var change in comparison.Changes)
    Console.WriteLine($"{change.Kind}: {change.Old.Text} -> {change.New.Text}; possible OCR error: {change.PossibleOcrError}");
```

```text
Changes: 1
Replace: 30 days -> 60 days; possible OCR error: False
```

## Variations

**Adjust the uncertainty threshold.** A higher threshold flags more low-confidence readings; it does not remove differences.

```csharp
using OneDiff.Documents;
using OneDiff.Documents.Pipeline;
using OneDiff.Ocr;

OcrRedline.Register();
foreach (double threshold in new[] { 0.95, 0.99 })
{
    var result = Comparison.Of("old.png", "new.png",
        new CompareSettings { OcrConfidenceThreshold = threshold });
    int flagged = result.Changes.Count(change => change.PossibleOcrError);
    Console.WriteLine($"Threshold {threshold}: {flagged} flagged");
}
```

```text
Threshold 0.95: 0 flagged
Threshold 0.99: 0 flagged
```

Every word touched by this change was read with confidence above 0.99, so neither the default 0.95 threshold nor 0.99 flags it. Confidence from unchanged words does not affect this flag. Other scans can produce lower-confidence readings.

## Related recipes

- [Compare document version history](https://recursiveai.org/onediff/cookbook/compare-document-version-history/)
- [Export a document comparison to JSON](https://recursiveai.org/onediff/cookbook/export-document-comparison-json/)
- [Combine reviewer changes](https://recursiveai.org/onediff/cookbook/combine-reviewer-changes/)

## The short answer

Register `OcrRedline` from `RecursiveAI.OneDiff.Ocr`, compare the two image paths with `Comparison.Of`, and save the result as HTML. Inspect `PossibleOcrError` on each change before treating it as a real edit. The example compares generated PNG pages with one changed payment term. It shows a text comparison, not a pixel subtraction: the result explains which recognized words differ.

OneDiff keeps recognition uncertainty alongside the difference. A scanner can introduce noise, skew or different line breaks without anyone editing the source document. Conversely, a crisp scan can contain a real change. The confidence flag helps decide what to review, but it cannot make that decision for you.

## How can I compare two PDF documents for differences?

For image-only PDFs, use the same registration and comparison calls as the PNG example, with PDF paths instead. The scan reader handles PDFs whose examined pages do not meet its text-layer threshold. A PDF with an invisible recognized-text layer counts as a digital PDF, so the PDF reader reads that text instead. `OcrRedline.Register()` also registers the PDF reader and gives the scan reader higher priority.

That distinction matters if a PDF already has poor recognized text embedded in it: registering OCR does not force every PDF through recognition. The [scan reader documentation](https://recursiveai.org/onediff/docs/ocr/) describes the selection rules. An unreadable or encrypted PDF is not classified as a scan just because text extraction fails; the PDF reader reports why it cannot read it.

The image reader accepts PNG, JPEG and TIFF signatures rather than trusting the filename. Each image frame becomes a page. Use the original source images when reviewing suspicious changes so you can check the printed words, not just their recognized representations.

## Reviewing changes and the OCR-error flag

Pass the older scan first and the newer scan second. Save the resulting comparison to a new HTML path and review its old and new text. The full program also walks the changes and prints the old text, new text and possible-error flag together. Keep those values together in any review screen you build; a bare change count loses the uncertainty that makes scan comparison different from ordinary text comparison.

`OcrConfidence` is the lowest confidence of the words touched by the change’s edits. For an entire inserted or deleted block, every word on that side counts. `PossibleOcrError` is true when that confidence falls below `OcrConfidenceThreshold`; the documented default is 0.95. Moves, formatting and attribute changes do not have a confidence. These are [reader rules](https://recursiveai.org/onediff/docs/ocr/), not a guarantee that a flagged edit is false or an unflagged edit is correct.

The variation raises the threshold to 0.99. The flag never hides, drops or reorders a change. In a review workflow, use it to request a closer look rather than silently accepting or discarding the difference. If recognition finds no words, inspect the source warning rather than interpreting the absence of changes as evidence that the pages match.

## Using your own scans

Start with the two images in the download and run the program unchanged. Then replace the paths with your own scans. Keep the generated example as a known input when diagnosing a failed reading. The [scan documentation](https://recursiveai.org/onediff/docs/ocr/) limits the reader to Latin script; it is not a general solution for every writing system.

Tables are off by default because independent scans can segment the same table differently. Cell text still reads in order. Recognition does not correct or conceal suspicious words before comparison, so inspect the redline against the images whenever a reported change affects a decision.
