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.
dotnet add package RecursiveAI.OneOcrRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
The snippet also needs FieldSchema.cs, shown below and included in the zip.
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, then dotnet run.
Prints each source word’s top-left and bottom-right coordinates in pixels:
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.
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.
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})"); }
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.
if (result.Fields.TryGetValue("batchName", out var field)) { var box = field.Box; Console.WriteLine($"{box.Left:F0}–{box.Right:F0}"); }
226–470Related recipes
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.
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.