Repository navigation
ProSnippets OCR
Uma Harano edited this page Nov 18, 2026
·
2 revisions
Language: C#
Subject: OCR
Contributor: ArcGIS Pro SDK Team <arcgisprosdk@esri.com>
Organization: Esri, http://www.esri.com
Date: 11/18/2026
ArcGIS Pro: 3.8
IDE: Visual Studio 2026, Visual Studio Code
string docFileName1 = "c:\\MyData\\MyDocument.pdf";
string docFileName2 = "c:\\MyData\\MyDocument.bmp";
string docFileName3 = "c:\\MyData\\MyDocument.txt";
var isValid = OCRManager.Instance.IsValidDocumentForOCR(docFileName1);
// isValid = true
isValid = OCRManager.Instance.IsValidDocumentForOCR(docFileName2);
// isValid = true
isValid = OCRManager.Instance.IsValidDocumentForOCR(docFileName3);
// isValid = falsestring docFileName = "c:\\MyData\\MyDocument.pdf";
var pageCount = OCRManager.Instance.GetDocumentPageCount(docFileName);string docFileName = "c:\\MyData\\MyDocument.pdf";
var text = await OCRManager.Instance.ExtractTextFromDocumentAsync(docFileName);
// or extract from a specific page
//int pageIdx = 1;
//var text = await OCRManager.Instance.ExtractTextFromDocumentAsync(deedFileName, pageIdx);string docFileName = "c:\\MyData\\MyDocument.pdf";
var text = "";
bool canceled = false;
using (CancelableProgressorSource cps = new CancelableProgressorSource("Reading document", "Canceled"))
{
canceled = await QueuedTask.Run<bool>(async () =>
{
try
{
text = await ArcGIS.Core.OCR.OCRManager.Instance.ExtractTextFromDocumentAsync(docFileName, cps.Progressor.CancellationToken);
}
catch (OperationCanceledException cex)
{
return true;
}
catch (Exception ex)
{
}
return false;
}, cps.Progressor);
}
if (canceled)
{
// to do - some cancel action
}
else
{
// to do - something with text
}string docFileName = "c:\\MyData\\MyDocument.tiff";
var ocrOptions = new OCROptions(docFileName);
ocrOptions.OutputType = OCROutputType.Text;
// set a specific page if necessary
// (if not set, then all pages are scanned)
// ocrOptions.Page = pageIdx;
// set the resolution if necessary
// ocrOptions.Resolution = 400;
var output = await OCRManager.Instance.ExtractFromDocumentAsync(ocrOptions);
var textOutput = output as OCRTextOutput;
var text = textOutput.Text;string docFileName = "c:\\MyData\\MyDocument.jpg";
var ocrOptions = new OCROptions(docFileName);
ocrOptions.OutputType = OCROutputType.Text;
// set a specific page if necessary
// (if not set, then all pages are scanned)
// ocrOptions.Page = pageIdx;
// set the resolution if necessary
// ocrOptions.Resolution = 400;
// set the language
// (de.traineddata should be installed at (arcgis_install_folder)\Resources\OCR)
// if file cannot be found an exception is thrown
ocrOptions.Model = "de";
try
{
var output = await OCRManager.Instance.ExtractFromDocumentAsync(ocrOptions);
var textOutput = output as OCRTextOutput;
var text = textOutput.Text;
}
catch (ArcGIS.Core.OCR.Exceptions.OCRException ex)
{
}string docFileName = "c:\\MyData\\MyDocument.bmp";
var ocrOptions = new OCROptions(docFileName);
ocrOptions.OutputType = OCROutputType.TSV;
// set a specific page if necessary
// (if not set, then all pages are scanned)
// ocrOptions.Page = pageIdx;
// set the resolution if necessary
// ocrOptions.Resolution = 400;
var output = await OCRManager.Instance.ExtractFromDocumentAsync(ocrOptions);
if (output is OCRTSVOutput ocr_tsvOutput)
{
// process the tsv output
foreach (var page in ocr_tsvOutput.Pages)
{
foreach (var block in page.Blocks)
{
foreach (var para in block.Paragraphs)
{
foreach (var line in para.Lines)
{
foreach (var word in line.Words)
{
var content = word.Content;
var confidence = word.Confidence;
// process content according to confidence
}
}
}
}
}
}string text = "";
var flowDocument = new FlowDocument();
foreach (var page in tsvOutput.Pages)
{
List<Paragraph> pageParagraphs = new();
foreach (var block in page.Blocks)
{
foreach (var paragraph in block.Paragraphs)
{
foreach (var line in paragraph.Lines)
{
string lineWords = string.Join(" ", line.Words.Select(w => w.Content));
pageParagraphs.Add(new Paragraph(new Span(new Run(lineWords))));
}
}
}
flowDocument.Blocks.AddRange(pageParagraphs);
flowDocument.Blocks.Add(new Paragraph(new Span(new Run(Environment.NewLine))));
}
var startPointer = flowDocument?.ContentStart;
var endPointer = flowDocument?.ContentEnd;
if (startPointer != null && endPointer != null)
{
text = new TextRange(startPointer, endPointer).Text;
}
Home | API Reference | Requirements | Download | Samples | Snippets
-
Determine if a document is valid for OCR
-
Get number of pages in a document
-
Extract text from a document
-
Extract text from a document using a CancellationTracker
-
Extract text from a document using OCROptions
-
Extract text from a document using a different model
-
Extract TSV output from a document
-
Get text from TSVOutput (simple)