0

我在这里使用代码提取了发票字段以用于预建发票模型, https: //docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/quickstarts/client-library?tabs=preview%2Cv2-1&pivots =编程语言csharp#analyze-invoices

但是没有关于如何提取发票行项目的文档。我在这里看到此功能最近于 2021 年 3 月推出 https://docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/whats-new

谁能指导我如何使用预建模型获取发票行项目?

4

1 回答 1

0

请将表单识别器 SDK NuGet 更新到版本 v3.1.0-beta.4 以查看发票行项目。调用 SDK 并获取订单项详细信息的示例代码如下:

            FormRecognizerClient client = new FormRecognizerClient(new Uri(YOUR_FR_URL), new Azure.AzureKeyCredential(YOUR_FR_KEY));

            using (FileStream stream = new FileStream(@"SampleInvoice.pdf", FileMode.Open))
            {
                RecognizeInvoicesOptions options = new RecognizeInvoicesOptions() { IncludeFieldElements = true };
                var oper = await client.StartRecognizeInvoices(stream, options).WaitForCompletionAsync();
                var rawResponse = oper.GetRawResponse();
                rawResponse.ContentStream.Position = 0;
                using (var reader = new StreamReader(oper.GetRawResponse().ContentStream))
                {
                    Console.WriteLine($"Raw output:");
                    Console.WriteLine(reader.ReadToEnd());
                }

                foreach (var invoice in oper.Value)
                {
                    foreach (FormField field in invoice.Fields.Values)
                    {
                        Console.WriteLine($"Field {field.Name}: ");
                        Console.WriteLine($"    Value: {field.ValueData?.Text}");

                        if (field.Name == "Items")
                        {
                            var count = 0;
                            foreach (var lineField in field.Value.AsList())
                            {
                                count++;
                                Console.WriteLine($"     LineItem {count}: ");
                                foreach (var (lineFieldName, lineFieldValue) in lineField.Value.AsDictionary())
                                {
                                    Console.WriteLine($"        LineField: {lineFieldName}");
                                    Console.WriteLine($"            Value: {lineFieldValue.ValueData?.Text}");
                                }
                            }
                        }
                    }
                }
            }

于 2021-04-14T19:21:46.113 回答