Leaders Logo

OCR and .NET: Architectures for Intelligent Large-Scale Document Processing

Introduction

If you have ever worked in a company that deals with a lot of paper, you know the scene: invoices, contracts, medical records, and receipts arrive as images or scanned PDFs, without a single searchable line of text. As long as the content is trapped in the image, you can't automate, search, or analyze anything. That's where optical character recognition (OCR) comes in, transforming these images into text and serving as the foundation for any intelligent document processing project.

Today, extracting text from an image is the easy part. The hard part is doing this with millions of documents without losing a single one along the way, without blowing the budget, and without exposing personal data. In this article, we will build together a distributed OCR architecture in .NET 10, covering asynchronous ingestion, parallel processing, image handling, machine learning, observability, and GDPR.

A Bit of Context

From Classic OCR to Document Understanding

OCR has evolved a lot. Classic engines like Tesseract follow a well-defined script: they analyze the layout, separate the lines, and classify character by character. Then came deep learning models, such as recurrent convolutional networks (CRNN), which read the entire sequence at once, without needing to crop each letter. More recently, models like LayoutLM have also started to consider where the text is on the page. With this, the question is no longer just "what is written?" but also "what does this document mean?", making it possible to extract fields, tables, and forms.

Why .NET?

Because .NET 10 already provides practically everything this kind of system needs: ASP.NET Core for the APIs, System.Threading.Channels and Parallel.ForEachAsync to control concurrency, ML.NET and ONNX Runtime to run models locally, as well as good integration with containers and OpenTelemetry. ML.NET, by the way, was created precisely to bring machine learning to production .NET applications without relying on external runtimes (AHMED et al., 2019).

The Challenges of Processing at Scale

Scalability and fault tolerance

OCR is heavy: it consumes a lot of CPU (or GPU), and processing time varies greatly from one document to another. If you process everything within the HTTP request itself, throughput drops and the client is left waiting until a timeout occurs. The natural solution is to put a queue in between, separating the receiver from the processor. However, queues bring their own problems: duplicate messages, lost messages, and reprocessing. The golden rule is to assume that every message may arrive more than once (at-least-once) and make every step idempotent (KLEPPMANN, 2017).

Extraction quality

In real life, documents don’t arrive pretty. There are crooked photos taken on cell phones, low-resolution scans, stamps over text, and each provider with a different layout. That’s why the pipeline needs to adapt: apply the right treatment to each type of document and combine OCR with layout models to recover structure, not just text.

Security and privacy

Corporate documents are full of personal data: name, taxpayer number, address and, in some sectors, even health information. The LGPD requires a legal basis, collection only of what is necessary, security, and the ability to prove that all of this is being fulfilled (BRAZIL, 2018). That is, privacy needs to be built into the design from the start, not patched in at the end.

Hands-On: Reference Architecture in .NET

One service for each step

The idea is to break the pipeline into small services, each with a single responsibility: ingestion, pre-processing, OCR, semantic extraction, anonymization, indexing, and delivery. The big advantage is that each step scales in its own way, which makes all the difference, since OCR consumes much more resources than receiving a file or indexing text (NEWMAN, 2021). The services communicate via messaging (Azure Service Bus, RabbitMQ, or Kafka), so one does not have to wait for the other to be available to continue working (RICHARDSON, 2018).

SVG Image from the Article

Asynchronous ingestion with 202 Accepted

The entry point does not perform any OCR. It only stores the file in object storage, registers the job, publishes a message to the queue, and immediately responds with 202 Accepted, informing where the client can track the status:

public sealed record DocumentMessage(Guid JobId, Uri BlobUri);

[ApiController]
[Route("api/ocr/documents")]
public sealed class OcrDocumentsController(
    IBlobStorage storage,
    IDocumentQueue queue,
    IJobStore jobs) : ControllerBase
{
    [HttpPost]
    [RequestSizeLimit(20_000_000)]
    public async Task<IActionResult> Submit(IFormFile document, CancellationToken ct)
    {
        if (document.Length == 0)
        {
            return BadRequest("Empty document.");
        }

        var jobId = Guid.CreateVersion7();

        await using var content = document.OpenReadStream();
        var blobUri = await storage.UploadAsync(jobId, content, ct);

        await jobs.CreateAsync(jobId, ct);
        await queue.EnqueueAsync(new DocumentMessage(jobId, blobUri), ct);

        return AcceptedAtAction(nameof(GetStatus), new { jobId }, new { jobId });
    }

    [HttpGet("{jobId:guid}")]
    public async Task<IActionResult> GetStatus(Guid jobId, CancellationToken ct)
        => await jobs.FindAsync(jobId, ct) is { } job ? Ok(job) : NotFound();
}

