這個 Iron Tesseract C# 範例展示了使用 IronOCR 從圖像中讀取文字的最簡單方法。 IronOcr.IronTesseract 類提取文字並將其作為字串返回。
using IronOcr;IronTesseract ocr = new IronTesseract();using OcrInput input = new OcrInput();var pageindices = new int[] { 1, 2 };input.LoadImageFrames(@"img\Potter.LowQuality.tiff", pageindices);input.Deskew(); // removes rotation and perspectiveOcrResult result = ocr.Read(input);Console.WriteLine(result.Text);
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.LowQuality.tiff", pageindices);
input.Deskew(); // removes rotation and perspective
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
ImportsIronOcrPrivate ocr As New IronTesseract()PrivateOcrInputAsusingPrivate pageindices = New Integer() { 1, 2 }input.LoadImageFrames("img\Potter.LowQuality.tiff", pageindices)input.Deskew() ' removes rotation and perspectiveDim result AsOcrResult = ocr.Read(input)Console.WriteLine(result.Text)
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
Private pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img\Potter.LowQuality.tiff", pageindices)
input.Deskew() ' removes rotation and perspective
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
這段程式碼在清晰圖片上達到 100% 的準確性,提取文字時完全符合圖片顯示:
IronOCR Simple ExampleIn this simple example we test the accuracy of our C# OCR library to read text from a PNG Image. This is a very basic test, but things will get more complicated as the tutorial continues.The quick brown fox jumps over the lazy dog
IronOCR Simple Example
In this simple example we test the accuracy of our C# OCR library to read text from a PNG Image. This is a very basic test, but things will get more complicated as the tutorial continues.
The quick brown fox jumps over the lazy dog
Text
IronTesseract 類在內部處理復雜的 OCR 操作。 它自動掃描對齊,優化解析度,使用 AI 從圖像中讀取文字,使用 IronOCR 具有媲美人類的準確性。
using IronOcr;using IronSoftware.Drawing;IronTesseract ocr = new IronTesseract();using OcrInput input = new OcrInput();// restrict OCR to a content area for faster processingRectangle contentArea = new Rectangle(x: 215, y: 1250, height: 280, width: 1335);input.LoadImage("img/ComSci.png", contentArea);OcrResult result = ocr.Read(input);Console.WriteLine(result.Text);
using IronOcr;
using IronSoftware.Drawing;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
// restrict OCR to a content area for faster processing
Rectangle contentArea = new Rectangle(x: 215, y: 1250, height: 280, width: 1335);
input.LoadImage("img/ComSci.png", contentArea);
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
// PM> Install IronOcr.Languages.Arabicusing IronOcr;IronTesseract ocr = new IronTesseract();ocr.Language = OcrLanguage.Arabic;using OcrInput input = new OcrInput();input.LoadImageFrame("img/arabic.gif", 1);// add image filters if needed// In this case, even thought input is very low quality// IronTesseract can read what conventional Tesseract cannot.OcrResult result = ocr.Read(input);// Console can't print Arabic on Windows easily.// Let's save to disk instead.result.SaveAsTextFile("arabic.txt");
// PM> Install IronOcr.Languages.Arabic
using IronOcr;
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.Arabic;
using OcrInput input = new OcrInput();
input.LoadImageFrame("img/arabic.gif", 1);
// add image filters if needed
// In this case, even thought input is very low quality
// IronTesseract can read what conventional Tesseract cannot.
OcrResult result = ocr.Read(input);
// Console can't print Arabic on Windows easily.
// Let's save to disk instead.
result.SaveAsTextFile("arabic.txt");
' PM> Install IronOcr.Languages.ArabicImportsIronOcrPrivate ocr As New IronTesseract()ocr.Language = OcrLanguage.ArabicUsing input As New OcrInput() input.LoadImageFrame("img/arabic.gif", 1) ' add image filters if needed ' In this case, even thought input is very low quality ' IronTesseract can read what conventional Tesseract cannot. Dim result AsOcrResult = ocr.Read(input) ' Console can't print Arabic on Windows easily. ' Let's save to disk instead. result.SaveAsTextFile("arabic.txt")EndUsing
' PM> Install IronOcr.Languages.Arabic
Imports IronOcr
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.Arabic
Using input As New OcrInput()
input.LoadImageFrame("img/arabic.gif", 1)
' add image filters if needed
' In this case, even thought input is very low quality
' IronTesseract can read what conventional Tesseract cannot.
Dim result As OcrResult = ocr.Read(input)
' Console can't print Arabic on Windows easily.
' Let's save to disk instead.
result.SaveAsTextFile("arabic.txt")
End Using
Scale() 和 EnhanceResolution() 與 SaveAsSearchablePdf() 不相容,因為在 v2025.12.3 中已知的問題。所有其他過濾器與可搜索的 PDF 輸出一起正常工作。
如何為最大速度配置 IronOCR?
使用這些設定來優化高質量掃描的處理速度:
using IronOcr;IronTesseract ocr = new IronTesseract();ocr.Language = OcrLanguage.ChineseSimplified;// We can add any number of languages.ocr.AddSecondaryLanguage(OcrLanguage.English);// Optionally add custom tesseract .traineddata files by specifying a file pathusing OcrInput input = new OcrInput();input.LoadImage("img/MultiLanguage.jpeg");OcrResult result = ocr.Read(input);result.SaveAsTextFile("MultiLanguage.txt");
using IronOcr;
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.ChineseSimplified;
// We can add any number of languages.
ocr.AddSecondaryLanguage(OcrLanguage.English);
// Optionally add custom tesseract .traineddata files by specifying a file path
using OcrInput input = new OcrInput();
input.LoadImage("img/MultiLanguage.jpeg");
OcrResult result = ocr.Read(input);
result.SaveAsTextFile("MultiLanguage.txt");
ImportsIronOcrPrivate ocr As New IronTesseract()ocr.Language = OcrLanguage.ChineseSimplified' We can add any number of languages.ocr.AddSecondaryLanguage(OcrLanguage.English)' Optionally add custom tesseract .traineddata files by specifying a file pathUsing input As New OcrInput() input.LoadImage("img/MultiLanguage.jpeg") Dim result AsOcrResult = ocr.Read(input) result.SaveAsTextFile("MultiLanguage.txt")EndUsing
Imports IronOcr
Private ocr As New IronTesseract()
ocr.Language = OcrLanguage.ChineseSimplified
' We can add any number of languages.
ocr.AddSecondaryLanguage(OcrLanguage.English)
' Optionally add custom tesseract .traineddata files by specifying a file path
Using input As New OcrInput()
input.LoadImage("img/MultiLanguage.jpeg")
Dim result As OcrResult = ocr.Read(input)
result.SaveAsTextFile("MultiLanguage.txt")
End Using
此優化設置在保持 99.8% 的準確性的同時,比預設設置提高了 35% 的速度改善。
如何使用 C# OCR 讀取特定區域的圖像?
下面的 Iron Tesseract C# 範例展示了如何使用 System.Drawing.Rectangle 針對特定區域。 此技術在處理標準化表單時非常寶貴,因為文字出現在可預測的位置。
IronOCR 能否處理裁剪區域以獲得更快的結果?
使用基於像素的座標,您可以將 OCR 限制在特定區域,大大提高速度並防止提取不需要的文字:
using IronOcr;IronTesseract ocr = new IronTesseract();using OcrInput input = new OcrInput();input.LoadImage("image1.jpeg");input.LoadImage("image2.png");var pageindices = new int[] { 1, 2 };input.LoadImageFrames("image3.gif", pageindices);OcrResult result = ocr.Read(input);Console.WriteLine($"{result.Pages.Length} Pages"); // 3 Pages
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.LoadImage("image1.jpeg");
input.LoadImage("image2.png");
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("image3.gif", pageindices);
OcrResult result = ocr.Read(input);
Console.WriteLine($"{result.Pages.Length} Pages"); // 3 Pages
ImportsIronOcrPrivate ocr As New IronTesseract()PrivateOcrInputAsusinginput.LoadImage("image1.jpeg")input.LoadImage("image2.png")Dim pageindices = New Integer() { 1, 2 }input.LoadImageFrames("image3.gif", pageindices)Dim result AsOcrResult = ocr.Read(input)Console.WriteLine($"{result.Pages.Length} Pages") ' 3 Pages
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
input.LoadImage("image1.jpeg")
input.LoadImage("image2.png")
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("image3.gif", pageindices)
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine($"{result.Pages.Length} Pages") ' 3 Pages
這種目標方法在提取相關文字同時提供 41% 的速度改善。 非常適合結構化文件,如 發票、支票和表格。 相同的裁剪技術在 PDF OCR 操作中無縫工作。
using IronOcr;IronTesseract ocr = new IronTesseract();using OcrInput input = new OcrInput();input.LoadPdf("example.pdf", Password: "password");// We can also select specific PDF page numbers to OCROcrResult result = ocr.Read(input);Console.WriteLine(result.Text);Console.WriteLine($"{result.Pages.Length} Pages");// 1 page for every page of the PDF
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.LoadPdf("example.pdf", Password: "password");
// We can also select specific PDF page numbers to OCR
OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Console.WriteLine($"{result.Pages.Length} Pages");
// 1 page for every page of the PDF
ImportsIronOcrPrivate ocr As New IronTesseract()PrivateOcrInputAsusinginput.LoadPdf("example.pdf", Password:= "password")' We can also select specific PDF page numbers to OCRDim result AsOcrResult = ocr.Read(input)Console.WriteLine(result.Text)Console.WriteLine($"{result.Pages.Length} Pages")' 1 page for every page of the PDF
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
input.LoadPdf("example.pdf", Password:= "password")
' We can also select specific PDF page numbers to OCR
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine(result.Text)
Console.WriteLine($"{result.Pages.Length} Pages")
' 1 page for every page of the PDF
using IronOcr;IronTesseract ocr = new IronTesseract();using OcrInput input = new OcrInput();input.Title = "Pdf Metadata Name";input.LoadPdf("example.pdf", Password: "password");OcrResult result = ocr.Read(input);result.SaveAsSearchablePdf("searchable.pdf");
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Pdf Metadata Name";
input.LoadPdf("example.pdf", Password: "password");
OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");
ImportsIronOcrPrivate ocr As New IronTesseract()PrivateOcrInputAsusinginput.Title = "Pdf Metadata Name"input.LoadPdf("example.pdf", Password:= "password")Dim result AsOcrResult = ocr.Read(input)result.SaveAsSearchablePdf("searchable.pdf")
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Pdf Metadata Name"
input.LoadPdf("example.pdf", Password:= "password")
Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")
using IronOcr;IronTesseract ocr = new IronTesseract();using OcrInput input = new OcrInput();input.Title = "Pdf Title";var pageindices = new int[] { 1, 2 };input.LoadImageFrames("example.tiff", pageindices);OcrResult result = ocr.Read(input);result.SaveAsSearchablePdf("searchable.pdf");
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Pdf Title";
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageindices);
OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");
ImportsIronOcrPrivate ocr As New IronTesseract()PrivateOcrInputAsusinginput.Title = "Pdf Title"Dim pageindices = New Integer() { 1, 2 }input.LoadImageFrames("example.tiff", pageindices)Dim result AsOcrResult = ocr.Read(input)result.SaveAsSearchablePdf("searchable.pdf")
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Pdf Title"
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("example.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")
高效地處理所有頁面的 TIFF 文件:
using IronOcr;IronTesseract ocr = new IronTesseract();using OcrInput input = new OcrInput();input.Title = "Html Title";// Add more content as required...input.LoadImage("image2.jpeg");input.LoadPdf("example.pdf",Password: "password");var pageindices = new int[] { 1, 2 };input.LoadImageFrames("example.tiff", pageindices);OcrResult result = ocr.Read(input);result.SaveAsHocrFile("hocr.html");
using IronOcr;
IronTesseract ocr = new IronTesseract();
using OcrInput input = new OcrInput();
input.Title = "Html Title";
// Add more content as required...
input.LoadImage("image2.jpeg");
input.LoadPdf("example.pdf",Password: "password");
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageindices);
OcrResult result = ocr.Read(input);
result.SaveAsHocrFile("hocr.html");
ImportsIronOcrPrivate ocr As New IronTesseract()PrivateOcrInputAsusinginput.Title = "Html Title"' Add more content as required...input.LoadImage("image2.jpeg")input.LoadPdf("example.pdf",Password:= "password")Dim pageindices = New Integer() { 1, 2 }input.LoadImageFrames("example.tiff", pageindices)Dim result AsOcrResult = ocr.Read(input)result.SaveAsHocrFile("hocr.html")
Imports IronOcr
Private ocr As New IronTesseract()
Private OcrInput As using
input.Title = "Html Title"
' Add more content as required...
input.LoadImage("image2.jpeg")
input.LoadPdf("example.pdf",Password:= "password")
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("example.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
result.SaveAsHocrFile("hocr.html")
將 TIFF 或 PDF 轉換為可搜索格式:
using IronOcr;IronTesseract ocr = new IronTesseract();ocr.Configuration.ReadBarCodes = true;using OcrInput input = new OcrInput();input.LoadImage("img/Barcode.png");OcrResult result = ocr.Read(input);foreach (var barcode in result.Barcodes){Console.WriteLine(barcode.Value); // type and location properties also exposed}
using IronOcr;
IronTesseract ocr = new IronTesseract();
ocr.Configuration.ReadBarCodes = true;
using OcrInput input = new OcrInput();
input.LoadImage("img/Barcode.png");
OcrResult result = ocr.Read(input);
foreach (var barcode in result.Barcodes)
{
Console.WriteLine(barcode.Value);
// type and location properties also exposed
}
ImportsIronOcrPrivate ocr As New IronTesseract()ocr.Configuration.ReadBarCodes = TrueUsing input As New OcrInput() input.LoadImage("img/Barcode.png") Dim result AsOcrResult = ocr.Read(input) For Each barcode In result.BarcodesConsole.WriteLine(barcode.Value) ' type and location properties also exposed Next barcodeEndUsing
Imports IronOcr
Private ocr As New IronTesseract()
ocr.Configuration.ReadBarCodes = True
Using input As New OcrInput()
input.LoadImage("img/Barcode.png")
Dim result As OcrResult = ocr.Read(input)
For Each barcode In result.Barcodes
Console.WriteLine(barcode.Value)
' type and location properties also exposed
Next barcode
End Using
將現有 PDF 轉換為可搜索版本:
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 APIsIronTesseract ocr = new IronTesseract();ocr.Configuration.ReadBarCodes = true;using OcrInput input = new OcrInput();var pageindices = new int[] { 1, 2 };input.LoadImageFrames(@"img\Potter.tiff", pageindices);OcrResult result = ocr.Read(input);foreach (var page in result.Pages){ // Page object int pageNumber = page.PageNumber; string pageText = page.Text; int pageWordCount = page.WordCount; // null if we don't set Ocr.Configuration.ReadBarCodes = true; OcrResult.Barcode[] barcodes = page.Barcodes; AnyBitmap pageImage = page.ToBitmap(input); System.Drawing.Bitmap pageImageLegacy = page.ToBitmap(input); double pageWidth = page.Width; double pageHeight = page.Height; foreach (var paragraph in page.Paragraphs) { // Pages -> Paragraphs int paragraphNumber = paragraph.ParagraphNumber; String paragraphText = paragraph.Text; System.Drawing.Bitmap paragraphImage = paragraph.ToBitmap(input); int paragraphXLocation = paragraph.X; int paragraphYLocation = paragraph.Y; int paragraphWidth = paragraph.Width; int paragraphHeight = paragraph.Height; double paragraphOcrAccuracy = paragraph.Confidence; var paragraphTextDirection = paragraph.TextDirection; foreach (var line in paragraph.Lines) { // Pages -> Paragraphs -> Lines int lineNumber = line.LineNumber; String lineText = line.Text; AnyBitmap lineImage = line.ToBitmap(input); System.Drawing.Bitmap lineImageLegacy = line.ToBitmap(input); int lineXLocation = line.X; int lineYLocation = 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(input); System.Drawing.Image wordImageLegacy = word.ToBitmap(input); int wordXLocation = word.X; int wordYLocation = word.Y; int wordWidth = word.Width; int wordHeight = word.Height; double wordOcrAccuracy = word.Confidence; if (word.Font != null) { // Word.Font is only set when using Tesseract Engine Modes rather than LTSM String fontName = word.Font.FontName; double fontSize = word.Font.FontSize; bool isBold = word.Font.IsBold; bool isFixedWidth = word.Font.IsFixedWidth; bool isItalic = word.Font.IsItalic; bool isSerif = word.Font.IsSerif; bool isUnderlined = word.Font.IsUnderlined; bool fontIsCaligraphic = word.Font.IsCaligraphic; } foreach (var character in word.Characters) { // Pages -> Paragraphs -> Lines -> Words -> Characters int characterNumber = character.CharacterNumber; String characterText = character.Text; AnyBitmap characterImage = character.ToBitmap(input); System.Drawing.Bitmap characterImageLegacy = character.ToBitmap(input); int characterXLocation = character.X; int characterYLocation = character.Y; int characterWidth = character.Width; int characterHeight = character.Height; double characterOcrAccuracy = character.Confidence; // Output alternative symbols choices and their probability. // Very useful for spell checking OcrResult.Choice[] characterChoices = character.Choices; } } } }}
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
IronTesseract ocr = new IronTesseract();
ocr.Configuration.ReadBarCodes = true;
using OcrInput input = new OcrInput();
var pageindices = new int[] { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageindices);
OcrResult result = ocr.Read(input);
foreach (var page in result.Pages)
{
// Page object
int pageNumber = page.PageNumber;
string pageText = page.Text;
int pageWordCount = page.WordCount;
// null if we don't set Ocr.Configuration.ReadBarCodes = true;
OcrResult.Barcode[] barcodes = page.Barcodes;
AnyBitmap pageImage = page.ToBitmap(input);
System.Drawing.Bitmap pageImageLegacy = page.ToBitmap(input);
double pageWidth = page.Width;
double pageHeight = page.Height;
foreach (var paragraph in page.Paragraphs)
{
// Pages -> Paragraphs
int paragraphNumber = paragraph.ParagraphNumber;
String paragraphText = paragraph.Text;
System.Drawing.Bitmap paragraphImage = paragraph.ToBitmap(input);
int paragraphXLocation = paragraph.X;
int paragraphYLocation = paragraph.Y;
int paragraphWidth = paragraph.Width;
int paragraphHeight = paragraph.Height;
double paragraphOcrAccuracy = paragraph.Confidence;
var paragraphTextDirection = paragraph.TextDirection;
foreach (var line in paragraph.Lines)
{
// Pages -> Paragraphs -> Lines
int lineNumber = line.LineNumber;
String lineText = line.Text;
AnyBitmap lineImage = line.ToBitmap(input);
System.Drawing.Bitmap lineImageLegacy = line.ToBitmap(input);
int lineXLocation = line.X;
int lineYLocation = 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(input);
System.Drawing.Image wordImageLegacy = word.ToBitmap(input);
int wordXLocation = word.X;
int wordYLocation = word.Y;
int wordWidth = word.Width;
int wordHeight = word.Height;
double wordOcrAccuracy = word.Confidence;
if (word.Font != null)
{
// Word.Font is only set when using Tesseract Engine Modes rather than LTSM
String fontName = word.Font.FontName;
double fontSize = word.Font.FontSize;
bool isBold = word.Font.IsBold;
bool isFixedWidth = word.Font.IsFixedWidth;
bool isItalic = word.Font.IsItalic;
bool isSerif = word.Font.IsSerif;
bool isUnderlined = word.Font.IsUnderlined;
bool fontIsCaligraphic = word.Font.IsCaligraphic;
}
foreach (var character in word.Characters)
{
// Pages -> Paragraphs -> Lines -> Words -> Characters
int characterNumber = character.CharacterNumber;
String characterText = character.Text;
AnyBitmap characterImage = character.ToBitmap(input);
System.Drawing.Bitmap characterImageLegacy = character.ToBitmap(input);
int characterXLocation = character.X;
int characterYLocation = character.Y;
int characterWidth = character.Width;
int characterHeight = character.Height;
double characterOcrAccuracy = character.Confidence;
// Output alternative symbols choices and their probability.
// Very useful for spell checking
OcrResult.Choice[] characterChoices = character.Choices;
}
}
}
}
}
ImportsIronOcrImportsIronSoftware.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 APIsPrivate ocr As New IronTesseract()ocr.Configuration.ReadBarCodes = TrueUsing input As New OcrInput() Dim pageindices = New Integer() { 1, 2 } input.LoadImageFrames("img\Potter.tiff", pageindices) Dim result AsOcrResult = ocr.Read(input) For Each page In result.Pages ' Page object Dim pageNumber AsInteger = page.PageNumber Dim pageText AsString = page.Text Dim pageWordCount AsInteger = page.WordCount ' null if we don't set Ocr.Configuration.ReadBarCodes = true; Dim barcodes() AsOcrResult.Barcode = page.Barcodes Dim pageImage AsAnyBitmap = page.ToBitmap(input) Dim pageImageLegacy AsSystem.Drawing.Bitmap = page.ToBitmap(input) Dim pageWidth AsDouble = page.Width Dim pageHeight AsDouble = page.Height For Each paragraph In page.Paragraphs ' Pages -> Paragraphs Dim paragraphNumber AsInteger = paragraph.ParagraphNumber Dim paragraphText AsString = paragraph.Text Dim paragraphImage AsSystem.Drawing.Bitmap = paragraph.ToBitmap(input) Dim paragraphXLocation AsInteger = paragraph.X Dim paragraphYLocation AsInteger = paragraph.Y Dim paragraphWidth AsInteger = paragraph.Width Dim paragraphHeight AsInteger = paragraph.Height Dim paragraphOcrAccuracy AsDouble = paragraph.Confidence Dim paragraphTextDirection = paragraph.TextDirection For Each line In paragraph.Lines ' Pages -> Paragraphs -> Lines Dim lineNumber AsInteger = line.LineNumber Dim lineText AsString = line.Text Dim lineImage AsAnyBitmap = line.ToBitmap(input) Dim lineImageLegacy AsSystem.Drawing.Bitmap = line.ToBitmap(input) Dim lineXLocation AsInteger = line.X Dim lineYLocation AsInteger = line.Y Dim lineWidth AsInteger = line.Width Dim lineHeight AsInteger = line.Height Dim lineOcrAccuracy AsDouble = line.Confidence Dim lineSkew AsDouble = line.BaselineAngle Dim lineOffset AsDouble = line.BaselineOffset For Each word In line.Words ' Pages -> Paragraphs -> Lines -> Words Dim wordNumber AsInteger = word.WordNumber Dim wordText AsString = word.Text Dim wordImage AsAnyBitmap = word.ToBitmap(input) Dim wordImageLegacy AsSystem.Drawing.Image = word.ToBitmap(input) Dim wordXLocation AsInteger = word.X Dim wordYLocation AsInteger = word.Y Dim wordWidth AsInteger = word.Width Dim wordHeight AsInteger = word.Height Dim wordOcrAccuracy AsDouble = word.Confidence If word.FontIsNot Nothing Then ' Word.Font is only set when using Tesseract Engine Modes rather than LTSM Dim fontName AsString = word.Font.FontName Dim fontSize AsDouble = word.Font.FontSize Dim isBold AsBoolean = word.Font.IsBold Dim isFixedWidth AsBoolean = word.Font.IsFixedWidth Dim isItalic AsBoolean = word.Font.IsItalic Dim isSerif AsBoolean = word.Font.IsSerif Dim isUnderlined AsBoolean = word.Font.IsUnderlined Dim fontIsCaligraphic AsBoolean = word.Font.IsCaligraphic End If For Each character In word.Characters ' Pages -> Paragraphs -> Lines -> Words -> Characters Dim characterNumber AsInteger = character.CharacterNumber Dim characterText AsString = character.Text Dim characterImage AsAnyBitmap = character.ToBitmap(input) Dim characterImageLegacy AsSystem.Drawing.Bitmap = character.ToBitmap(input) Dim characterXLocation AsInteger = character.X Dim characterYLocation AsInteger = character.Y Dim characterWidth AsInteger = character.Width Dim characterHeight AsInteger = character.Height Dim characterOcrAccuracy AsDouble = character.Confidence ' Output alternative symbols choices and their probability. ' Very useful for spell checking Dim characterChoices() AsOcrResult.Choice = character.Choices Next character Next word Next line Next paragraph Next pageEndUsing
Imports IronOcr
Imports 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
Private ocr As New IronTesseract()
ocr.Configuration.ReadBarCodes = True
Using input As New OcrInput()
Dim pageindices = New Integer() { 1, 2 }
input.LoadImageFrames("img\Potter.tiff", pageindices)
Dim result As OcrResult = ocr.Read(input)
For Each page In result.Pages
' Page object
Dim pageNumber As Integer = page.PageNumber
Dim pageText As String = page.Text
Dim pageWordCount As Integer = page.WordCount
' null if we don't set Ocr.Configuration.ReadBarCodes = true;
Dim barcodes() As OcrResult.Barcode = page.Barcodes
Dim pageImage As AnyBitmap = page.ToBitmap(input)
Dim pageImageLegacy As System.Drawing.Bitmap = page.ToBitmap(input)
Dim pageWidth As Double = page.Width
Dim pageHeight As Double = page.Height
For Each paragraph In page.Paragraphs
' Pages -> Paragraphs
Dim paragraphNumber As Integer = paragraph.ParagraphNumber
Dim paragraphText As String = paragraph.Text
Dim paragraphImage As System.Drawing.Bitmap = paragraph.ToBitmap(input)
Dim paragraphXLocation As Integer = paragraph.X
Dim paragraphYLocation As Integer = paragraph.Y
Dim paragraphWidth As Integer = paragraph.Width
Dim paragraphHeight As Integer = paragraph.Height
Dim paragraphOcrAccuracy As Double = paragraph.Confidence
Dim paragraphTextDirection = paragraph.TextDirection
For Each line In paragraph.Lines
' Pages -> Paragraphs -> Lines
Dim lineNumber As Integer = line.LineNumber
Dim lineText As String = line.Text
Dim lineImage As AnyBitmap = line.ToBitmap(input)
Dim lineImageLegacy As System.Drawing.Bitmap = line.ToBitmap(input)
Dim lineXLocation As Integer = line.X
Dim lineYLocation As Integer = line.Y
Dim lineWidth As Integer = line.Width
Dim lineHeight As Integer = line.Height
Dim lineOcrAccuracy As Double = line.Confidence
Dim lineSkew As Double = line.BaselineAngle
Dim lineOffset As Double = line.BaselineOffset
For Each word In line.Words
' Pages -> Paragraphs -> Lines -> Words
Dim wordNumber As Integer = word.WordNumber
Dim wordText As String = word.Text
Dim wordImage As AnyBitmap = word.ToBitmap(input)
Dim wordImageLegacy As System.Drawing.Image = word.ToBitmap(input)
Dim wordXLocation As Integer = word.X
Dim wordYLocation As Integer = word.Y
Dim wordWidth As Integer = word.Width
Dim wordHeight As Integer = word.Height
Dim wordOcrAccuracy As Double = word.Confidence
If word.Font IsNot Nothing Then
' Word.Font is only set when using Tesseract Engine Modes rather than LTSM
Dim fontName As String = word.Font.FontName
Dim fontSize As Double = word.Font.FontSize
Dim isBold As Boolean = word.Font.IsBold
Dim isFixedWidth As Boolean = word.Font.IsFixedWidth
Dim isItalic As Boolean = word.Font.IsItalic
Dim isSerif As Boolean = word.Font.IsSerif
Dim isUnderlined As Boolean = word.Font.IsUnderlined
Dim fontIsCaligraphic As Boolean = word.Font.IsCaligraphic
End If
For Each character In word.Characters
' Pages -> Paragraphs -> Lines -> Words -> Characters
Dim characterNumber As Integer = character.CharacterNumber
Dim characterText As String = character.Text
Dim characterImage As AnyBitmap = character.ToBitmap(input)
Dim characterImageLegacy As System.Drawing.Bitmap = character.ToBitmap(input)
Dim characterXLocation As Integer = character.X
Dim characterYLocation As Integer = character.Y
Dim characterWidth As Integer = character.Width
Dim characterHeight As Integer = character.Height
Dim characterOcrAccuracy As Double = character.Confidence
' Output alternative symbols choices and their probability.
' Very useful for spell checking
Dim characterChoices() As OcrResult.Choice = character.Choices
Next character
Next word
Next line
Next paragraph
Next page
End Using
將相同技術應用於 TIFF 轉換:
using IronOcr;var ocr = new IronTesseract();using (var input = new OcrInput()){ // Configure document properties input.Title = "Scanned Archive Document"; // Select pages to process var pageIndices = new int[] { 1, 2 }; input.LoadImageFrames("example.tiff", pageIndices); // Create searchable PDF from TIFF OcrResult result = ocr.Read(input); result.SaveAsSearchablePdf("searchable.pdf");}
using IronOcr;
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
// Configure document properties
input.Title = "Scanned Archive Document";
// Select pages to process
var pageIndices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageIndices);
// Create searchable PDF from TIFF
OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable.pdf");
}
ImportsIronOcrDim ocr As New IronTesseract()Using input As New OcrInput() ' Configure document properties input.Title = "Scanned Archive Document" ' Select pages to process Dim pageIndices AsInteger() = {1, 2} input.LoadImageFrames("example.tiff", pageIndices) ' Create searchable PDF from TIFF Dim result AsOcrResult = ocr.Read(input) result.SaveAsSearchablePdf("searchable.pdf")EndUsing
Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Configure document properties
input.Title = "Scanned Archive Document"
' Select pages to process
Dim pageIndices As Integer() = {1, 2}
input.LoadImageFrames("example.tiff", pageIndices)
' Create searchable PDF from TIFF
Dim result As OcrResult = ocr.Read(input)
result.SaveAsSearchablePdf("searchable.pdf")
End Using
如何將 OCR 結果匯出為 HOCR HTML?
IronOCR 支援 HOCR HTML 匯出,可在保持佈局資訊的同時進行結構化 PDF 到 HTML 及 TIFF 到 HTML 轉換:
using IronOcr;var ocr = new IronTesseract();using (var input = new OcrInput()){ // Set HTML title input.Title = "Document Archive"; // Process multiple document types input.LoadImage("image2.jpeg"); input.LoadPdf("example.pdf", "password"); // Add TIFF pages var pageIndices = new int[] { 1, 2 }; input.LoadImageFrames("example.tiff", pageIndices); // Export as HOCR with position data OcrResult result = ocr.Read(input); result.SaveAsHocrFile("hocr.html");}
using IronOcr;
var ocr = new IronTesseract();
using (var input = new OcrInput())
{
// Set HTML title
input.Title = "Document Archive";
// Process multiple document types
input.LoadImage("image2.jpeg");
input.LoadPdf("example.pdf", "password");
// Add TIFF pages
var pageIndices = new int[] { 1, 2 };
input.LoadImageFrames("example.tiff", pageIndices);
// Export as HOCR with position data
OcrResult result = ocr.Read(input);
result.SaveAsHocrFile("hocr.html");
}
ImportsIronOcrDim ocr As New IronTesseract()Using input As New OcrInput() ' Set HTML title input.Title = "Document Archive" ' Process multiple document types input.LoadImage("image2.jpeg") input.LoadPdf("example.pdf", "password") ' Add TIFF pages Dim pageIndices AsInteger() = {1, 2} input.LoadImageFrames("example.tiff", pageIndices) ' Export as HOCR with position data Dim result AsOcrResult = ocr.Read(input) result.SaveAsHocrFile("hocr.html")EndUsing
Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Set HTML title
input.Title = "Document Archive"
' Process multiple document types
input.LoadImage("image2.jpeg")
input.LoadPdf("example.pdf", "password")
' Add TIFF pages
Dim pageIndices As Integer() = {1, 2}
input.LoadImageFrames("example.tiff", pageIndices)
' Export as HOCR with position data
Dim result As OcrResult = ocr.Read(input)
result.SaveAsHocrFile("hocr.html")
End Using
// Enable combined text and barcode recognitionusing IronOcr;var ocr = new IronTesseract();// Enable barcode detectionocr.Configuration.ReadBarCodes = true;using (var input = new OcrInput()){ // Load image containing both text and barcodes input.LoadImage("img/Barcode.png"); // Process both text and barcodes var result = ocr.Read(input); // Extract barcode data foreach (var barcode in result.Barcodes) {Console.WriteLine($"Barcode Value: {barcode.Value}");Console.WriteLine($"Format: {barcode.Format}"); }}
// Enable combined text and barcode recognition
using IronOcr;
var ocr = new IronTesseract();
// Enable barcode detection
ocr.Configuration.ReadBarCodes = true;
using (var input = new OcrInput())
{
// Load image containing both text and barcodes
input.LoadImage("img/Barcode.png");
// Process both text and barcodes
var result = ocr.Read(input);
// Extract barcode data
foreach (var barcode in result.Barcodes)
{
Console.WriteLine($"Barcode Value: {barcode.Value}");
Console.WriteLine($"Format: {barcode.Format}");
}
}
using System;using IronOcr;using IronSoftware.Drawing;// Configure with barcode supportIronTesseract ocr = new IronTesseract{Configuration = { ReadBarCodes = true }};using OcrInput input = new OcrInput();// Process multi-page documentint[] pageIndices = { 1, 2 };input.LoadImageFrames(@"img\Potter.tiff", pageIndices);OcrResult result = ocr.Read(input);// Navigate the complete results hierarchyforeach (var page in result.Pages){ // Page-level data int pageNumber = page.PageNumber; string pageText = page.Text; int pageWordCount = page.WordCount; // Extract page elements OcrResult.Barcode[] barcodes = page.Barcodes; AnyBitmap pageImage = page.ToBitmap(); double pageWidth = page.Width; double pageHeight = page.Height; foreach (var paragraph in page.Paragraphs) { // Paragraph properties int paragraphNumber = paragraph.ParagraphNumber; string paragraphText = paragraph.Text; double paragraphConfidence = paragraph.Confidence; var textDirection = paragraph.TextDirection; foreach (var line in paragraph.Lines) { // Line details including baseline information string lineText = line.Text; double lineConfidence = line.Confidence; double baselineAngle = line.BaselineAngle; double baselineOffset = line.BaselineOffset; foreach (var word in line.Words) { // Word-level data string wordText = word.Text; double wordConfidence = word.Confidence; // Font information (when available) if (word.Font != null) { string fontName = word.Font.FontName; double fontSize = word.Font.FontSize; bool isBold = word.Font.IsBold; bool isItalic = word.Font.IsItalic; } foreach (var character in word.Characters) { // Character-level analysis string charText = character.Text; double charConfidence = character.Confidence; // Alternative character choices for spell-checking OcrResult.Choice[] alternatives = character.Choices; } } } }}
using System;
using IronOcr;
using IronSoftware.Drawing;
// Configure with barcode support
IronTesseract ocr = new IronTesseract
{
Configuration = { ReadBarCodes = true }
};
using OcrInput input = new OcrInput();
// Process multi-page document
int[] pageIndices = { 1, 2 };
input.LoadImageFrames(@"img\Potter.tiff", pageIndices);
OcrResult result = ocr.Read(input);
// Navigate the complete results hierarchy
foreach (var page in result.Pages)
{
// Page-level data
int pageNumber = page.PageNumber;
string pageText = page.Text;
int pageWordCount = page.WordCount;
// Extract page elements
OcrResult.Barcode[] barcodes = page.Barcodes;
AnyBitmap pageImage = page.ToBitmap();
double pageWidth = page.Width;
double pageHeight = page.Height;
foreach (var paragraph in page.Paragraphs)
{
// Paragraph properties
int paragraphNumber = paragraph.ParagraphNumber;
string paragraphText = paragraph.Text;
double paragraphConfidence = paragraph.Confidence;
var textDirection = paragraph.TextDirection;
foreach (var line in paragraph.Lines)
{
// Line details including baseline information
string lineText = line.Text;
double lineConfidence = line.Confidence;
double baselineAngle = line.BaselineAngle;
double baselineOffset = line.BaselineOffset;
foreach (var word in line.Words)
{
// Word-level data
string wordText = word.Text;
double wordConfidence = word.Confidence;
// Font information (when available)
if (word.Font != null)
{
string fontName = word.Font.FontName;
double fontSize = word.Font.FontSize;
bool isBold = word.Font.IsBold;
bool isItalic = word.Font.IsItalic;
}
foreach (var character in word.Characters)
{
// Character-level analysis
string charText = character.Text;
double charConfidence = character.Confidence;
// Alternative character choices for spell-checking
OcrResult.Choice[] alternatives = character.Choices;
}
}
}
}
}
ImportsSystemImportsIronOcrImportsIronSoftware.Drawing' Configure with barcode supportDim ocr As New IronTesseractWith { .Configuration = New TesseractConfigurationWith { .ReadBarCodes = True }}Using input As New OcrInput() ' Process multi-page document Dim pageIndices AsInteger() = {1, 2} input.LoadImageFrames("img\Potter.tiff", pageIndices) Dim result AsOcrResult = ocr.Read(input) ' Navigate the complete results hierarchy For Each page In result.Pages ' Page-level data Dim pageNumber AsInteger = page.PageNumber Dim pageText AsString = page.Text Dim pageWordCount AsInteger = page.WordCount ' Extract page elements Dim barcodes AsOcrResult.Barcode() = page.Barcodes Dim pageImage AsAnyBitmap = page.ToBitmap() Dim pageWidth AsDouble = page.Width Dim pageHeight AsDouble = page.Height For Each paragraph In page.Paragraphs ' Paragraph properties Dim paragraphNumber AsInteger = paragraph.ParagraphNumber Dim paragraphText AsString = paragraph.Text Dim paragraphConfidence AsDouble = paragraph.Confidence Dim textDirection = paragraph.TextDirection For Each line In paragraph.Lines ' Line details including baseline information Dim lineText AsString = line.Text Dim lineConfidence AsDouble = line.Confidence Dim baselineAngle AsDouble = line.BaselineAngle Dim baselineOffset AsDouble = line.BaselineOffset For Each word In line.Words ' Word-level data Dim wordText AsString = word.Text Dim wordConfidence AsDouble = word.Confidence ' Font information (when available) If word.FontIsNot Nothing Then Dim fontName AsString = word.Font.FontName Dim fontSize AsDouble = word.Font.FontSize Dim isBold AsBoolean = word.Font.IsBold Dim isItalic AsBoolean = word.Font.IsItalic End If For Each character In word.Characters ' Character-level analysis Dim charText AsString = character.Text Dim charConfidence AsDouble = character.Confidence ' Alternative character choices for spell-checking Dim alternatives AsOcrResult.Choice() = character.Choices Next Next Next Next NextEndUsing
Imports System
Imports IronOcr
Imports IronSoftware.Drawing
' Configure with barcode support
Dim ocr As New IronTesseract With {
.Configuration = New TesseractConfiguration With {
.ReadBarCodes = True
}
}
Using input As New OcrInput()
' Process multi-page document
Dim pageIndices As Integer() = {1, 2}
input.LoadImageFrames("img\Potter.tiff", pageIndices)
Dim result As OcrResult = ocr.Read(input)
' Navigate the complete results hierarchy
For Each page In result.Pages
' Page-level data
Dim pageNumber As Integer = page.PageNumber
Dim pageText As String = page.Text
Dim pageWordCount As Integer = page.WordCount
' Extract page elements
Dim barcodes As OcrResult.Barcode() = page.Barcodes
Dim pageImage As AnyBitmap = page.ToBitmap()
Dim pageWidth As Double = page.Width
Dim pageHeight As Double = page.Height
For Each paragraph In page.Paragraphs
' Paragraph properties
Dim paragraphNumber As Integer = paragraph.ParagraphNumber
Dim paragraphText As String = paragraph.Text
Dim paragraphConfidence As Double = paragraph.Confidence
Dim textDirection = paragraph.TextDirection
For Each line In paragraph.Lines
' Line details including baseline information
Dim lineText As String = line.Text
Dim lineConfidence As Double = line.Confidence
Dim baselineAngle As Double = line.BaselineAngle
Dim baselineOffset As Double = line.BaselineOffset
For Each word In line.Words
' Word-level data
Dim wordText As String = word.Text
Dim wordConfidence As Double = word.Confidence
' Font information (when available)
If word.Font IsNot Nothing Then
Dim fontName As String = word.Font.FontName
Dim fontSize As Double = word.Font.FontSize
Dim isBold As Boolean = word.Font.IsBold
Dim isItalic As Boolean = word.Font.IsItalic
End If
For Each character In word.Characters
' Character-level analysis
Dim charText As String = character.Text
Dim charConfidence As Double = character.Confidence
' Alternative character choices for spell-checking
Dim alternatives As OcrResult.Choice() = character.Choices
Next
Next
Next
Next
Next
End Using
總結
IronOCR 為 C# 開發人員提供了最先進的 Tesseract API 實現,在 Windows、Linux 和 Mac 平臺無縫運行。 其使用 IronOCR 的能力,準確地從圖像中讀取文字——即使從不完美的文件中——使其不同於基礎的 OCR 解決方案。
程式庫的獨特功能包括整合條碼閱讀和能夠將結果匯出為可搜尋的 PDF 或 HOCR HTML,這些能力在標準的 Tesseract 實現中無法獲得。