Azure. AI. FormRecognizer 4.0.0.
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Azure.AI.FormRecognizer --version 4.0.0.
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Azure.AI.FormRecognizer, 4.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Azure.AI.FormRecognizer as a Cake Addin #addin nuget:?package=Azure.AI.FormRecognizer&version=4.0.0 // Install Azure.AI.FormRecognizer as a Cake Tool #tool nuget:?package=Azure.AI.FormRecognizer&version=4.0.0.
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Azure Cognitive Services Form Recognizer client library for .NET.
Azure Cognitive Services Form Recognizer is a cloud service that uses machine learning to analyze text and structured data from your documents. It includes the following main features:
Layout - Extract text, selection marks, table structures, styles, and paragraphs, along with their bounding region coordinates from documents. General document - Analyze key-value pairs in addition to general layout from documents. Read - Read information about textual elements, such as page words and lines in addition to text language information. Prebuilt - Analyze data from certain types of common documents using prebuilt models. Supported documents include receipts, invoices, business cards, identity documents, US W2 tax forms, and more. Custom - Build custom models to analyze text, field values, selection marks, table structures, styles, and paragraphs from documents. Custom models are built with your own data, so they're tailored to your documents.
Getting started.
Install the package.
Install the Azure Form Recognizer client library for .NET with NuGet:
dotnet add package Azure.AI.FormRecognizer.
Note: This version of the client library defaults to the 2022-08-31 version of the service.
This table shows the relationship between SDK versions and supported API versions of the service:
SDK version Supported API version of service 4.0.0 2.0, 2.1, 2022-08-31 3.1.X 2.0, 2.1 3.0.X 2.0.
Note: Starting with version 4.0.0 , a new set of clients were introduced to leverage the newest features of the Form Recognizer service. Please see the Migration Guide for detailed instructions on how to update application code from client library version 3.1.X or lower to the latest version. Additionally, see the Changelog for more detailed information. The table below describes the relationship of each client and its supported API version(s):
API version Supported clients 2022-08-31 DocumentAnalysisClient and DocumentModelAdministrationClient 2.1 FormRecognizerClient and FormTrainingClient 2.0 FormRecognizerClient and FormTrainingClient.
Prerequisites.
An Azure subscription. A Cognitive Services or Form Recognizer resource to use this package.
Create a Cognitive Services or Form Recognizer resource.
Form Recognizer supports both multi-service and single-service access. Create a Cognitive Services resource if you plan to access multiple cognitive services under a single endpoint/key. For Form Recognizer access only, create a Form Recognizer resource. Please note that you will need a single-service resource if you intend to use Azure Active Directory authentication.
You can create either resource using:
Option 1: Azure Portal. Option 2: Azure CLI.
Below is an example of how you can create a Form Recognizer resource using the CLI:
# Create a new resource group to hold the Form Recognizer resource # If using an existing resource group, skip this step az group create --name --location.
# Create the Form Recognizer resource az cognitiveservices account create \ --name \ --resource-group \ --kind FormRecognizer \ --sku \ --location \ --yes.
For more information about creating the resource or how to get the location and sku information see here.
Authenticate the client.
In order to interact with the Form Recognizer service, you'll need to create an instance of the DocumentAnalysisClient class. An endpoint and credential are necessary to instantiate the client object.
Get the endpoint.
You can find the endpoint for your Form Recognizer resource using the Azure Portal or Azure CLI:
# Get the endpoint for the Form Recognizer resource az cognitiveservices account show --name "" --resource-group "" --query "properties.endpoint"
Either a regional endpoint or a custom subdomain can be used for authentication. They are formatted as follows:
Regional endpoint: https://.api.cognitive.microsoft.com/ Custom subdomain: https://.cognitiveservices.azure.com/
A regional endpoint is the same for every resource in a region. A complete list of supported regional endpoints can be consulted here. Please note that regional endpoints do not support AAD authentication.
A custom subdomain, on the other hand, is a name that is unique to the Form Recognizer resource. They can only be used by single-service resources.
Get the API Key.
The API key can be found in the Azure Portal or by running the following Azure CLI command:
az cognitiveservices account keys list --name "" --resource-group ""
Create DocumentAnalysisClient with AzureKeyCredential.
Once you have the value for the API key, create an AzureKeyCredential . With the endpoint and key credential, you can create the DocumentAnalysisClient :
string endpoint = ""; string apiKey = ""; var credential = new AzureKeyCredential(apiKey); var client = new DocumentAnalysisClient(new Uri(endpoint), credential);
Create DocumentAnalysisClient with Azure Active Directory Credential.
AzureKeyCredential authentication is used in the examples in this getting started guide, but you can also authenticate with Azure Active Directory using the Azure Identity library. Note that regional endpoints do not support AAD authentication. Create a custom subdomain for your resource in order to use this type of authentication.
To use the DefaultAzureCredential provider shown below, or other credential providers provided with the Azure SDK, please install the Azure.Identity package:
dotnet add package Azure.Identity.
You will also need to register a new AAD application and grant access to Form Recognizer by assigning the "Cognitive Services User" role to your service principal.
Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: AZURE CLIENT ID, AZURE TENANT ID, AZURE CLIENT SECRET.
string endpoint = ""; var client = new DocumentAnalysisClient(new Uri(endpoint), new DefaultAzureCredential());
Key concepts.
DocumentAnalysisClient.
DocumentAnalysisClient provides operations for analyzing input documents using prebuilt and custom models through the AnalyzeDocument and AnalyzeDocumentFromUri APIs. Use the modelId parameter to select the type of model for analysis.
Sample code snippets are provided to illustrate using a DocumentAnalysisClient here. More information about analyzing documents, including supported features, locales, and document types can be found in the service documentation.
DocumentModelAdministrationClient.
DocumentModelAdministrationClient provides operations for:
Building custom models to analyze specific fields you specify by labeling your custom documents. A DocumentModelDetails instance is returned indicating the document type(s) the model can analyze, the fields it can analyze for each document type, as well as the estimated confidence for each field. See the service documentation for a more detailed explanation. Compose a model from a collection of existing models. Managing models created in your account. Listing document model operations or getting a specific model operation created within the last 24 hours. Copying a custom model from one Form Recognizer resource to another.
Please note that models can also be built using a graphical user interface such as the Form Recognizer Labeling Tool.
Long-Running Operations.
Because analyzing documents and building models take time, these operations are implemented as long-running operations . Long-running operations consist of an initial request sent to the service to start an operation, followed by polling the service at intervals to determine whether the operation has completed or failed, and if it has succeeded, to get the result.
For long running operations in the Azure SDK, the client exposes a method that returns an Operation object. You can set its parameter waitUntil to WaitUntil.Completed to wait for the operation to complete and obtain its result; or set it to WaitUntil.Started if you just want to start the operation and consume the result later. A sample code snippet is provided to illustrate using long-running operations below.
Thread safety.
We guarantee that all client instance methods are thread-safe and independent of each other (guideline). This ensures that the recommendation of reusing client instances is always safe, even across threads.
Additional concepts.
Examples.
The following section provides several code snippets illustrating common patterns used in the Form Recognizer .NET API. Most of the snippets below make use of asynchronous service calls, but keep in mind that the Azure.AI.FormRecognizer package supports both synchronous and asynchronous APIs.
Async examples.
Extract Layout Use the Prebuilt General Document Model Use the Prebuilt Read Model Use Prebuilt Models Build a Custom Model Analyze Custom Documents Manage Models.
Sync examples.
Note that these samples use SDK version 4.0.0 . For lower versions of the SDK, please see Form Recognizer Samples for V3.1.X.
Extract Layout.
Extract text, selection marks, table structures, styles, and paragraphs, along with their bounding region coordinates from documents.
Uri fileUri = new Uri(""); AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-layout", fileUri); AnalyzeResult result = operation.Value; foreach (DocumentPage page in result.Pages) X: , Y: "); > > for (int i = 0; i X: , Y: "); > > > Console.WriteLine("Paragraphs:"); foreach (DocumentParagraph paragraph in result.Paragraphs) > foreach (DocumentStyle style in result.Styles) 0.8) "); > > > Console.WriteLine("The following tables were extracted:"); for (int i = 0; i >
For more information and samples see here.
Use the Prebuilt General Document Model.
Analyze text, selection marks, table structures, styles, paragraphs, and key-value pairs from documents using the prebuilt general document model.
Uri fileUri = new Uri(""); AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-document", fileUri); AnalyzeResult result = operation.Value; Console.WriteLine("Detected key-value pairs:"); foreach (DocumentKeyValuePair kvp in result.KeyValuePairs) else > foreach (DocumentPage page in result.Pages) X: , Y: "); > > for (int i = 0; i X: , Y: "); > > > foreach (DocumentStyle style in result.Styles) 0.8) "); > > > Console.WriteLine("The following tables were extracted:"); for (int i = 0; i >
For more information and samples see here.
Use the Prebuilt Read Model.
Analyze textual elements, such as page words and lines, styles, paragraphs, and text language information from documents using the prebuilt read model.
Uri fileUri = new Uri(""); AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-read", fileUri); AnalyzeResult result = operation.Value; Console.WriteLine("Detected languages:"); foreach (DocumentLanguage language in result.Languages) foreach (DocumentPage page in result.Pages) X: , Y: "); > > > foreach (DocumentStyle style in result.Styles) 0.8) "); > > >
For more information and samples see here.
Use Prebuilt Models.
Analyze data from certain types of common documents using prebuilt models provided by the Form Recognizer service.
For example, to analyze fields from an invoice, use the prebuilt Invoice model provided by passing the prebuilt-invoice model ID into the AnalyzeDocumentAsync method:
string filePath = ""; using var stream = new FileStream(filePath, FileMode.Open); AnalyzeDocumentOperation operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-invoice", stream); AnalyzeResult result = operation.Value; // To see the list of all the supported fields returned by service and its corresponding types for the // prebuilt-invoice model, consult: // https://aka.ms/azsdk/formrecognizer/invoicefieldschema for (int i = 0; i > if (document.Fields.TryGetValue("CustomerName", out DocumentField customerNameField)) > if (document.Fields.TryGetValue("Items", out DocumentField itemsField)) > if (itemFields.TryGetValue("Amount", out DocumentField itemAmountField)) > > > > > if (document.Fields.TryGetValue("SubTotal", out DocumentField subTotalField)) > if (document.Fields.TryGetValue("TotalTax", out DocumentField totalTaxField)) > if (document.Fields.TryGetValue("InvoiceTotal", out DocumentField invoiceTotalField)) > >
You are not limited to invoices! There are a couple of prebuilt models to choose from, each of which has its own set of supported fields. More information about the supported document types can be found in the service documentation.
For more information and samples, see here.
Build a Custom Model.
Build a custom model on your own document type. The resulting model can be used to analyze values from the types of documents it was built on.
// For this sample, you can use the training documents found in the `trainingFiles` folder. // Upload the documents to your storage container and then generate a container SAS URL. Note // that a container URI without SAS is accepted only when the container is public or has a // managed identity configured. // // For instructions to set up documents for training in an Azure Blob Storage Container, please see: // https://aka.ms/azsdk/formrecognizer/buildcustommodel Uri blobContainerUri = new Uri(""); var client = new DocumentModelAdministrationClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); // We are selecting the Template build mode in this sample. For more information about the available // build modes and their differences, please see: // https://aka.ms/azsdk/formrecognizer/buildmode BuildDocumentModelOperation operation = await client.BuildDocumentModelAsync(WaitUntil.Completed, blobContainerUri, DocumentBuildMode.Template); DocumentModelDetails model = operation.Value; Console.WriteLine($" Model Id: "); if (string.IsNullOrEmpty(model.Description)) Console.WriteLine($" Model description: "); Console.WriteLine($" Created on: "); Console.WriteLine(" Doc types the model can recognize:"); foreach (KeyValuePair documentType in model.DocumentTypes) >
For more information and samples see here.
Analyze Custom Documents.
Analyze text, field values, selection marks, and table structures, styles, and paragraphs from custom documents, using models you built with your own document types.
string modelId = ""; Uri fileUri = new Uri(""); AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, modelId, fileUri); AnalyzeResult result = operation.Value; Console.WriteLine($"Document was analyzed with model with ID: "); foreach (AnalyzedDocument document in result.Documents) >
For more information and samples see here.
Manage Models.
Manage the models stored in your account.
var client = new DocumentModelAdministrationClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); // Check number of custom models in the FormRecognizer account, and the maximum number of custom models that can be stored. ResourceDetails resourceDetails = await client.GetResourceDetailsAsync(); Console.WriteLine($"Resource has custom models."); Console.WriteLine($"It can have at most custom models."); // List the first ten or fewer models currently stored in the account. AsyncPageable models = client.GetDocumentModelsAsync(); int count = 0; await foreach (DocumentModelSummary modelSummary in models) // Create a new model to store in the account Uri blobContainerUri = new Uri(""); BuildDocumentModelOperation operation = await client.BuildDocumentModelAsync(WaitUntil.Completed, blobContainerUri, DocumentBuildMode.Template); DocumentModelDetails model = operation.Value; // Get the model that was just created DocumentModelDetails newCreatedModel = await client.GetDocumentModelAsync(model.ModelId); Console.WriteLine($"Custom Model with Id has the following information:"); Console.WriteLine($" Model Id: "); if (string.IsNullOrEmpty(newCreatedModel.Description)) Console.WriteLine($" Model description: "); Console.WriteLine($" Created on: "); // Delete the model from the account. await client.DeleteDocumentModelAsync(newCreatedModel.ModelId);
For more information and samples see here.
Manage Models Synchronously.
Manage the models stored in your account with a synchronous API.
var client = new DocumentModelAdministrationClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); // Check number of custom models in the FormRecognizer account, and the maximum number of custom models that can be stored. ResourceDetails resourceDetails = client.GetResourceDetails(); Console.WriteLine($"Resource has custom models."); Console.WriteLine($"It can have at most custom models."); // List the first ten or fewer models currently stored in the account. Pageable models = client.GetDocumentModels(); foreach (DocumentModelSummary modelSummary in models.Take(10)) // Create a new model to store in the account Uri blobContainerUri = new Uri(""); BuildDocumentModelOperation operation = client.BuildDocumentModel(WaitUntil.Completed, blobContainerUri, DocumentBuildMode.Template); DocumentModelDetails model = operation.Value; // Get the model that was just created DocumentModelDetails newCreatedModel = client.GetDocumentModel(model.ModelId); Console.WriteLine($"Custom Model with Id has the following information:"); Console.WriteLine($" Model Id: "); if (string.IsNullOrEmpty(newCreatedModel.Description)) Console.WriteLine($" Model description: "); Console.WriteLine($" Created on: "); // Delete the created model from the account. client.DeleteDocumentModel(newCreatedModel.ModelId);
Troubleshooting.
General.
When you interact with the Cognitive Services Form Recognizer client library using the .NET SDK, errors returned by the service will result in a RequestFailedException with the same HTTP status code returned by the REST API request.
For example, if you submit a receipt image with an invalid Uri , a 400 error is returned, indicating "Bad Request".
try catch (RequestFailedException e)
You will notice that additional information is logged, like the client request ID of the operation.
Message: Azure.RequestFailedException: Service request failed. Status: 400 (Bad Request) ErrorCode: InvalidRequest Content: >> Headers: Transfer-Encoding: chunked x-envoy-upstream-service-time: REDACTED apim-request-id: REDACTED Strict-Transport-Security: REDACTED X-Content-Type-Options: REDACTED Date: Fri, 01 Oct 2021 02:55:44 GMT Content-Type: application/json; charset=utf-8.
Error codes and messages raised by the Form Recognizer service can be found in the service documentation.
Setting up console logging.
The simplest way to see the logs is to enable the console logging. To create an Azure SDK log listener that outputs messages to console use the AzureEventSourceListener.CreateConsoleLogger method.
// Setup a listener to monitor logged events. using AzureEventSourceListener listener = AzureEventSourceListener.CreateConsoleLogger();
To learn more about other logging mechanisms see Diagnostics Samples.
Next steps.
Samples showing how to use the Cognitive Services Form Recognizer library are available in this GitHub repository. Samples are provided for each main functional area:
Extract the layout of a document Analyze with the prebuilt general document model Analyze with the prebuilt read model Analyze a document with a custom model Analyze a document with a prebuilt model Build a custom model Manage models Get and List document model operations Compose a model Copy a custom model between Form Recognizer resources.
Note that these samples use SDK version 4.0.0 . For lower versions of the SDK, please see Form Recognizer Samples for V3.1.X.
Contributing.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Product Versions .NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows .NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1 .NET Standard netstandard2.0 netstandard2.1 .NET Framework net461 net462 net463 net47 net471 net472 net48 MonoAndroid monoandroid MonoMac monomac MonoTouch monotouch Tizen tizen40 tizen60 Xamarin.iOS xamarinios Xamarin.Mac xamarinmac Xamarin.TVOS xamarintvos Xamarin.WatchOS xamarinwatchos.
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
.NETStandard 2.0.
Azure.Core (>= 1.25.0) System.Text.Json (>= 4.7.2)
NuGet packages (6)
Showing the top 5 NuGet packages that depend on Azure.AI.FormRecognizer:
Bootstrap UI OCR components experience.
The Machine Learning Microsoft Cognitive Services Library by Genocs.
The base that help using Azure Cognitive Services into .NET Core projects.
Modelo de datos de Maldivas.
My package description.
GitHub repositories.
This package is not used by any popular GitHub repositories.
Version Downloads Last updated 4.0.0 66 253 09.09.2022 4.0.0-beta.5 11 335 09.08.2022 4.0.0-beta.4 36 722 08.06.2022 4.0.0-beta.3 53 575 11.02.2022 4.0.0-beta.2 30 630 09.11.2021 4.0.0-beta.1 7 814 07.10.2021 3.1.1 269 303 09.06.2021 3.1.0 60 136 26.05.2021 3.1.0-beta.4 11 767 06.04.2021 3.1.0-beta.3 3 235 09.03.2021 3.1.0-beta.2 10 278 09.02.2021 3.1.0-beta.1 21 264 23.11.2022 3.0.1 16 646 09.04.2021 3.0.0 106 515 20.08.2022 3.0.0-preview.2 6 101 18.08.2022 1.0.0-preview.4 8 375 07.07.2022 1.0.0-preview.3 2 891 10.06.2022 1.0.0-preview.2 2 995 06.05.2022 1.0.0-preview.1 937 23.04.2022.