A detail worth highlighting: Guid.CreateVersion7() generates time-ordered identifiers. This makes database indexes more efficient and makes it much easier to track a document through the pipeline logs.

Parallel workers with backpressure

Inside each worker, concurrency needs to have a limit. A channel with fixed capacity solves this with backpressure: if the workers can't keep up, whoever is producing messages waits a bit, instead of piling everything up in memory until the container goes down.

var channel = Channel.CreateBounded<DocumentMessage>(new BoundedChannelOptions(capacity: 500)
{
    FullMode = BoundedChannelFullMode.Wait
});

public static Task RunOcrWorkersAsync(
    ChannelReader<DocumentMessage> reader,
    IOcrProcessor processor,
    int degreeOfParallelism,
    CancellationToken ct)
{
    var options = new ParallelOptions
    {
        MaxDegreeOfParallelism = degreeOfParallelism,
        CancellationToken = ct
    };

    return Parallel.ForEachAsync(reader.ReadAllAsync(ct), options, async (message, token) =>
    {
        // Idempotency: duplicate deliveries do not result in reprocessing
        if (await processor.IsAlreadyProcessedAsync(message.JobId, token))
        {
            return;
        }

        await processor.ProcessAsync(message, token);
    });
}

Since OCR is CPU-intensive, a good starting point for the degree of parallelism is the number of cores in the container. From there, adjust by looking at actual metrics for latency and resource usage.

Image pre-processing

Poor image quality yields poor text, plain and simple. Before OCR, it is worthwhile to convert to grayscale, correct orientation, enhance contrast, binarize, remove noise, and straighten skewed pages (deskew). In .NET, ImageSharp covers the basics without any native dependencies; for more advanced operations, such as deskew, you can resort to OpenCV via OpenCvSharp.

using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

public static async Task<Stream> PreprocessAsync(Stream input, CancellationToken ct)
{
    // L8 loads the image directly in grayscale (8 bits)
    using var image = await Image.LoadAsync<L8>(input, ct);

    image.Mutate(ctx => ctx
        .AutoOrient()
        .Contrast(1.2f)1
        .BinaryThreshold(0.5f));

    var output = new MemoryStream();
    await image.SaveAsPngAsync(output, ct);
    output.Position = 0;
    return output;
}

Image Pre-processing Example:

GIF Image from the Article

Putting Machine Learning in the Pipeline

What is this document?

With the text in hand, the next step is to figure out what type of document it is: an invoice, a contract, a receipt? This answer defines which field extractor will go into action. With ML.NET, you can train and run a multiclass classifier within the .NET process itself (AHMED et al., 2019):

public sealed class OcrDocument
{
    public string OcrText { get; set; } = string.Empty;
    public string DocumentType { get; set; } = string.Empty;
}

public sealed class DocumentTypePrediction
{
    [ColumnName("PredictedLabel")]
    public string DocumentType { get; set; } = string.Empty;
}

var mlContext = new MLContext(seed: 42);
IDataView trainingData = mlContext.Data.LoadFromEnumerable(labeledDocuments);

var pipeline = mlContext.Transforms.Conversion
        .MapValueToKey("Label", nameof(OcrDocument.DocumentType))
    .Append(mlContext.Transforms.Text.FeaturizeText("Features", nameof(OcrDocument.OcrText)))
    .Append(mlContext.MulticlassClassification.Trainers.SdcaMaximumEntropy())
    .Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));

var model = pipeline.Fit(trainingData);

var engine = mlContext.Model.CreatePredictionEngine<OcrDocument, DocumentTypePrediction>(model);
var prediction = engine.Predict(new OcrDocument { OcrText = extractedText });

A warning: PredictionEngine is not thread-safe. In APIs and web services, use PredictionEnginePool from the Microsoft.Extensions.ML package.

Tables, layouts, and external models

To understand tables and forms, it's not enough to just read the words in sequence: the model needs to know where each piece of text is on the page. These models are usually trained in Python and exported to ONNX, running on .NET via ONNX Runtime. Another option is to publish them as dedicated microservices on GPU, accessed via gRPC. The right choice depends on how much you can spend on hardware and how much latency each step can handle.

Observability and Autoscaling

Metrics with OpenTelemetry

