using IronOcr;
string imageText = new IronTesseract().Read(@"images\image.png").Text;
Install-Package IronOcr
PDF OCR Text Extraction
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// OCR entire document
ocrInput.LoadPdf("example.pdf", Password: "password");
int[] pages = { 1, 2, 3, 4, 5 };
// Alternatively OCR selected page numbers
ocrInput.LoadPdfPages("example.pdf", pages, Password: "password");
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
OCR with Barcode & QR Reading
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
ocrTesseract.Configuration.ReadBarCodes = true;
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\imageWithBarcode.png");
var ocrResult = ocrTesseract.Read(ocrInput);
foreach (var barcode in ocrResult.Barcodes)
{
Console.WriteLine(barcode.Value);
}
Install-Package IronOcr
125 International OCR Languages
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
ocrTesseract.Language = OcrLanguage.Arabic;
using (var ocrInput = new OcrInput())
{
ocrInput.LoadImage(@"images\arabic.gif");
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
}
// Example with a Custom Trained Font Being used:
var ocrTesseractCustomerLang = new IronTesseract();
ocrTesseractCustomerLang.UseCustomTesseractLanguageFile("custom_tesseract_files/custom.traineddata");
ocrTesseractCustomerLang.AddSecondaryLanguage(OcrLanguage.EnglishBest);
using (var ocrInput = new OcrInput())
{
ocrInput.LoadPdf(@"images\mixed-lang.pdf");
var ocrResult = ocrTesseractCustomerLang.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
}
Install-Package IronOcr
Fixing Low Quality Scans & Images
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\image.png");
ocrInput.Deskew();
ocrInput.DeNoise();
ocrInput.Despeckle();
ocrInput.EnhanceResolution(225);
ocrInput.Sharpen();
ocrInput.Erode();
ocrInput.Dilate();
ocrInput.Scale(200);
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
Fast OCR Configuration
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
// Fast Dictionary
ocrTesseract.Language = OcrLanguage.EnglishFast;
// Turn off unneeded options
ocrTesseract.Configuration.ReadBarCodes = false;
// Assume text is laid out neatly in an orthogonal document
ocrTesseract.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.Auto;
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\image.png");
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
OCR Image Optimization Filters
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// First load all image(s)
ocrInput.LoadImage(@"images\image.png");
// Note: You don't need all of them; most users only need Deskew() and occasionally DeNoise()
ocrInput.WithTitle("My Document");
ocrInput.Binarize();
ocrInput.Contrast();
ocrInput.Deskew();
ocrInput.DeNoise();
ocrInput.Despeckle();
ocrInput.Dilate();
ocrInput.EnhanceResolution(300);
ocrInput.Invert();
ocrInput.Rotate(90);
ocrInput.Scale(150);
ocrInput.Sharpen();
ocrInput.ToGrayScale();
ocrInput.Erode();
// WIZARD - If you are unsure use the debug-wizard to test all combinations:
string codeToRun = OcrInputFilterWizard.Run(@"images\image.png", out double confidence, ocrTesseract);
Console.WriteLine(codeToRun);
// Optional: Export modified images so you can view them.
foreach (var page in ocrInput.GetPages())
{
page.SaveAsImage($"filtered_{page.Index}.bmp");
}
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
OCR Image Color Editing
using IronOcr;
using IronSoftware.Drawing;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.LoadImage("blue_and_pink.png");
ocrInput.WithTitle("Recolored");
ocrInput.ReplaceColor(Color.Pink, Color.White, 10);
// Pink detection has 10% tolerance
ocrInput.ReplaceColor(Color.Blue, Color.Black, 5);
// Blue detection has 5% tolerance
// Export the modified image so you can manually inspect it.
foreach (var page in ocrInput.GetPages())
{
page.SaveAsImage($"black_and_white_page_{page.Index}.bmp");
}
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
OcrResult Class
using IronOcr;
using IronSoftware.Drawing;
// We can delve deep into OCR results as an object model of
// Pages, Barcodes, Paragraphs, Lines, Words and Characters
// This allows us to explore, export and draw OCR content using other APIs/
var ocrTesseract = new IronTesseract();
ocrTesseract.Configuration.ReadBarCodes = true;
using var ocrInput = new OcrInput();
var pages = new int[] { 1, 2 };
ocrInput.LoadImageFrames("example.tiff", pages);
OcrResult ocrResult = ocrTesseract.Read(ocrInput);
foreach (var page in ocrResult.Pages)
{
// Page object
int PageNumber = page.PageNumber;
string PageText = page.Text;
int PageWordCount = page.WordCount;
// null if we dont set Ocr.Configuration.ReadBarCodes = true;
OcrResult.Barcode[] Barcodes = page.Barcodes;
AnyBitmap PageImage = page.ToBitmap(ocrInput);
double PageWidth = page.Width;
double PageHeight = page.Height;
double PageRotation = page.Rotation; // angular correction in degrees from OcrInput.Deskew()
foreach (var paragraph in page.Paragraphs)
{
// Pages -> Paragraphs
int ParagraphNumber = paragraph.ParagraphNumber;
string ParagraphText = paragraph.Text;
AnyBitmap ParagraphImage = paragraph.ToBitmap(ocrInput);
int ParagraphX_location = paragraph.X;
int ParagraphY_location = paragraph.Y;
int ParagraphWidth = paragraph.Width;
int ParagraphHeight = paragraph.Height;
double ParagraphOcrAccuracy = paragraph.Confidence;
OcrResult.TextFlow paragrapthText_direction = paragraph.TextDirection;
foreach (var line in paragraph.Lines)
{
// Pages -> Paragraphs -> Lines
int LineNumber = line.LineNumber;
string LineText = line.Text;
AnyBitmap LineImage = line.ToBitmap(ocrInput);
int LineX_location = line.X;
int LineY_location = line.Y;
int LineWidth = line.Width;
int LineHeight = line.Height;
double LineOcrAccuracy = line.Confidence;
double LineSkew = line.BaselineAngle;
double LineOffset = line.BaselineOffset;
foreach (var word in line.Words)
{
// Pages -> Paragraphs -> Lines -> Words
int WordNumber = word.WordNumber;
string WordText = word.Text;
AnyBitmap WordImage = word.ToBitmap(ocrInput);
int WordX_location = word.X;
int WordY_location = word.Y;
int WordWidth = word.Width;
int WordHeight = word.Height;
double WordOcrAccuracy = word.Confidence;
foreach (var character in word.Characters)
{
// Pages -> Paragraphs -> Lines -> Words -> Characters
int CharacterNumber = character.CharacterNumber;
string CharacterText = character.Text;
AnyBitmap CharacterImage = character.ToBitmap(ocrInput);
int CharacterX_location = character.X;
int CharacterY_location = character.Y;
int CharacterWidth = character.Width;
int CharacterHeight = character.Height;
double CharacterOcrAccuracy = character.Confidence;
// Output alternative symbols choices and their probability.
// Very useful for spellchecking
OcrResult.Choice[] Choices = character.Choices;
}
}
}
}
}
Install-Package IronOcr
Create Searchable PDFs by OCR
using IronOcr;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\page1.png");
ocrInput.LoadImage(@"images\page2.bmp");
var page = new int[]{ 2, 3 };
ocrInput.LoadImageFrames(@"images\page3.tiff", page);
ocrInput.Deskew();
var ocrResult = ocrTesseract.Read(ocrInput);
ocrResult.SaveAsSearchablePdf("searchable.pdf");
Install-Package IronOcr
Tesseract 5 for .NET
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
// This is done by default and can be omitted:
// ocrTesseract.Configuration.TesseractVersion = TesseractVersion.Tesseract5;
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\image.png");
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
Tesseract Detailed Configuration
using IronOcr;
using System;
var ocrTesseract = new IronTesseract()
{
Language = OcrLanguage.EnglishBest,
Configuration = new TesseractConfiguration()
{
ReadBarCodes = false,
RenderHocr = true,
BlackListCharacters = "`ë|^",
PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd,
}
};
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\image.png");
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
OcrInput Class
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\image.png");
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
OCR a Region of an Image
using IronOcr;
using IronSoftware.Drawing;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// A 41% improvement on speed by specifiying a pixel region
var ContentArea = new Rectangle(x: 215, y: 1250, width: 1335, height: 280);
ocrInput.LoadImage("img/example.png", ContentArea);
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
TIFF to Searchable PDF Converter
using IronOcr;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.LoadImageFrame("images/multiframe.tiff", 1);
var ocrResult = ocrTesseract.Read(ocrInput);
ocrResult.SaveAsSearchablePdf("searchable.pdf");
Install-Package IronOcr
Image Resolution Optimization (DPI)
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.TargetDPI = 300;
ocrInput.LoadImage(@"images\image.png");
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
MultithreadedTesseract OCR
using IronOcr;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.LoadPdf("scan.pdf");
// Image processing is automatically multi-threaded
ocrInput.Deskew();
// OCR reading is automatically multi-threaded too
var ocrResult = ocrTesseract.Read(ocrInput);
Install-Package IronOcr
OCR Progress and Performance Tracking
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
ocrTesseract.OcrProgress += (_, ocrProgressEventsArgs) =>
{
Console.WriteLine(ocrProgressEventsArgs.ProgressPercent + "% " + ocrProgressEventsArgs.Duration.TotalSeconds + "s");
};
using var input = new OcrInput();
input.LoadPdf("large.pdf");
// Progress events will fire during the read operation even if the main thread is blocked.
var result = ocrTesseract.Read(input);
Install-Package IronOcr
OCR for MultiPage TIFF Files
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
var pages = new int[] { 1, 2 };
ocrInput.LoadImageFrames("images/multiframe.tiff", pages);
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
Make any PDF have Searchable, Copyable Text
using IronOcr;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.LoadPdf("scan.pdf", Password: "password");
// Clean up twisted pages
ocrInput.Deskew();
var ocrResult = ocrTesseract.Read(ocrInput);
ocrResult.SaveAsSearchablePdf("searchable.pdf");
Install-Package IronOcr
Using Custom Tesseract Language Files
using IronOcr;
using System;
var ocrTesseract = new IronTesseract();
ocrTesseract.UseCustomTesseractLanguageFile("custom_tesseract_files/custom.traineddata");
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\image.png");
var ocrResult = ocrTesseract.Read(ocrInput);
Console.WriteLine(ocrResult.Text);
Install-Package IronOcr
Multiple Languages for 1 Document
//PM> Install-Package IronOcr.Languages.Arabic
//PM> Install-Package IronOcr.Languages.Chinese
using IronOcr;
using System;
var Ocr = new IronTesseract();
// Add a primary language (Default is English)
Ocr.Language = OcrLanguage.English;
// Add as many secondary languages as you like
Ocr.AddSecondaryLanguage(OcrLanguage.Arabic);
Ocr.AddSecondaryLanguage(OcrLanguage.ChineseSimplified);
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\image.png");
var Result = Ocr.Read(ocrInput);
Console.WriteLine(Result.Text);
Install-Package IronOcr
Exporting Images of OCR Elements
using IronOcr;
using IronSoftware.Drawing;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
ocrInput.LoadImage(@"images\image.png");
var ocrResult = ocrTesseract.Read(ocrInput);
foreach (var page in ocrResult.Pages)
{
foreach (var word in page.Words)
{
word.ToBitmap(ocrInput).SaveAs($"page{page.PageNumber}_word{word.WordNumber}.png", AnyBitmap.ImageFormat.Png);
}
}