|
| 1 | +// #define RUN_AS_ENTRY_OF_TOP_LEVEL_STATEMENT |
| 2 | +#if RUN_AS_ENTRY_OF_TOP_LEVEL_STATEMENT |
| 3 | + |
| 4 | +/* |
| 5 | + This code sample shows Prebuilt Layout operations with the Azure AI Document Intelligence client library. |
| 6 | +
|
| 7 | + To learn more, please visit the documentation - Quickstart: Document Intelligence (formerly Form Recognizer) SDKs |
| 8 | + https://learn.microsoft.com/azure/ai-services/document-intelligence/quickstarts/get-started-sdks-rest-api?pivots=programming-language-csharp |
| 9 | +*/ |
| 10 | + |
| 11 | +using Azure; |
| 12 | +using Azure.AI.DocumentIntelligence; |
| 13 | + |
| 14 | +/* |
| 15 | + Remember to remove the key from your code when you're done, and never post it publicly. For production, use |
| 16 | + secure methods to store and access your credentials. For more information, see |
| 17 | + https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-security?tabs=command-line%2Ccsharp#environment-variables-and-application-configuration |
| 18 | +*/ |
| 19 | + |
| 20 | +string endpoint = Environment.GetEnvironmentVariable("DOCUMENTINTELLIGENCE_ENDPOINT") ?? string.Empty; |
| 21 | +string key = Environment.GetEnvironmentVariable("DOCUMENTINTELLIGENCE_API_KEY") ?? string.Empty; |
| 22 | +if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(key)) |
| 23 | +{ |
| 24 | + Console.WriteLine("Please set the environment variables for the endpoint and key."); |
| 25 | + return; |
| 26 | +} |
| 27 | + |
| 28 | +AzureKeyCredential credential = new AzureKeyCredential(key); |
| 29 | +DocumentIntelligenceClient client = new DocumentIntelligenceClient(new Uri(endpoint), credential); |
| 30 | + |
| 31 | +// sample document |
| 32 | +Uri fileUri = new Uri("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"); |
| 33 | + |
| 34 | +Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-layout", fileUri); |
| 35 | + |
| 36 | +AnalyzeResult result = operation.Value; |
| 37 | + |
| 38 | +foreach (DocumentPage page in result.Pages) |
| 39 | +{ |
| 40 | + Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s),"); |
| 41 | + Console.WriteLine($"and {page.SelectionMarks.Count} selection mark(s)."); |
| 42 | + |
| 43 | + for (int i = 0; i < page.Lines.Count; i++) |
| 44 | + { |
| 45 | + DocumentLine line = page.Lines[i]; |
| 46 | + Console.WriteLine($" Line {i} has content: '{line.Content}'."); |
| 47 | + |
| 48 | + Console.WriteLine($" Its bounding box is:"); |
| 49 | + Console.WriteLine($" Upper left => X: {line.Polygon[0]}, Y= {line.Polygon[1]}"); |
| 50 | + Console.WriteLine($" Upper right => X: {line.Polygon[2]}, Y= {line.Polygon[3]}"); |
| 51 | + Console.WriteLine($" Lower right => X: {line.Polygon[4]}, Y= {line.Polygon[5]}"); |
| 52 | + Console.WriteLine($" Lower left => X: {line.Polygon[6]}, Y= {line.Polygon[7]}"); |
| 53 | + } |
| 54 | + |
| 55 | + for (int i = 0; i < page.SelectionMarks.Count; i++) |
| 56 | + { |
| 57 | + DocumentSelectionMark selectionMark = page.SelectionMarks[i]; |
| 58 | + |
| 59 | + Console.WriteLine($" Selection Mark {i} is {selectionMark.State}."); |
| 60 | + Console.WriteLine($" Its bounding box is:"); |
| 61 | + Console.WriteLine($" Upper left => X: {selectionMark.Polygon[0]}, Y= {selectionMark.Polygon[1]}"); |
| 62 | + Console.WriteLine($" Upper right => X: {selectionMark.Polygon[2]}, Y= {selectionMark.Polygon[3]}"); |
| 63 | + Console.WriteLine($" Lower right => X: {selectionMark.Polygon[4]}, Y= {selectionMark.Polygon[5]}"); |
| 64 | + Console.WriteLine($" Lower left => X: {selectionMark.Polygon[6]}, Y= {selectionMark.Polygon[7]}"); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +foreach (DocumentStyle style in result.Styles) |
| 69 | +{ |
| 70 | + // Check the style and style confidence to see if text is handwritten. |
| 71 | + // Note that value '0.8' is used as an example. |
| 72 | + |
| 73 | + bool isHandwritten = style.IsHandwritten.HasValue && style.IsHandwritten == true; |
| 74 | + |
| 75 | + if (isHandwritten && style.Confidence > 0.8) |
| 76 | + { |
| 77 | + Console.WriteLine($"Handwritten content found:"); |
| 78 | + |
| 79 | + foreach (DocumentSpan span in style.Spans) |
| 80 | + { |
| 81 | + Console.WriteLine($" Content: {result.Content.Substring(span.Offset, span.Length)}"); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +Console.WriteLine("The following tables were extracted:"); |
| 87 | + |
| 88 | +for (int i = 0; i < result.Tables.Count; i++) |
| 89 | +{ |
| 90 | + DocumentTable table = result.Tables[i]; |
| 91 | + Console.WriteLine($" Table {i} has {table.RowCount} rows and {table.ColumnCount} columns."); |
| 92 | + |
| 93 | + foreach (DocumentTableCell cell in table.Cells) |
| 94 | + { |
| 95 | + Console.WriteLine($" Cell ({cell.RowIndex}, {cell.ColumnIndex}) has kind '{cell.Kind}' and content: '{cell.Content}'."); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#endif |
0 commit comments