IRONSOFTWAREHOME
USING IRONOCR

OCR in C# CodeProject Tutorial: Extract Text from Images with IronOCR

Kannaopat Udonpant
Kannapat Udonpant
Updated: June 28, 2026

Optical character recognition (OCR) in C# lets you extract machine-readable text from scanned documents, image files, and TIFF files inside .NET applications. With IronOCR, a .NET-native OCR library, you install one NuGet package and start reading text from images in a few lines of code -- no external service, no runtime dependency, no per-call API fee.

Start your free trial of IronOCR to follow along with the code samples below.

How Do You Install IronOCR in a .NET Project?

The fastest way to add OCR to a .NET 10 project is through the NuGet Package Manager. Open a terminal in your project directory and run the .NET CLI command, or use the Package Manager Console inside Visual Studio:

PM > Install-Package IronOcr

After installation, the NuGet package manager downloads all required assemblies and wires up references automatically. IronOCR targets .NET Framework 4.6.2+, .NET Core 3.1+, and .NET 5 through .NET 10, so it works across console apps, ASP.NET Core services, WPF applications, and Azure Functions.

You do not need to register a license key to test locally -- a trial watermark appears on output until a license is applied. Add the using directive and, when you are ready for production, pass your key once at startup:

using IronOcr;

// Apply license key before any OCR calls (production only)
IronOcr.License.LicenseKey = "YOUR-LICENSE-KEY";

See the IronOCR licensing page for pricing and activation details.

How Do You Extract Text from an Image File?

The core OCR workflow involves three objects: IronTesseract (the engine), OcrInput (the input container), and OcrResult (the output). The sample below reads a PNG and prints the recognized text to the console.

using IronOcr;

var ocr = new IronTesseract();

using var input = new OcrInput();
input.LoadImage("sample-document.png");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

Optical Character Recognition Output

OCR in C# CodeProject Tutorial: Extract Text from Images with IronOCR: Image 1 - Screenshot of OCR output

IronTesseract wraps the Tesseract 5 engine with .NET-friendly defaults and automatic model management. OcrInput.LoadImage accepts PNG, JPEG, BMP, GIF, TIFF, and WebP files, so you rarely need to convert formats before passing an image to the engine.

The OcrResult.Text property returns a plain string of all recognized characters joined by newlines. For richer access -- word bounding boxes, confidence scores, per-paragraph text -- navigate the result.Pages, result.Paragraphs, result.Words, and result.Characters collections.

Key properties worth knowing:

  • result.Pages[0].Text -- text from a single page
  • result.Words[n].Text and result.Words[n].Confidence -- per-word accuracy (0.0 -- 1.0)
  • result.Pages[0].Paragraphs -- paragraph segmentation for structured extraction

You can also call ocr.ReadAsync(input) to keep the UI thread free in desktop or web applications.

How Do You Process Scanned Documents and TIFF Files?

Multi-page TIFF files are common in document scanning workflows. IronOCR handles them with LoadImageFrames, which lets you choose exactly which frames (pages) to process -- useful when you only need a subset of a large archive.

using IronOcr;

var ocr = new IronTesseract();

using var input = new OcrInput();
int[] pageIndices = { 0, 1, 2 };
input.LoadImageFrames("scanned-documents.tiff", pageIndices);

// Correct skew and remove noise before reading
input.Deskew();
input.DeNoise();

OcrResult result = ocr.Read(input);

foreach (var page in result.Pages)
{
    Console.WriteLine($"Page {page.PageNumber}:");
    Console.WriteLine(page.Text);
}

OCR Output from Multi-Paged TIFF File

OCR in C# CodeProject Tutorial: Extract Text from Images with IronOCR: Image 2 - Multi-paged TIFF OCR output

Deskew rotates the image to correct any tilt introduced by flatbed scanners. DeNoise removes speckles and JPEG artifacts that confuse the Tesseract engine. Together, these two preprocessing filters significantly improve recognition accuracy on low-quality scans.

