Not Receiving KeyvaluePair in the response in Azure Invoice Parser using .Net Sdk

Ashik Chittari 0 Reputation points
2023-09-14T08:40:55.2233333+00:00
   var client = new FormRecognizerClient(new Uri(endPoint), credential);
                Stream stream = file.OpenReadStream();
                var options = new RecognizeInvoicesOptions() { Locale = parserServiceLocale, Pages = { pageNumber.ToString() }, IncludeFieldElements = true };
                RecognizeInvoicesOperation operation = await client.StartRecognizeInvoicesAsync(stream, options);

.NET
.NET
Microsoft Technologies based on the .NET software framework.
2,048 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
3,560 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
8,970 questions
Azure Document Intelligence
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
1,677 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JasonPan - MSFT 2,456 Reputation points Microsoft Vendor
    2023-09-14T14:05:00.7933333+00:00

    Hi @Ashik Chittari,

    Does the gif below is you want ?

    BlazorApp1

    Here is the sample code:

    using Azure.AI.FormRecognizer.DocumentAnalysis;
    using Azure;
    using Microsoft.AspNetCore.Mvc;
    using Azure.AI.FormRecognizer;
    using Azure.AI.FormRecognizer.Models;
    
    namespace signalr_iwa.Controllers
    {
        public class TestController : Controller
        {
            private readonly IWebHostEnvironment _host;
    
            public TestController(IWebHostEnvironment host) {
                _host = host;
            }
    
            private async Task WriteSseEventAsync(HttpResponse response, string data)
            {
                await response.WriteAsync($"data: {data}\n\n");
                await response.Body.FlushAsync();
            }
    
    
            public async Task formrecognizer() {
                string endpoint = "https://myformrecognizer.cognitiveservices.azure.com/";
                string apiKey = "0631fa0****0f63723";
                var credential = new AzureKeyCredential(apiKey);
                var client = new FormRecognizerClient(new Uri(endpoint), credential);
                string invoicePath = Path.Combine(_host.WebRootPath, "pdf", "sample-invoice.pdf");
                using var stream = new FileStream(invoicePath, FileMode.Open);
                var options = new RecognizeInvoicesOptions()
                {
                    IncludeFieldElements = true
                };
                var operation = client.StartRecognizeInvoices(stream, options);
    
                Response<RecognizedFormCollection> operationResponse = operation.WaitForCompletionAsync().Result;
                RecognizedFormCollection recognizedInvoices = operationResponse.Value;
                var response = Response;
                response.Headers.Add("Content-Type", "text/event-stream; charset=utf-8");
                foreach (var recognizedForm in recognizedInvoices)
                {
                    Console.WriteLine($"Form type: {recognizedForm.FormType}");
                    foreach (var field in recognizedForm.Fields)
                    {
                        if (field.Value.ValueData != null) {
                            Console.WriteLine($"Field name: {field.Key}, Value: {field.Value.ValueData.Text}, Confidence: {field.Value.Confidence}");
                            await WriteSseEventAsync(response, $"Field name: {field.Key}, Value: {field.Value.ValueData.Text}, Confidence: {field.Value.Confidence}");
                            //await Task.Delay(800);
                        }
                    }
    
                }
            }
        }
    }
    
    

    I save the pdf file under the wwwroot.
    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Jason

    0 comments No comments