---
title: "Get bounding boxes for an extracted document field in C#"
url: https://recursiveai.org/oneocr/cookbook/locate-extracted-document-field-on-image/
description: "Get the bounding boxes of an extracted document field in C#. Load a prepared OneOcr model and use WordBoxes to locate each source word in the image."
version: "1.1.0"
---

# Get bounding boxes for an extracted document field in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneOcr) · [GitHub](https://github.com/recursive-ai-oss/oneocr-csharp) · [Docs](https://recursiveai.org/oneocr/docs/) · [Download example (.zip)](https://recursiveai.org/oneocr/cookbook/locate-extracted-document-field-on-image/locate-extracted-document-field-on-image.zip)

The zip has `batch.png` (“Batch Blue Cedar”) and its demo model, `batches.onm`. For your own document layout, you’ll need a [trained `.onm` model](https://recursiveai.org/oneocr/cookbook/fine-tune-custom-document-extractor/).

```shell
dotnet add package RecursiveAI.OneOcr
```

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

The snippet also needs `FieldSchema.cs`, shown below and included in the zip.

```csharp
using OneImage;
using OneOcr;
using OneOcr.Documents;
var engine = OcrEngine.LoadBundled();
var model = DocumentExtractor.Load(
    "batches.onm");
var image = Image.Load("batch.png");
var result = image.Extract(
    FieldSchema.Default.BatchFields, engine, model);
foreach (var b in result.WordBoxes("batchName"))
    Console.WriteLine($"{b.Left:F0},{b.Top:F0} " +
                      $"{b.Right:F0},{b.Bottom:F0}");
```

Unzip [the example](https://recursiveai.org/oneocr/cookbook/locate-extracted-document-field-on-image/locate-extracted-document-field-on-image.zip), then `dotnet run`.

Prints each source word’s top-left and bottom-right coordinates in pixels:

```text
226,104 320,139
345,104 470,139
```

`FieldSchema` uses the .NET SDK’s JSON source generator. Add this `FieldSchema.cs` next to `Program.cs` if you’re pasting into your own .NET 10 console project. The model’s field is `batchName`; the C# property is `Name`.

```csharp
using System.Text.Json.Serialization;

record BatchFields(
    [property: JsonPropertyName("batchName")] string? Name);

[JsonSerializable(typeof(BatchFields))]
partial class FieldSchema : JsonSerializerContext;
```

## The full program

The sample image shows “Batch Blue Cedar” above “Warehouse Copy.” `batches.onm` is a demo model for that layout, not a general-purpose form model. The example loads it without training and prints both corners of each selected word box.

```csharp
using System.Globalization;
using OneImage;
using OneOcr;
using OneOcr.Documents;

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
var model = DocumentExtractor.Load("batches.onm");
var engine = OcrEngine.LoadBundled();
var image = Image.Load("batch.png");
var schema = FieldSchema.Default.BatchFields;
var result = image.Extract(schema, engine, model);
Console.WriteLine($"Name: {result.Value.Name}");
var boxes = result.WordBoxes("batchName");
if (boxes.Count == 0)
    Console.WriteLine("No source words for batchName.");
foreach (var box in boxes)
{
    Console.WriteLine($"({box.Left:F0}, {box.Top:F0}) to " +
                      $"({box.Right:F0}, {box.Bottom:F0})");
}
```

```text
Name: Blue Cedar
(226, 104) to (320, 139)
(345, 104) to (470, 139)
```

An existing property with no extraction gives an empty list; passing a name that isn’t a JSON property of the type throws `KeyNotFoundException`.

## Variations

**Use one rectangle for the whole field.** Add this after extraction to use the enclosing box instead of a rectangle for each word.

```csharp
if (result.Fields.TryGetValue("batchName", out var field))
{
    var box = field.Box;
    Console.WriteLine($"{box.Left:F0}–{box.Right:F0}");
}
```

```text
226–470
```

## Related recipes

- [Read character-level OCR boxes](https://recursiveai.org/oneocr/cookbook/read-character-level-ocr-boxes/)
- [Fine-tune a custom document extractor](https://recursiveai.org/oneocr/cookbook/fine-tune-custom-document-extractor/)

## The short answer

Load a prepared `DocumentExtractor` and call `image.Extract` with the JSON type information for your result type. Call `WordBoxes` on that result using the field’s JSON name. For the batch slip, `WordBoxes("batchName")` gives two rectangles, one over “Blue” and one over “Cedar.” You don’t have to find the field’s value by searching the OCR text again, which could match another occurrence elsewhere on the page.

## What are bounding box coordinates in OCR?

An `OcrBox` gives `Left`, `Top`, `Right`, and `Bottom`. For an image these are pixel coordinates from the top-left of the image: x increases to the right and y increases downward. They are not percentages. Keep the decimals if your viewer accepts them; the console output rounds to whole pixels for readability. If you display a scaled copy, scale the x and y coordinates in the same way and apply the same display offset as the image. For PDF pages, [the units and coordinate mapping differ](https://recursiveai.org/oneocr/cookbook/map-ocr-boxes-to-pdf-page-coordinates/).

## How can I get the X and Y coordinates of an extracted field?

Use each word box’s `Left` and `Top` for its x and y position, and its `Right` and `Bottom` for the far edges. The boxes are in reading order. For a field that wraps across lines, drawing each word separately avoids shading the space between lines. For a single rectangle, the `Fields` variation gives you the enclosing box, including the gaps between words.

Use the field names the prepared model was trained to return as JSON property names. Here `Name` is the property you read from `result.Value`, but `batchName` is the name you pass to `WordBoxes`. The extract result tells you where the model read the value; it doesn’t certify that it picked the correct words. That’s why keeping the coordinates is useful when someone needs to check an extraction against the source image.