Additional OcrInput filters available for difficult source material:

  • input.Sharpen() -- increases edge contrast for blurry images
  • input.Binarize() -- converts to black-and-white for fax-quality documents
  • input.Scale(200) -- upscales small images for better character separation
  • input.Rotate(90) -- corrects rotated document orientations

See the IronOCR image filters guide for a full list of preprocessing options and when to apply them.

How Do You Configure Language Support for OCR?

By default, IronOCR reads English text. To process documents in other languages, install the matching language NuGet package and set the Language property on the IronTesseract instance.

dotnet add package IronOcr.Languages.German, IronOcr.Languages.French, IronOcr.Languages.Japanese

Then configure the engine and, for bilingual documents, add a secondary language:

using IronOcr;
using IronOcr.Languages;

var ocr = new IronTesseract();
ocr.Language = OcrLanguage.German;

// For bilingual documents (e.g. Canadian forms, EU directives)
ocr.AddSecondaryLanguage(OcrLanguage.French);

using var input = new OcrInput();
input.LoadImage("german-invoice.png");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

IronOCR supports over 125 languages, each distributed as a separate lightweight NuGet package. This keeps your production binary small -- only the language data your application actually needs is included. The engine blends primary and secondary language models during recognition when you call AddSecondaryLanguage.

How Do You Handle OCR Errors and Improve Recognition Results?

Production applications need error handling around the OCR pipeline. Image quality issues, missing files, or unsupported formats can cause exceptions. Wrapping the call in a try/catch block gives you a clean recovery path.

using IronOcr;

var ocr = new IronTesseract();
ocr.Language = OcrLanguage.English;

try
{
    using var input = new OcrInput();
    input.LoadImage("document.png");
    input.DeNoise();
    input.Deskew();

    OcrResult result = ocr.Read(input);

    if (result.Text.Length > 0)
    {
        Console.WriteLine("Recognised text:");
        Console.WriteLine(result.Text);
    }
    else
    {
        Console.WriteLine("No text was detected in the image.");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"OCR error: {ex.Message}");
}

A few additional settings that help when accuracy is lower than expected:

  • ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto -- lets Tesseract choose between single-column, multi-column, and single-word layouts automatically
  • ocr.Configuration.ReadBarCodes = false -- disables barcode detection if you are processing text-only documents and want faster throughput
  • ocr.Configuration.TesseractVersion = TesseractVersion.Tesseract5 -- ensures you use the fastest available engine

For structured forms where fields appear at predictable positions, use region-based OCR to read only the areas that matter:

using IronOcr;
using IronSoftware.Drawing;

var ocr = new IronTesseract();

using var input = new OcrInput();
var region = new CropRectangle(x: 50, y: 200, width: 600, height: 100);
input.LoadImage("form.png", region);

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);

Limiting recognition to a crop rectangle reduces processing time by up to 90 percent on large images. This technique is well-suited for invoice number extraction, form field reading, and ID document scanning. More details are available in the region OCR how-to guide.

How Do You Create a Searchable PDF from Recognized Text?

Converting scanned image archives into searchable PDF files is one of the highest-value OCR use cases. The resulting file preserves the original visual appearance while embedding an invisible text layer that PDF viewers, search engines, and screen readers can index.

using IronOcr;

var ocr = new IronTesseract();

using var input = new OcrInput();
input.Title = "Quarterly Report Q1 2026";
input.LoadImage("page1.png");
input.LoadImage("page2.png");
input.LoadImage("page3.png");

OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable-output.pdf");

Console.WriteLine("Searchable PDF created.");
Console.WriteLine($"Pages processed: {result.Pages.Count}");

Output Searchable PDF Document

OCR in C# CodeProject Tutorial: Extract Text from Images with IronOCR: Image 3 - Searchable PDF created from input images

