diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/README.md b/sdk/formrecognizer/Azure.AI.FormRecognizer/README.md index aa9d49dc75fc3..f9d6e56fcb1d8 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/README.md +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/README.md @@ -3,8 +3,10 @@ Azure Cognitive Services Form Recognizer is a cloud service that uses machine le - Recognize Custom Forms - Recognize and extract form fields and other content from your custom forms, using models you trained with your own form types. - Recognize Form Content - Recognize and extract tables, lines, words, and selection marks like radio buttons and check boxes in forms documents, without the need to train a model. -- Recognize Receipts - Recognize and extract common fields from receipts, using a pre-trained receipt model. -- Recognize Business Card - Recognize and extract common fields from business cards, using a pre-trained business cards model. +- Recognize Prebuilt models - Recognize data using the following prebuilt models: + - Receipts - Recognize and extract common fields from receipts, using a pre-trained receipt model. + - Business Cards - Recognize and extract common fields from business cards, using a pre-trained business cards model. + - Invoices - Recognize and extract common fields from invoices, using a pre-trained invoice model. [Source code][formreco_client_src] | [Package (NuGet)][formreco_nuget_package] | [API reference documentation][formreco_refdocs] | [Product documentation][formreco_docs] | [Samples][formreco_samples] @@ -101,10 +103,12 @@ var client = new FormRecognizerClient(new Uri(endpoint), new DefaultAzureCredent `FormRecognizerClient` provides operations for: - - Recognizing form fields and content, using custom models trained to recognize your custom forms. These values are returned in a collection of `RecognizedForm` objects. See example [Recognize Custom Forms](#recognize-custom-forms). - - Recognizing form content, including tables, lines, words, and selection marks like radio buttons and check boxes without the need to train a model. Form content is returned in a collection of `FormPage` objects. See example [Recognize Content](#recognize-content). - - Recognizing common fields from receipts, using a pre-trained receipt model on the Form Recognizer service. These fields and meta-data are returned in a collection of `RecognizedForm` objects. See example [Recognize Receipts](#recognize-receipts). -- Recognizing common fields from business cards, using a pre-trained business cards model on the Form Recognizer service. These fields and meta-data are returned in a collection of `RecognizedForm` objects. See example [Recognize Business Cards](#recognize-business-cards). +- Recognizing form fields and content, using custom models trained to recognize your custom forms. These values are returned in a collection of `RecognizedForm` objects. See example [Recognize Custom Forms](#recognize-custom-forms). +- Recognizing form content, including tables, lines, words, and selection marks like radio buttons and check boxes without the need to train a model. Form content is returned in a collection of `FormPage` objects. See example [Recognize Content](#recognize-content). +- Recognizing common fields from the following form types using prebuilt models. These fields and meta-data are returned in a collection of `RecognizedForm` objects. + - Sales receipts. See example [Recognize Receipts](#recognize-receipts). + - Business cards. See example [Recognize Business Cards](#recognize-business-cards). + - Invoices. See example [Recognize Invoices](#recognize-invoices). ### FormTrainingClient @@ -134,6 +138,7 @@ The following section provides several code snippets illustrating common pattern * [Recognize Custom Forms](#recognize-custom-forms) * [Recognize Receipts](#recognize-receipts) * [Recognize Business Cards](#recognize-business-cards) +* [Recognize Invoices](#recognize-invoices) * [Train a Model](#train-a-model) * [Manage Custom Models](#manage-custom-models) @@ -299,7 +304,7 @@ using (FileStream stream = new FileStream(receiptPath, FileMode.Open)) Recognize data from business cards using a prebuilt model. Business card fields recognized by the service can be found [here][service_recognize_business_cards_fields]. ```C# Snippet:FormRecognizerSampleRecognizeBusinessCardFileStream -using (FileStream stream = new FileStream(busienssCardsPath, FileMode.Open)) +using (FileStream stream = new FileStream(businessCardsPath, FileMode.Open)) { var options = new RecognizeBusinessCardsOptions() { Locale = "en-US" }; RecognizedFormCollection businessCards = await client.StartRecognizeBusinessCardsAsync(stream, options).WaitForCompletionAsync(); @@ -368,6 +373,52 @@ using (FileStream stream = new FileStream(busienssCardsPath, FileMode.Open)) } ``` +### Recognize Invoices +Recognize data from invoices using a prebuilt model. Invoices fields recognized by the service can be found [here][service_recognize_invoices_fields]. + +```C# Snippet:FormRecognizerSampleRecognizeInvoicesFileStream +using (FileStream stream = new FileStream(invoicePath, FileMode.Open)) +{ + var options = new RecognizeInvoicesOptions() { Locale = "en-US" }; + RecognizedFormCollection invoices = await client.StartRecognizeInvoicesAsync(stream, options).WaitForCompletionAsync(); + + // To see the list of the supported fields returned by service and its corresponding types, consult: + // https://aka.ms/formrecognizer/invoicefields + + RecognizedForm invoice = invoices.Single(); + + FormField vendorNameField; + if (invoice.Fields.TryGetValue("VendorName", out vendorNameField)) + { + if (vendorNameField.Value.ValueType == FieldValueType.String) + { + string vendorName = vendorNameField.Value.AsString(); + Console.WriteLine($" Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}"); + } + } + + FormField customerNameField; + if (invoice.Fields.TryGetValue("CustomerName", out customerNameField)) + { + if (customerNameField.Value.ValueType == FieldValueType.String) + { + string customerName = customerNameField.Value.AsString(); + Console.WriteLine($" Customer Name: '{customerName}', with confidence {customerNameField.Confidence}"); + } + } + + FormField invoiceTotalField; + if (invoice.Fields.TryGetValue("InvoiceTotal", out invoiceTotalField)) + { + if (invoiceTotalField.Value.ValueType == FieldValueType.Float) + { + float invoiceTotal = invoiceTotalField.Value.AsFloat(); + Console.WriteLine($" Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}"); + } + } +} +``` + ### Train a Model Train a machine-learned model on your own form types. The resulting model will be able to recognize values from the types of forms it was trained on. @@ -602,6 +653,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con [labeling_tool]: https://docs.microsoft.com/azure/cognitive-services/form-recognizer/quickstarts/label-tool [service_recognize_receipt_fields]: https://aka.ms/formrecognizer/receiptfields [service_recognize_business_cards_fields]: https://aka.ms/formrecognizer/businesscardfields +[service_recognize_invoices_fields]: https://aka.ms/formrecognizer/invoicefields [dotnet_lro_guidelines]: https://azure.github.io/azure-sdk/dotnet_introduction.html#dotnet-longrunning [logging]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/core/Azure.Core/samples/Diagnostics.md diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/README.md b/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/README.md index 66d888eb556df..9540c3db702ef 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/README.md +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/README.md @@ -23,6 +23,7 @@ Azure Cognitive Services Form Recognizer is a cloud service that uses machine le - [Recognize custom forms](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample2_RecognizeCustomForms.md) - [Recognize receipts](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample3_RecognizeReceipts.md) - [Recognize business cards](https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample9_RecognizeBusinessCards.md) + - [Train a model](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample5_TrainModel.md) - [Manage custom models](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample6_ManageCustomModels.md) diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample10_RecognizeInvoices.md b/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample10_RecognizeInvoices.md new file mode 100644 index 0000000000000..459310a6bd44f --- /dev/null +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample10_RecognizeInvoices.md @@ -0,0 +1,178 @@ +# Recognize invoices + +This sample demonstrates how to recognize and extract common fields from invoices, using a pre-trained model. For a suggested approach to extracting information from invoices, see [strongly-typing a recognized form][strongly_typing_a_recognized_form]. + +To get started you'll need a Cognitive Services resource or a Form Recognizer resource. See [README][README] for prerequisites and instructions. + +## Creating a `FormRecognizerClient` + +To create a new `FormRecognizerClient` you need the endpoint and credentials from your resource. In the sample below you'll use a Form Recognizer API key credential by creating an `AzureKeyCredential` object, that if needed, will allow you to update the API key without creating a new client. + +You can set `endpoint` and `apiKey` based on an environment variable, a configuration setting, or any way that works for your application. + +```C# Snippet:CreateFormRecognizerClient +string endpoint = ""; +string apiKey = ""; +var credential = new AzureKeyCredential(apiKey); +var client = new FormRecognizerClient(new Uri(endpoint), credential); +``` + +## Recognize invoices from a URI + +To recognize invoices from a URI, use the `StartRecognizeInvoicesFromUriAsync` method. + +```C# Snippet:FormRecognizerSampleRecognizeInvoicesUri + var options = new RecognizeInvoicesOptions() { Locale = "en-US" }; + RecognizedFormCollection invoices = await client.StartRecognizeInvoicesFromUriAsync(invoiceUri, options).WaitForCompletionAsync(); + + // To see the list of the supported fields returned by service and its corresponding types, consult: + // https://aka.ms/formrecognizer/invoicefields + + RecognizedForm invoice = invoices.Single(); + + FormField invoiceIdField; + if (invoice.Fields.TryGetValue("InvoiceId", out invoiceIdField)) + { + if (invoiceIdField.Value.ValueType == FieldValueType.String) + { + string invoiceId = invoiceIdField.Value.AsString(); + Console.WriteLine($" Invoice Id: '{invoiceId}', with confidence {invoiceIdField.Confidence}"); + } + } + + FormField invoiceDateField; + if (invoice.Fields.TryGetValue("InvoiceDate", out invoiceDateField)) + { + if (invoiceDateField.Value.ValueType == FieldValueType.Date) + { + DateTime invoiceDate = invoiceDateField.Value.AsDate(); + Console.WriteLine($" Invoice Date: '{invoiceDate}', with confidence {invoiceDateField.Confidence}"); + } + } + + FormField dueDateField; + if (invoice.Fields.TryGetValue("DueDate", out dueDateField)) + { + if (dueDateField.Value.ValueType == FieldValueType.Date) + { + DateTime dueDate = dueDateField.Value.AsDate(); + Console.WriteLine($" Due Date: '{dueDate}', with confidence {dueDateField.Confidence}"); + } + } + + FormField vendorNameField; + if (invoice.Fields.TryGetValue("VendorName", out vendorNameField)) + { + if (vendorNameField.Value.ValueType == FieldValueType.String) + { + string vendorName = vendorNameField.Value.AsString(); + Console.WriteLine($" Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}"); + } + } + + FormField vendorAddressField; + if (invoice.Fields.TryGetValue("VendorAddress", out vendorAddressField)) + { + if (vendorAddressField.Value.ValueType == FieldValueType.String) + { + string vendorAddress = vendorAddressField.Value.AsString(); + Console.WriteLine($" Vendor Address: '{vendorAddress}', with confidence {vendorAddressField.Confidence}"); + } + } + + FormField customerNameField; + if (invoice.Fields.TryGetValue("CustomerName", out customerNameField)) + { + if (customerNameField.Value.ValueType == FieldValueType.String) + { + string customerName = customerNameField.Value.AsString(); + Console.WriteLine($" Customer Name: '{customerName}', with confidence {customerNameField.Confidence}"); + } + } + + FormField customerAddressField; + if (invoice.Fields.TryGetValue("CustomerAddress", out customerAddressField)) + { + if (customerAddressField.Value.ValueType == FieldValueType.String) + { + string customerAddress = customerAddressField.Value.AsString(); + Console.WriteLine($" Customer Address: '{customerAddress}', with confidence {customerAddressField.Confidence}"); + } + } + + FormField customerAddressRecipientField; + if (invoice.Fields.TryGetValue("CustomerAddressRecipient", out customerAddressRecipientField)) + { + if (customerAddressRecipientField.Value.ValueType == FieldValueType.String) + { + string customerAddressRecipient = customerAddressRecipientField.Value.AsString(); + Console.WriteLine($" Customer address recipient: '{customerAddressRecipient}', with confidence {customerAddressRecipientField.Confidence}"); + } + } + + FormField invoiceTotalField; + if (invoice.Fields.TryGetValue("InvoiceTotal", out invoiceTotalField)) + { + if (invoiceTotalField.Value.ValueType == FieldValueType.Float) + { + float invoiceTotal = invoiceTotalField.Value.AsFloat(); + Console.WriteLine($" Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}"); + } + } +} +``` + +## Recognize invoices from a given file + +To recognize invoices from a given file, use the `StartRecognizeInvoicesAsync` method. + +```C# Snippet:FormRecognizerSampleRecognizeInvoicesFileStream +using (FileStream stream = new FileStream(invoicePath, FileMode.Open)) +{ + var options = new RecognizeInvoicesOptions() { Locale = "en-US" }; + RecognizedFormCollection invoices = await client.StartRecognizeInvoicesAsync(stream, options).WaitForCompletionAsync(); + + // To see the list of the supported fields returned by service and its corresponding types, consult: + // https://aka.ms/formrecognizer/invoicefields + + RecognizedForm invoice = invoices.Single(); + + FormField vendorNameField; + if (invoice.Fields.TryGetValue("VendorName", out vendorNameField)) + { + if (vendorNameField.Value.ValueType == FieldValueType.String) + { + string vendorName = vendorNameField.Value.AsString(); + Console.WriteLine($" Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}"); + } + } + + FormField customerNameField; + if (invoice.Fields.TryGetValue("CustomerName", out customerNameField)) + { + if (customerNameField.Value.ValueType == FieldValueType.String) + { + string customerName = customerNameField.Value.AsString(); + Console.WriteLine($" Customer Name: '{customerName}', with confidence {customerNameField.Confidence}"); + } + } + + FormField invoiceTotalField; + if (invoice.Fields.TryGetValue("InvoiceTotal", out invoiceTotalField)) + { + if (invoiceTotalField.Value.ValueType == FieldValueType.Float) + { + float invoiceTotal = invoiceTotalField.Value.AsFloat(); + Console.WriteLine($" Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}"); + } + } +} +``` + +To see the full example source files, see: + +* [Recognize invoices from URI](https://github.com/Azure/azure-sdk-for-net/blob/feature/formrecognizer2.1/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromUri.cs) +* [Recognize invoices from file](https://github.com/Azure/azure-sdk-for-net/blob/feature/formrecognizer2.1/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromFile.cs) + +[README]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer#getting-started +[strongly_typing_a_recognized_form]: https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/formrecognizer/Azure.AI.FormRecognizer/samples/Sample4_StronglyTypingARecognizedForm.md \ No newline at end of file diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample12_RecognizeBusinessCardsFromFile.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample12_RecognizeBusinessCardsFromFile.cs index ff758861cb3c4..4ebb4f7a06788 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample12_RecognizeBusinessCardsFromFile.cs +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample12_RecognizeBusinessCardsFromFile.cs @@ -22,10 +22,10 @@ public async Task RecognizeBusinessCardsFromFile() FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); - string busienssCardsPath = FormRecognizerTestEnvironment.CreatePath("businessCard.jpg"); + string businessCardsPath = FormRecognizerTestEnvironment.CreatePath("businessCard.jpg"); #region Snippet:FormRecognizerSampleRecognizeBusinessCardFileStream - using (FileStream stream = new FileStream(busienssCardsPath, FileMode.Open)) + using (FileStream stream = new FileStream(businessCardsPath, FileMode.Open)) { var options = new RecognizeBusinessCardsOptions() { Locale = "en-US" }; RecognizedFormCollection businessCards = await client.StartRecognizeBusinessCardsAsync(stream, options).WaitForCompletionAsync(); diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromFile.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromFile.cs index 36d9851e02eb9..fa2147aafb651 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromFile.cs +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromFile.cs @@ -3,6 +3,7 @@ using System; using System.IO; +using System.Linq; using System.Threading.Tasks; using Azure.AI.FormRecognizer.Models; using Azure.AI.FormRecognizer.Tests; @@ -32,96 +33,35 @@ public async Task RecognizeInvoicesFromFile() // To see the list of the supported fields returned by service and its corresponding types, consult: // https://aka.ms/formrecognizer/invoicefields - foreach (RecognizedForm invoice in invoices) - { - FormField invoiceIdField; - if (invoice.Fields.TryGetValue("InvoiceId", out invoiceIdField)) - { - if (invoiceIdField.Value.ValueType == FieldValueType.String) - { - string invoiceId = invoiceIdField.Value.AsString(); - Console.WriteLine($" Invoice Id: '{invoiceId}', with confidence {invoiceIdField.Confidence}"); - } - } - - FormField invoiceDateField; - if (invoice.Fields.TryGetValue("InvoiceDate", out invoiceDateField)) - { - if (invoiceDateField.Value.ValueType == FieldValueType.Date) - { - DateTime invoiceDate = invoiceDateField.Value.AsDate(); - Console.WriteLine($" Invoice Date: '{invoiceDate}', with confidence {invoiceDateField.Confidence}"); - } - } - - FormField dueDateField; - if (invoice.Fields.TryGetValue("DueDate", out dueDateField)) - { - if (dueDateField.Value.ValueType == FieldValueType.Date) - { - DateTime dueDate = dueDateField.Value.AsDate(); - Console.WriteLine($" Due Date: '{dueDate}', with confidence {dueDateField.Confidence}"); - } - } - - FormField vendorNameField; - if (invoice.Fields.TryGetValue("VendorName", out vendorNameField)) - { - if (vendorNameField.Value.ValueType == FieldValueType.String) - { - string vendorName = vendorNameField.Value.AsString(); - Console.WriteLine($" Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}"); - } - } + RecognizedForm invoice = invoices.Single(); - FormField vendorAddressField; - if (invoice.Fields.TryGetValue("VendorAddress", out vendorAddressField)) - { - if (vendorAddressField.Value.ValueType == FieldValueType.String) - { - string vendorAddress = vendorAddressField.Value.AsString(); - Console.WriteLine($" Vendor Address: '{vendorAddress}', with confidence {vendorAddressField.Confidence}"); - } - } - - FormField customerNameField; - if (invoice.Fields.TryGetValue("CustomerName", out customerNameField)) - { - if (customerNameField.Value.ValueType == FieldValueType.String) - { - string customerName = customerNameField.Value.AsString(); - Console.WriteLine($" Customer Name: '{customerName}', with confidence {customerNameField.Confidence}"); - } - } - - FormField customerAddressField; - if (invoice.Fields.TryGetValue("CustomerAddress", out customerAddressField)) + FormField vendorNameField; + if (invoice.Fields.TryGetValue("VendorName", out vendorNameField)) + { + if (vendorNameField.Value.ValueType == FieldValueType.String) { - if (customerAddressField.Value.ValueType == FieldValueType.String) - { - string customerAddress = customerAddressField.Value.AsString(); - Console.WriteLine($" Customer Address: '{customerAddress}', with confidence {customerAddressField.Confidence}"); - } + string vendorName = vendorNameField.Value.AsString(); + Console.WriteLine($" Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}"); } + } - FormField customerAddressRecipientField; - if (invoice.Fields.TryGetValue("CustomerAddressRecipient", out customerAddressRecipientField)) + FormField customerNameField; + if (invoice.Fields.TryGetValue("CustomerName", out customerNameField)) + { + if (customerNameField.Value.ValueType == FieldValueType.String) { - if (customerAddressRecipientField.Value.ValueType == FieldValueType.String) - { - string customerAddressRecipient = customerAddressRecipientField.Value.AsString(); - Console.WriteLine($" Customer address recipient: '{customerAddressRecipient}', with confidence {customerAddressRecipientField.Confidence}"); - } + string customerName = customerNameField.Value.AsString(); + Console.WriteLine($" Customer Name: '{customerName}', with confidence {customerNameField.Confidence}"); } + } - FormField invoiceTotalField; - if (invoice.Fields.TryGetValue("InvoiceTotal", out invoiceTotalField)) + FormField invoiceTotalField; + if (invoice.Fields.TryGetValue("InvoiceTotal", out invoiceTotalField)) + { + if (invoiceTotalField.Value.ValueType == FieldValueType.Float) { - if (invoiceTotalField.Value.ValueType == FieldValueType.Float) - { - float invoiceTotal = invoiceTotalField.Value.AsFloat(); - Console.WriteLine($" Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}"); - } + float invoiceTotal = invoiceTotalField.Value.AsFloat(); + Console.WriteLine($" Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}"); } } } diff --git a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromUri.cs b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromUri.cs index d9a52fd8491b0..c03449daff4c5 100644 --- a/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromUri.cs +++ b/sdk/formrecognizer/Azure.AI.FormRecognizer/tests/samples/Sample13_RecognizeInvoicesFromUri.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using System.Linq; using System.Threading.Tasks; using Azure.AI.FormRecognizer.Models; using Azure.AI.FormRecognizer.Tests; @@ -29,96 +30,95 @@ public async Task RecognizeInvoicesFromUri() // To see the list of the supported fields returned by service and its corresponding types, consult: // https://aka.ms/formrecognizer/invoicefields - foreach (RecognizedForm invoice in invoices) + RecognizedForm invoice = invoices.Single(); + + FormField invoiceIdField; + if (invoice.Fields.TryGetValue("InvoiceId", out invoiceIdField)) { - FormField invoiceIdField; - if (invoice.Fields.TryGetValue("InvoiceId", out invoiceIdField)) + if (invoiceIdField.Value.ValueType == FieldValueType.String) { - if (invoiceIdField.Value.ValueType == FieldValueType.String) - { - string invoiceId = invoiceIdField.Value.AsString(); - Console.WriteLine($" Invoice Id: '{invoiceId}', with confidence {invoiceIdField.Confidence}"); - } + string invoiceId = invoiceIdField.Value.AsString(); + Console.WriteLine($" Invoice Id: '{invoiceId}', with confidence {invoiceIdField.Confidence}"); } + } - FormField invoiceDateField; - if (invoice.Fields.TryGetValue("InvoiceDate", out invoiceDateField)) + FormField invoiceDateField; + if (invoice.Fields.TryGetValue("InvoiceDate", out invoiceDateField)) + { + if (invoiceDateField.Value.ValueType == FieldValueType.Date) { - if (invoiceDateField.Value.ValueType == FieldValueType.Date) - { - DateTime invoiceDate = invoiceDateField.Value.AsDate(); - Console.WriteLine($" Invoice Date: '{invoiceDate}', with confidence {invoiceDateField.Confidence}"); - } + DateTime invoiceDate = invoiceDateField.Value.AsDate(); + Console.WriteLine($" Invoice Date: '{invoiceDate}', with confidence {invoiceDateField.Confidence}"); } + } - FormField dueDateField; - if (invoice.Fields.TryGetValue("DueDate", out dueDateField)) + FormField dueDateField; + if (invoice.Fields.TryGetValue("DueDate", out dueDateField)) + { + if (dueDateField.Value.ValueType == FieldValueType.Date) { - if (dueDateField.Value.ValueType == FieldValueType.Date) - { - DateTime dueDate = dueDateField.Value.AsDate(); - Console.WriteLine($" Due Date: '{dueDate}', with confidence {dueDateField.Confidence}"); - } + DateTime dueDate = dueDateField.Value.AsDate(); + Console.WriteLine($" Due Date: '{dueDate}', with confidence {dueDateField.Confidence}"); } + } - FormField vendorNameField; - if (invoice.Fields.TryGetValue("VendorName", out vendorNameField)) + FormField vendorNameField; + if (invoice.Fields.TryGetValue("VendorName", out vendorNameField)) + { + if (vendorNameField.Value.ValueType == FieldValueType.String) { - if (vendorNameField.Value.ValueType == FieldValueType.String) - { - string vendorName = vendorNameField.Value.AsString(); - Console.WriteLine($" Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}"); - } + string vendorName = vendorNameField.Value.AsString(); + Console.WriteLine($" Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}"); } + } - FormField vendorAddressField; - if (invoice.Fields.TryGetValue("VendorAddress", out vendorAddressField)) + FormField vendorAddressField; + if (invoice.Fields.TryGetValue("VendorAddress", out vendorAddressField)) + { + if (vendorAddressField.Value.ValueType == FieldValueType.String) { - if (vendorAddressField.Value.ValueType == FieldValueType.String) - { - string vendorAddress = vendorAddressField.Value.AsString(); - Console.WriteLine($" Vendor Address: '{vendorAddress}', with confidence {vendorAddressField.Confidence}"); - } + string vendorAddress = vendorAddressField.Value.AsString(); + Console.WriteLine($" Vendor Address: '{vendorAddress}', with confidence {vendorAddressField.Confidence}"); } + } - FormField customerNameField; - if (invoice.Fields.TryGetValue("CustomerName", out customerNameField)) + FormField customerNameField; + if (invoice.Fields.TryGetValue("CustomerName", out customerNameField)) + { + if (customerNameField.Value.ValueType == FieldValueType.String) { - if (customerNameField.Value.ValueType == FieldValueType.String) - { - string customerName = customerNameField.Value.AsString(); - Console.WriteLine($" Customer Name: '{customerName}', with confidence {customerNameField.Confidence}"); - } + string customerName = customerNameField.Value.AsString(); + Console.WriteLine($" Customer Name: '{customerName}', with confidence {customerNameField.Confidence}"); } + } - FormField customerAddressField; - if (invoice.Fields.TryGetValue("CustomerAddress", out customerAddressField)) + FormField customerAddressField; + if (invoice.Fields.TryGetValue("CustomerAddress", out customerAddressField)) + { + if (customerAddressField.Value.ValueType == FieldValueType.String) { - if (customerAddressField.Value.ValueType == FieldValueType.String) - { - string customerAddress = customerAddressField.Value.AsString(); - Console.WriteLine($" Customer Address: '{customerAddress}', with confidence {customerAddressField.Confidence}"); - } + string customerAddress = customerAddressField.Value.AsString(); + Console.WriteLine($" Customer Address: '{customerAddress}', with confidence {customerAddressField.Confidence}"); } + } - FormField customerAddressRecipientField; - if (invoice.Fields.TryGetValue("CustomerAddressRecipient", out customerAddressRecipientField)) + FormField customerAddressRecipientField; + if (invoice.Fields.TryGetValue("CustomerAddressRecipient", out customerAddressRecipientField)) + { + if (customerAddressRecipientField.Value.ValueType == FieldValueType.String) { - if (customerAddressRecipientField.Value.ValueType == FieldValueType.String) - { - string customerAddressRecipient = customerAddressRecipientField.Value.AsString(); - Console.WriteLine($" Customer address recipient: '{customerAddressRecipient}', with confidence {customerAddressRecipientField.Confidence}"); - } + string customerAddressRecipient = customerAddressRecipientField.Value.AsString(); + Console.WriteLine($" Customer address recipient: '{customerAddressRecipient}', with confidence {customerAddressRecipientField.Confidence}"); } + } - FormField invoiceTotalField; - if (invoice.Fields.TryGetValue("InvoiceTotal", out invoiceTotalField)) + FormField invoiceTotalField; + if (invoice.Fields.TryGetValue("InvoiceTotal", out invoiceTotalField)) + { + if (invoiceTotalField.Value.ValueType == FieldValueType.Float) { - if (invoiceTotalField.Value.ValueType == FieldValueType.Float) - { - float invoiceTotal = invoiceTotalField.Value.AsFloat(); - Console.WriteLine($" Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}"); - } + float invoiceTotal = invoiceTotalField.Value.AsFloat(); + Console.WriteLine($" Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}"); } } }