// </snippet_bc_print> // <snippet_invoice_call> private static async Task AnalyzeInvoice( FormRecognizerClient recognizerClient, string invoiceUrl) { var options = new RecognizeInvoicesOptions() { Locale = "en-US" }; RecognizedFormCollection invoices = await recognizerClient.StartRecognizeInvoicesFromUriAsync(invoiceUrl, options).WaitForCompletionAsync(); // </snippet_invoice_call> // <snippet_invoice_print> 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}"); } } }
public async Task RecognizeInvoicesFromUri() { string endpoint = TestEnvironment.Endpoint; string apiKey = TestEnvironment.ApiKey; FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); Uri invoiceUri = FormRecognizerTestEnvironment.CreateUri("Invoice_1.pdf"); #region 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}"); } } }
public async Task RecognizeInvoicesFromUri() { string endpoint = TestEnvironment.Endpoint; string apiKey = TestEnvironment.ApiKey; FormRecognizerClient client = new FormRecognizerClient(new Uri(endpoint), new AzureKeyCredential(apiKey)); Uri invoiceUri = FormRecognizerTestEnvironment.CreateUri("recommended_invoice.jpg"); #region Snippet:FormRecognizerSampleRecognizeInvoicesUri //@@ Uri invoiceUri = <invoiceUri>; var options = new RecognizeInvoicesOptions() { Locale = "en-US" }; RecognizeInvoicesOperation operation = await client.StartRecognizeInvoicesFromUriAsync(invoiceUri, options); Response <RecognizedFormCollection> operationResponse = await operation.WaitForCompletionAsync(); RecognizedFormCollection invoices = operationResponse.Value; // To see the list of all the supported fields returned by service and its corresponding types, consult: // https://aka.ms/formrecognizer/invoicefields RecognizedForm invoice = invoices.Single(); if (invoice.Fields.TryGetValue("InvoiceId", out FormField invoiceIdField)) { if (invoiceIdField.Value.ValueType == FieldValueType.String) { string invoiceId = invoiceIdField.Value.AsString(); Console.WriteLine($"Invoice Id: '{invoiceId}', with confidence {invoiceIdField.Confidence}"); } } if (invoice.Fields.TryGetValue("VendorName", out FormField vendorNameField)) { if (vendorNameField.Value.ValueType == FieldValueType.String) { string vendorName = vendorNameField.Value.AsString(); Console.WriteLine($"Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}"); } } if (invoice.Fields.TryGetValue("CustomerName", out FormField customerNameField)) { if (customerNameField.Value.ValueType == FieldValueType.String) { string customerName = customerNameField.Value.AsString(); Console.WriteLine($"Customer Name: '{customerName}', with confidence {customerNameField.Confidence}"); } } if (invoice.Fields.TryGetValue("Items", out FormField itemsField)) { if (itemsField.Value.ValueType == FieldValueType.List) { foreach (FormField itemField in itemsField.Value.AsList()) { Console.WriteLine("Item:"); if (itemField.Value.ValueType == FieldValueType.Dictionary) { IReadOnlyDictionary <string, FormField> itemFields = itemField.Value.AsDictionary(); if (itemFields.TryGetValue("Description", out FormField itemDescriptionField)) { if (itemDescriptionField.Value.ValueType == FieldValueType.String) { string itemDescription = itemDescriptionField.Value.AsString(); Console.WriteLine($" Description: '{itemDescription}', with confidence {itemDescriptionField.Confidence}"); } } if (itemFields.TryGetValue("UnitPrice", out FormField itemUnitPriceField)) { if (itemUnitPriceField.Value.ValueType == FieldValueType.Float) { float itemUnitPrice = itemUnitPriceField.Value.AsFloat(); Console.WriteLine($" UnitPrice: '{itemUnitPrice}', with confidence {itemUnitPriceField.Confidence}"); } } if (itemFields.TryGetValue("Quantity", out FormField itemQuantityField)) { if (itemQuantityField.Value.ValueType == FieldValueType.Float) { float quantityAmount = itemQuantityField.Value.AsFloat(); Console.WriteLine($" Quantity: '{quantityAmount}', with confidence {itemQuantityField.Confidence}"); } } if (itemFields.TryGetValue("Amount", out FormField itemAmountField)) { if (itemAmountField.Value.ValueType == FieldValueType.Float) { float itemAmount = itemAmountField.Value.AsFloat(); Console.WriteLine($" Amount: '{itemAmount}', with confidence {itemAmountField.Confidence}"); } } } } } } if (invoice.Fields.TryGetValue("SubTotal", out FormField subTotalField)) { if (subTotalField.Value.ValueType == FieldValueType.Float) { float subTotal = subTotalField.Value.AsFloat(); Console.WriteLine($"Sub Total: '{subTotal}', with confidence {subTotalField.Confidence}"); } } if (invoice.Fields.TryGetValue("TotalTax", out FormField totalTaxField)) { if (totalTaxField.Value.ValueType == FieldValueType.Float) { float totalTax = totalTaxField.Value.AsFloat(); Console.WriteLine($"Total Tax: '{totalTax}', with confidence {totalTaxField.Confidence}"); } } if (invoice.Fields.TryGetValue("InvoiceTotal", out FormField invoiceTotalField)) { if (invoiceTotalField.Value.ValueType == FieldValueType.Float) { float invoiceTotal = invoiceTotalField.Value.AsFloat(); Console.WriteLine($"Invoice Total: '{invoiceTotal}', with confidence {invoiceTotalField.Confidence}"); } } }