using IronOcr;// Write logs to a specific fileInstallation.LogFilePath = "logs/ocr_diagnostics.log";// Enable all logging channels: file + debug outputInstallation.LoggingMode = Installation.LoggingModes.All;// Or pipe logs into your existing ILogger pipelineInstallation.CustomLogger = myLoggerInstance;
using IronOcr;
// Write logs to a specific file
Installation.LogFilePath = "logs/ocr_diagnostics.log";
// Enable all logging channels: file + debug output
Installation.LoggingMode = Installation.LoggingModes.All;
// Or pipe logs into your existing ILogger pipeline
Installation.CustomLogger = myLoggerInstance;
ImportsIronOcr' Write logs to a specific fileInstallation.LogFilePath = "logs/ocr_diagnostics.log"' Enable all logging channels: file + debug outputInstallation.LoggingMode = Installation.LoggingModes.All' Or pipe logs into your existing ILogger pipelineInstallation.CustomLogger = myLoggerInstance
Imports IronOcr
' Write logs to a specific file
Installation.LogFilePath = "logs/ocr_diagnostics.log"
' Enable all logging channels: file + debug output
Installation.LoggingMode = Installation.LoggingModes.All
' Or pipe logs into your existing ILogger pipeline
Installation.CustomLogger = myLoggerInstance
using IronOcr;using IronOcr.Exceptions;var ocr = new IronTesseract();try{ using var input = new OcrInput(); input.LoadPdf("invoice_scan.pdf"); OcrResult result = ocr.Read(input);Console.WriteLine($"Text: {result.Text}");Console.WriteLine($"Confidence: {result.Confidence:P1}");}catch (IronOcrInputException ex){ // File could not be loaded — corrupt, locked, or unsupported formatConsole.Error.WriteLine($"Input error: {ex.Message}");}catch (IronOcrDictionaryException ex){ // Language pack missing — common in containerized deploymentsConsole.Error.WriteLine($"Language pack error: {ex.Message}");}catch (IronOcrNativeException ex) when (ex.Message.Contains("AVX")){ // CPU does not support AVX instructionsConsole.Error.WriteLine($"Hardware incompatibility: {ex.Message}");}catch (IronOcrLicensingException){Console.Error.WriteLine("License key is missing or invalid.");}catch (IronOcrProductException ex){ // Catch-all for other IronOCR engine errorsConsole.Error.WriteLine($"OCR engine error: {ex.Message}");Console.Error.WriteLine($"Stack trace: {ex.StackTrace}");}
using IronOcr;
using IronOcr.Exceptions;
var ocr = new IronTesseract();
try
{
using var input = new OcrInput();
input.LoadPdf("invoice_scan.pdf");
OcrResult result = ocr.Read(input);
Console.WriteLine($"Text: {result.Text}");
Console.WriteLine($"Confidence: {result.Confidence:P1}");
}
catch (IronOcrInputException ex)
{
// File could not be loaded — corrupt, locked, or unsupported format
Console.Error.WriteLine($"Input error: {ex.Message}");
}
catch (IronOcrDictionaryException ex)
{
// Language pack missing — common in containerized deployments
Console.Error.WriteLine($"Language pack error: {ex.Message}");
}
catch (IronOcrNativeException ex) when (ex.Message.Contains("AVX"))
{
// CPU does not support AVX instructions
Console.Error.WriteLine($"Hardware incompatibility: {ex.Message}");
}
catch (IronOcrLicensingException)
{
Console.Error.WriteLine("License key is missing or invalid.");
}
catch (IronOcrProductException ex)
{
// Catch-all for other IronOCR engine errors
Console.Error.WriteLine($"OCR engine error: {ex.Message}");
Console.Error.WriteLine($"Stack trace: {ex.StackTrace}");
}
ImportsIronOcrImportsIronOcr.ExceptionsDim ocr = New IronTesseract()TryUsing input = New OcrInput() input.LoadPdf("invoice_scan.pdf") Dim result AsOcrResult = ocr.Read(input)Console.WriteLine($"Text: {result.Text}")Console.WriteLine($"Confidence: {result.Confidence:P1}")EndUsingCatch ex AsIronOcrInputException ' File could not be loaded — corrupt, locked, or unsupported formatConsole.Error.WriteLine($"Input error: {ex.Message}")Catch ex AsIronOcrDictionaryException ' Language pack missing — common in containerized deploymentsConsole.Error.WriteLine($"Language pack error: {ex.Message}")Catch ex AsIronOcrNativeExceptionWhen ex.Message.Contains("AVX") ' CPU does not support AVX instructionsConsole.Error.WriteLine($"Hardware incompatibility: {ex.Message}")Catch ex AsIronOcrLicensingExceptionConsole.Error.WriteLine("License key is missing or invalid.")Catch ex AsIronOcrProductException ' Catch-all for other IronOCR engine errorsConsole.Error.WriteLine($"OCR engine error: {ex.Message}")Console.Error.WriteLine($"Stack trace: {ex.StackTrace}")EndTry
Imports IronOcr
Imports IronOcr.Exceptions
Dim ocr = New IronTesseract()
Try
Using input = New OcrInput()
input.LoadPdf("invoice_scan.pdf")
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine($"Text: {result.Text}")
Console.WriteLine($"Confidence: {result.Confidence:P1}")
End Using
Catch ex As IronOcrInputException
' File could not be loaded — corrupt, locked, or unsupported format
Console.Error.WriteLine($"Input error: {ex.Message}")
Catch ex As IronOcrDictionaryException
' Language pack missing — common in containerized deployments
Console.Error.WriteLine($"Language pack error: {ex.Message}")
Catch ex As IronOcrNativeException When ex.Message.Contains("AVX")
' CPU does not support AVX instructions
Console.Error.WriteLine($"Hardware incompatibility: {ex.Message}")
Catch ex As IronOcrLicensingException
Console.Error.WriteLine("License key is missing or invalid.")
Catch ex As IronOcrProductException
' Catch-all for other IronOCR engine errors
Console.Error.WriteLine($"OCR engine error: {ex.Message}")
Console.Error.WriteLine($"Stack trace: {ex.StackTrace}")
End Try
输出
成功输出
发票加载正常,引擎返回字符数和置信度评分。
输出失败
按从最具体到最一般的顺序排列捕获块。 when 子句在 IronOcrNativeException 上筛选与AVX相关的失败,而不捕获无关的本机错误。 每个处理程序都会记录异常消息; 兜底块还会捕获堆栈跟踪信息,以便进行事后分析。
using IronOcr;var ocr = new IronTesseract();using var input = new OcrInput();input.LoadImage("receipt.png");OcrResult result = ocr.Read(input);double confidence = result.Confidence;Console.WriteLine($"Overall confidence: {confidence:P1}");// Threshold-gated decisionif (confidence >= 0.90){Console.WriteLine("ACCEPT — high confidence, processing result.");ProcessResult(result.Text);}else if (confidence >= 0.70){Console.WriteLine("FLAG — moderate confidence, queuing for review.");QueueForReview(result.Text, confidence);}else{Console.WriteLine("REJECT — low confidence, logging for investigation.");LogRejection("receipt.png", confidence);}// Drill into per-page and per-word confidence for diagnosticsforeach (var page in result.Pages){Console.WriteLine($" Page {page.PageNumber}: {page.Confidence:P1}"); var lowConfidenceWords = page.Words .Where(w => w.Confidence < 0.70) .ToList(); foreach (var word in lowConfidenceWords) {Console.WriteLine($" Low-confidence word: \"{word.Text}\" ({word.Confidence:P1})"); }}
using IronOcr;
var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("receipt.png");
OcrResult result = ocr.Read(input);
double confidence = result.Confidence;
Console.WriteLine($"Overall confidence: {confidence:P1}");
// Threshold-gated decision
if (confidence >= 0.90)
{
Console.WriteLine("ACCEPT — high confidence, processing result.");
ProcessResult(result.Text);
}
else if (confidence >= 0.70)
{
Console.WriteLine("FLAG — moderate confidence, queuing for review.");
QueueForReview(result.Text, confidence);
}
else
{
Console.WriteLine("REJECT — low confidence, logging for investigation.");
LogRejection("receipt.png", confidence);
}
// Drill into per-page and per-word confidence for diagnostics
foreach (var page in result.Pages)
{
Console.WriteLine($" Page {page.PageNumber}: {page.Confidence:P1}");
var lowConfidenceWords = page.Words
.Where(w => w.Confidence < 0.70)
.ToList();
foreach (var word in lowConfidenceWords)
{
Console.WriteLine($" Low-confidence word: \"{word.Text}\" ({word.Confidence:P1})");
}
}
ImportsIronOcrDim ocr As New IronTesseract()Using input As New OcrInput() input.LoadImage("receipt.png") Dim result AsOcrResult = ocr.Read(input) Dim confidence AsDouble = result.ConfidenceConsole.WriteLine($"Overall confidence: {confidence:P1}") ' Threshold-gated decision If confidence >= 0.9 ThenConsole.WriteLine("ACCEPT — high confidence, processing result.")ProcessResult(result.Text) ElseIf confidence >= 0.7 ThenConsole.WriteLine("FLAG — moderate confidence, queuing for review.")QueueForReview(result.Text, confidence) ElseConsole.WriteLine("REJECT — low confidence, logging for investigation.")LogRejection("receipt.png", confidence) End If ' Drill into per-page and per-word confidence for diagnostics For Each page In result.PagesConsole.WriteLine($" Page {page.PageNumber}: {page.Confidence:P1}") Dim lowConfidenceWords = page.Words _ .Where(Function(w) w.Confidence < 0.7) _ .ToList() For Each word In lowConfidenceWordsConsole.WriteLine($" Low-confidence word: ""{word.Text}"" ({word.Confidence:P1})") Next NextEndUsing
Imports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
input.LoadImage("receipt.png")
Dim result As OcrResult = ocr.Read(input)
Dim confidence As Double = result.Confidence
Console.WriteLine($"Overall confidence: {confidence:P1}")
' Threshold-gated decision
If confidence >= 0.9 Then
Console.WriteLine("ACCEPT — high confidence, processing result.")
ProcessResult(result.Text)
ElseIf confidence >= 0.7 Then
Console.WriteLine("FLAG — moderate confidence, queuing for review.")
QueueForReview(result.Text, confidence)
Else
Console.WriteLine("REJECT — low confidence, logging for investigation.")
LogRejection("receipt.png", confidence)
End If
' Drill into per-page and per-word confidence for diagnostics
For Each page In result.Pages
Console.WriteLine($" Page {page.PageNumber}: {page.Confidence:P1}")
Dim lowConfidenceWords = page.Words _
.Where(Function(w) w.Confidence < 0.7) _
.ToList()
For Each word In lowConfidenceWords
Console.WriteLine($" Low-confidence word: ""{word.Text}"" ({word.Confidence:P1})")
Next
Next
End Using
using IronOcr;var ocr = new IronTesseract();ocr.OcrProgress += (sender, e) =>{Console.WriteLine( $"[OCR] {e.ProgressPercent}% complete | " + $"Page {e.PagesComplete}/{e.TotalPages} | " + $"Elapsed: {e.Duration.TotalSeconds:F1}s" );};using var input = new OcrInput();input.LoadPdf("quarterly_report.pdf");OcrResult result = ocr.Read(input);Console.WriteLine($"Finished in {result.Pages.Count()} pages, confidence: {result.Confidence:P1}");
using IronOcr;
var ocr = new IronTesseract();
ocr.OcrProgress += (sender, e) =>
{
Console.WriteLine(
$"[OCR] {e.ProgressPercent}% complete | " +
$"Page {e.PagesComplete}/{e.TotalPages} | " +
$"Elapsed: {e.Duration.TotalSeconds:F1}s"
);
};
using var input = new OcrInput();
input.LoadPdf("quarterly_report.pdf");
OcrResult result = ocr.Read(input);
Console.WriteLine($"Finished in {result.Pages.Count()} pages, confidence: {result.Confidence:P1}");
ImportsIronOcrDim ocr As New IronTesseract()AddHandler ocr.OcrProgress, Sub(sender, e)Console.WriteLine($"[OCR] {e.ProgressPercent}% complete | " & $"Page {e.PagesComplete}/{e.TotalPages} | " & $"Elapsed: {e.Duration.TotalSeconds:F1}s")End SubUsing input As New OcrInput() input.LoadPdf("quarterly_report.pdf") Dim result AsOcrResult = ocr.Read(input)Console.WriteLine($"Finished in {result.Pages.Count()} pages, confidence: {result.Confidence:P1}")EndUsing
Imports IronOcr
Dim ocr As New IronTesseract()
AddHandler ocr.OcrProgress, Sub(sender, e)
Console.WriteLine($"[OCR] {e.ProgressPercent}% complete | " &
$"Page {e.PagesComplete}/{e.TotalPages} | " &
$"Elapsed: {e.Duration.TotalSeconds:F1}s")
End Sub
Using input As New OcrInput()
input.LoadPdf("quarterly_report.pdf")
Dim result As OcrResult = ocr.Read(input)
Console.WriteLine($"Finished in {result.Pages.Count()} pages, confidence: {result.Confidence:P1}")
End Using
ImportsIronOcrImportsIronOcr.ExceptionsDim ocr As New IronTesseract()Installation.LogFilePath = "batch_debug.log"Installation.LoggingMode = Installation.LoggingModes.FileDim files AsString() = Directory.GetFiles("scans/", "*.pdf")Dim succeeded AsInteger = 0, failed AsInteger = 0Dim totalConfidence AsDouble = 0Dim failures As New List(Of (FileAsString, ErrorAsString))()For Each file AsStringIn filesTryUsing input As New OcrInput() input.LoadPdf(file) Dim result AsOcrResult = ocr.Read(input) totalConfidence += result.Confidence succeeded += 1Console.WriteLine($"OK: {Path.GetFileName(file)} — {result.Confidence:P1}")EndUsingCatch ex AsIronOcrInputException failed += 1 failures.Add((file, $"Input error: {ex.Message}"))Console.Error.WriteLine($"FAIL: {Path.GetFileName(file)} — {ex.Message}")Catch ex AsIronOcrProductException failed += 1 failures.Add((file, $"Engine error: {ex.Message}"))Console.Error.WriteLine($"FAIL: {Path.GetFileName(file)} — {ex.Message}")Catch ex AsException failed += 1 failures.Add((file, $"Unexpected: {ex.Message}"))Console.Error.WriteLine($"FAIL: {Path.GetFileName(file)} — {ex.GetType().Name}: {ex.Message}")EndTryNext' Summary reportConsole.WriteLine(vbCrLf & "--- Batch Summary ---")Console.WriteLine($"Total: {files.Length} | Passed: {succeeded} | Failed: {failed}")If succeeded > 0 ThenConsole.WriteLine($"Average confidence: {totalConfidence / succeeded:P1}")End IfFor Each failure In failuresConsole.WriteLine($" {Path.GetFileName(failure.File)}: {failure.Error}")Next
Imports IronOcr
Imports IronOcr.Exceptions
Dim ocr As New IronTesseract()
Installation.LogFilePath = "batch_debug.log"
Installation.LoggingMode = Installation.LoggingModes.File
Dim files As String() = Directory.GetFiles("scans/", "*.pdf")
Dim succeeded As Integer = 0, failed As Integer = 0
Dim totalConfidence As Double = 0
Dim failures As New List(Of (File As String, Error As String))()
For Each file As String In files
Try
Using input As New OcrInput()
input.LoadPdf(file)
Dim result As OcrResult = ocr.Read(input)
totalConfidence += result.Confidence
succeeded += 1
Console.WriteLine($"OK: {Path.GetFileName(file)} — {result.Confidence:P1}")
End Using
Catch ex As IronOcrInputException
failed += 1
failures.Add((file, $"Input error: {ex.Message}"))
Console.Error.WriteLine($"FAIL: {Path.GetFileName(file)} — {ex.Message}")
Catch ex As IronOcrProductException
failed += 1
failures.Add((file, $"Engine error: {ex.Message}"))
Console.Error.WriteLine($"FAIL: {Path.GetFileName(file)} — {ex.Message}")
Catch ex As Exception
failed += 1
failures.Add((file, $"Unexpected: {ex.Message}"))
Console.Error.WriteLine($"FAIL: {Path.GetFileName(file)} — {ex.GetType().Name}: {ex.Message}")
End Try
Next
' Summary report
Console.WriteLine(vbCrLf & "--- Batch Summary ---")
Console.WriteLine($"Total: {files.Length} | Passed: {succeeded} | Failed: {failed}")
If succeeded > 0 Then
Console.WriteLine($"Average confidence: {totalConfidence / succeeded:P1}")
End If
For Each failure In failures
Console.WriteLine($" {Path.GetFileName(failure.File)}: {failure.Error}")
Next