ArcGIS Pro 3.8 API Reference Guide
ArcGIS.Core.OCR Namespace / OCRManager Class / ExtractTextFromDocumentAsync Method / ExtractTextFromDocumentAsync(String,CancellationToken) Method
The document image. See IsValidDocumentForOCR for information about the file extensions supported.
A System.Threading.CancellationToken used to control cancellation behavior for the returned task.
Example

In This Topic
    ExtractTextFromDocumentAsync(String,CancellationToken) Method
    In This Topic
    Extracts text from a document image using the OCROutputType.Text output type. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public Task<string> ExtractTextFromDocumentAsync( 
       string documentFileName,
       CancellationToken cancellationToken
    )
    Public Overloads Function ExtractTextFromDocumentAsync( _
       ByVal documentFileName As String, _
       ByVal cancellationToken As CancellationToken _
    ) As Task(Of String)

    Parameters

    documentFileName
    The document image. See IsValidDocumentForOCR for information about the file extensions supported.
    cancellationToken
    A System.Threading.CancellationToken used to control cancellation behavior for the returned task.

    Return Value

    A string containing the text extracted. The string can be empty if no data can be extracted.
    Exceptions
    ExceptionDescription
    documentFileName is null.
    The document image cannot be found or has an invalid extension for OCR.
    An error occurred within the OCR engine.
    The operation was canceled.
    This method or property must be called within the lambda passed to QueuedTask.Run.
    Remarks
    Optical Character Recognition (OCR) technology is used to extract text from the document image. Text extraction and character recognition will be dependent on the quality and content of the image. There might also be difficulty in extracting text from complex data.

    If you wish to specify a specific output type or customize other options, use the ExtractFromDocumentAsync(OCROptions) or ExtractFromDocumentAsync(OCROptions,CancellationToken) overloads and set the options such as OCROptions.OutputType as appropriate.

    Example
    Extract text from a document using a CancellationTracker
    {
      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
      }
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.8 or higher.
    See Also