Without per-stage metrics, you don't know where the bottleneck is, and scaling becomes a guessing game. At a minimum, track latency by document type, failure rate per stage, and queue size. These are the signals that will support your SLOs and alerts (BEYER et al., 2016). In .NET 10, the System.Diagnostics.Metrics API talks directly to OpenTelemetry, and you can export data to Prometheus, Azure Monitor, or any backend compatible with OTLP:

using System.Diagnostics.Metrics;

public sealed class OcrMetrics
{
    public const string MeterName = "Leaders.Ocr";

    private readonly Histogram<double> _duration;
    private readonly Counter<long> _failures;

    public OcrMetrics(IMeterFactory meterFactory)
    {
        var meter = meterFactory.Create(MeterName);
        _duration = meter.CreateHistogram<double>("ocr.document.duration", unit: "ms");
        _failures = meter.CreateCounter<long>("ocr.document.failures");
    }

    public void RecordSuccess(TimeSpan duration, string documentType)
        => _duration.Record(duration.TotalMilliseconds, new KeyValuePair<string, object?>("document.type", documentType));

    public void RecordFailure(string stage)
        => _failures.Add(1, new KeyValuePair<string, object?>("ocr.stage", stage));
}

// Program.cs
builder.Services.AddSingleton<OcrMetrics>();
builder.Services.AddOpenTelemetry()
    .WithMetrics(metrics => metrics
        .AddMeter(OcrMetrics.MeterName)
        .AddOtlpExporter());

Scaling by the queue

For OCR workers, CPU usage is not always the best indicator of when to scale. The queue size gives a much clearer picture of how much work is waiting. In Kubernetes, you can combine the Horizontal Pod Autoscaler with KEDA and scale pods according to the queue in Service Bus, RabbitMQ, or Kafka, going down to zero replicas when there is nothing to process.

Security, Compliance, and Governance

Encryption and Anonymization

Data needs to be protected both at rest, with keys managed by services like Azure Key Vault or HashiCorp Vault, and in transit, with TLS 1.2 or higher. And before indexing anything, mask personal identifiers: this is the LGPD’s minimization principle in practice (BRAZIL, 2018). For a first layer of protection, a compile-time generated regular expression already helps a lot:

using System.Text.RegularExpressions;

public static partial class PiiMasker
{
    // Accepts formatted CPF (000.000.000-00) or just digits
    [GeneratedRegex(@"\b\d{3}\.?\d{3}\.?\d{3}-?\d{2}\b")]
    private static partial Regex CpfRegex();

    public static string MaskCpf(string text)
        => CpfRegex().Replace(text, "***.***.***-**");
}

Just don’t rely only on regex: it generates false positives and won’t catch names or addresses. In production, combine verifying check digits with entity recognition models (NER) trained for your domain.

Auditing and Traceability

Each step must record who processed the document, with which model version, and what the result was. This audit trail is what enables accountability, as required by the LGPD (BRAZIL, 2018). And there’s even a bonus: when a model is updated, you know exactly which documents need to be reprocessed.

Storage, Search and Delivery

Indexing for search

The original files are stored in an object storage (Azure Blob Storage or Amazon S3), while the already anonymized text and metadata are sent to a search engine. If you’re still using NEST, here’s a tip: it was discontinued as of Elasticsearch 8, and the official client now is Elastic.Clients.Elasticsearch:

using Elastic.Clients.Elasticsearch;

public sealed record OcrIndexedDocument(
    Guid Id,
    string FileName,
    string DocumentType,
    string MaskedText,
    DateTimeOffset ProcessedAt);

var settings = new ElasticsearchClientSettings(new Uri("https://localhost:9200"))
    .DefaultIndex("ocr-documents");
var client = new ElasticsearchClient(settings);

var document = new OcrIndexedDocument(jobId, fileName, documentType, PiiMasker.MaskCpf(ocrText), DateTimeOffset.UtcNow);
var response = await client.IndexAsync(document, ct);

if (!response.IsValidResponse)
{
    throw new InvalidOperationException($"Failed to index document {jobId}.");
}

Integration with legacy systems

The result can reach the client by status query (polling), webhooks, or published events on the bus. When the destination is an ERP or another legacy system, an anti-corruption layer translates the pipeline model to the contracts that already exist. This way, internal changes do not affect those who consume the data (RICHARDSON, 2018).

In Practice: A High-Volume Multitenant Pipeline

The scenario