SaveAsSearchablePdf writes a PDF/A-compatible file where each recognized word is placed at the exact pixel coordinates of the original image. Adobe Acrobat, Preview on macOS, and Foxit Reader all support full-text search in these files immediately after generation.

For web-based document viewers or downstream NLP pipelines, use result.SaveAsHocrFile("output.hocr") instead. The hOCR format is an open XML standard that encodes per-word bounding boxes alongside the text, enabling client-side highlight-on-search and word-level accessibility annotations.

Additional output formats available from OcrResult:

  • result.SaveAsHocrFile("output.hocr") -- hOCR XML with positional data
  • result.ToXDocument() -- LINQ-queryable XDocument for programmatic processing
  • result.Pages[0].Text -- plain text per page for streaming pipelines

For applications that already work with IronPDF you can pipe OcrResult directly into PDF generation workflows, combining OCR extraction with PDF editing in a single .NET process.

How Do You Read Barcodes Alongside Text?

IronOCR can read barcodes and QR codes embedded in the same image as printed text, eliminating the need to run a separate barcode library. Enable the feature with one configuration property:

using IronOcr;

var ocr = new IronTesseract();
ocr.Configuration.ReadBarCodes = true;

using var input = new OcrInput();
input.LoadImage("shipping-label.png");

OcrResult result = ocr.Read(input);

Console.WriteLine("Text:");
Console.WriteLine(result.Text);

Console.WriteLine("Barcodes:");
foreach (var barcode in result.Barcodes)
{
    Console.WriteLine($"  {barcode.Format}: {barcode.Value}");
}

Supported barcode formats include Code 128, Code 39, EAN-13, EAN-8, UPC-A, UPC-E, PDF417, Data Matrix, and QR Code. Full details are in the IronOCR barcode reading guide.

This capability is particularly useful in logistics, healthcare, and retail applications where shipping labels, patient wristbands, and product tags carry both human-readable text and machine-readable barcodes.

How Do You Compare IronOCR with Other .NET OCR Options?

Developers evaluating OCR libraries for .NET typically consider IronOCR, Tesseract.NET, and cloud services such as Google Cloud Vision or Azure Computer Vision. The table below summarizes the key differences:

Comparison of .NET OCR options across key developer criteria
CriterionIronOCRTesseract.NETAzure Computer Vision
DeploymentOn-premise or cloud, no external callsOn-premiseCloud-only, requires internet
InstallationSingle NuGet packageMultiple packages + native binariesSDK + Azure subscription
Language packs125+ via NuGet packagesManual tessdata downloadManaged by Azure
Searchable PDF outputBuilt-in one method callNot includedNot included
Image preprocessing12+ built-in filtersManual pre-processing requiredAutomatic (server-side)
Pricing modelOne-time perpetual licenseOpen source (Apache 2.0)Per-call billing

Tesseract, maintained by Google as an open-source project, powers both IronOCR and Tesseract.NET under the hood. IronOCR adds .NET-idiomatic packaging, automatic model management, and the production output features (searchable PDF, hOCR export) that raw Tesseract bindings lack. Azure Computer Vision provides state-of-the-art cloud accuracy but introduces network latency and per-call costs that are unsuitable for high-volume or offline workflows.

For scenarios where data privacy regulations prohibit sending documents to external services -- healthcare records, legal documents, financial statements -- an on-premise library like IronOCR is the appropriate choice.

What Are Your Next Steps?

You now have the building blocks to add OCR to any .NET 10 application: installation via NuGet, basic image-to-text extraction, multi-page TIFF processing, language configuration, error handling, region-based reading, barcode detection, and searchable PDF generation.

To go deeper, explore these IronOCR resources:

For licensing questions or to deploy IronOCR in a production environment, visit the IronOCR licensing page. A free trial license removes output watermarks during your evaluation period, and Iron Software's support team is available for technical questions at any tier.

First Step:
arrow pointer

Related Articles

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required