To put all this into practice (pun intended), imagine a financial group needing to process contracts and receipts from multiple companies. The goals are: 1 million documents per day, peaks of 10,000 documents per minute, an average latency of 2 seconds per document, table extraction, anonymization, and complete audit trail. The scenario is illustrative, but the numbers help clarify the decisions.

How many workers are needed?

Little’s Law provides the answer: the required concurrency is the arrival rate multiplied by latency. At peak, 10,000 documents per minute is about 167 per second. With 2 seconds per document, the system needs to handle around 334 concurrent processes. With workers of 8 parallelism slots, that’s about 42 OCR pods at peak, against just a few replicas during normal operation. Keeping all these running all the time would be wasteful, and that’s why scaling by the queue makes so much sense.

Architectural decisions

  • Tenant isolation: each client has their own queues and storage prefixes, so a tenant with huge volume doesn’t impact the others.
  • SLA-based priority: urgent documents and batch processing go to different queues.
  • GPU only where it matters: layout and NER models run in dedicated pools accessed via gRPC; classic OCR stays on CPU.
  • Dead-letter queues: documents that repeatedly fail are separated for analysis, without blocking the rest of the flow.

Risks and how to protect yourself

The main risks here are the enormous variety of layouts and the silent loss of model quality over time. To protect yourself, classify the document before extracting the fields, maintain validation sets for each document type, and monitor the distribution of input data to notice when it starts to change (SCULLEY et al., 2015).

Best Practices and Lessons Learned

1. Treat models as code

Models, pipelines, and infrastructure deserve versioning and deployment via CI/CD, with Infrastructure as Code (Terraform, Bicep, or Helm). Machine learning systems quickly accumulate technical debt when data dependencies and configurations are left implicit (SCULLEY et al., 2015).

2. Test with real documents

Unit and integration tests are important, but not enough. Build a set of reference documents with the expected text and measure the character error rate with each new version. This way you catch those accuracy drops that no functional test would show.

3. Plan for failures from day one

Timeouts, retries with exponential backoff, idempotency, and dead-letter queues need to be part of the design from the start, not appear hastily after the first incident (KLEPPMANN, 2017).

Conclusion

In the end, large-scale OCR is much more of a distributed architecture problem than a text recognition one. And .NET 10 delivers the right pieces to solve it: asynchronous, parallel, and observable pipelines, bringing together classic OCR, layout models, and machine learning in the same ecosystem.

Asynchronous ingestion, backpressure, idempotency, scaling through the queue, and anonymization before indexing: with these ingredients, processing documents stops being a headache and becomes a predictable and auditable capability. And the future is promising: multimodal models, which understand text, image, and layout at the same time, should increasingly reduce the need for specific rules for each document type. If you are just getting started, do not try to build everything at once: start with the queue and idempotency, measure everything, and evolve from there.

References

  • AHMED, Zeeshan et al. Machine learning at Microsoft with ML.NET. In: ACM SIGKDD INTERNATIONAL CONFERENCE ON KNOWLEDGE DISCOVERY & DATA MINING, 25th, 2019, Anchorage. Accessed on: Oct. 7, 2026. reference.Description
  • BEYER, Betsy et al. Site reliability engineering: how Google runs production systems. Sebastopol: O'Reilly Media, 2016. Available at: Google Scholar. Accessed on: Oct. 7, 2026. reference.Description
  • BRAZIL. Law No. 13,709, of August 14, 2018. General Data Protection Law (LGPD). Official Gazette of the Union, Brasília, DF, Aug. 15, 2018. Accessed on: Oct. 7, 2026. reference.Description
  • KLEPPMANN, Martin. Designing data-intensive applications: the big ideas behind reliable, scalable, and maintainable systems. Sebastopol: O'Reilly Media, 2017. Accessed on: Oct 7, 2026. reference.Description
  • NEWMAN, Sam. Building microservices: designing fine-grained systems. 2nd ed. Sebastopol: O'Reilly Media, 2021. Accessed on: Oct 7, 2026. reference.Description
  • RICHARDSON, Chris. Microservices patterns: with examples in Java. Shelter Island: Manning, 2018. Accessed on: Oct. 7, 2026. reference.Description
  • SCULLEY, D. et al. Hidden technical debt in machine learning systems. In: ADVANCES IN NEURAL INFORMATION PROCESSING SYSTEMS, 28th, 2015, Montreal. Proceedings [...]. Red Hook: Curran Associates, 2015. p. 2503-2511. Accessed on: Oct. 7, 2026. reference.Description
About the author