Skip to content

Commit f23232c

Browse files
committed
resolve feedback
1 parent 85b0cc9 commit f23232c

File tree

2 files changed

+1
-329
lines changed

2 files changed

+1
-329
lines changed
-12.8 KB
Loading

articles/applied-ai-services/form-recognizer/quickstarts/try-v3-csharp-sdk.md

Lines changed: 1 addition & 329 deletions
Original file line numberDiff line numberDiff line change
@@ -344,333 +344,6 @@ foreach (DocumentPage page in result.Pages)
344344
}
345345
}
346346

347-
foreach (DocumentStyle style in result.Styles)
348-
{
349-
// Check the style and style confidence to see if text is handwritten.
350-
// Note that value '0.8' is used as an example.
351-
352-
bool isHandwritten = style.IsHandwrGet started with Azure Form Recognizer using the C# programming language. Azure Form Recognizer is a cloud-based Azure Applied AI Service that uses machine learning to extract key-value pairs, text, and tables from your documents. You can easily call Form Recognizer models by integrating our client library SDks into your workflows and applications. We recommend that you use the free service when you're learning the technology. Remember that the number of free pages is limited to 500 per month.
353-
354-
To learn more about Form Recognizer features and development options, visit our [Overview](../overview.md#form-recognizer-features-and-development-options) page.
355-
356-
In this quickstart, you'll use following features to analyze and extract data and values from forms and documents:
357-
358-
* [🆕 **General document model**](#general-document-model)—Analyze and extract text, tables, structure, key-value pairs, and named entities.
359-
360-
* [**Layout model**](#layout-model)—Analyze and extract tables, lines, words, and selection marks like radio buttons and check boxes in forms documents, without the need to train a model.
361-
362-
* [**Prebuilt model**](#prebuilt-model)—Analyze and extract common fields from specific document types using a pre-trained model.
363-
364-
## Prerequisites
365-
366-
* Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services/).
367-
368-
* The current version of [Visual Studio IDE](https://visualstudio.microsoft.com/vs/). <!-- or [.NET Core](https://dotnet.microsoft.com/download). -->
369-
370-
* A Cognitive Services or Form Recognizer resource. Once you have your Azure subscription, create a [single-service](https://portal.azure.com/#create/Microsoft.CognitiveServicesFormRecognizer) or [multi-service](https://portal.azure.com/#create/Microsoft.CognitiveServicesAllInOne) Form Recognizer resource in the Azure portal to get your key and endpoint. You can use the free pricing tier (`F0`) to try the service, and upgrade later to a paid tier for production.
371-
372-
> [!TIP]
373-
> Create a Cognitive Services resource if you plan to access multiple cognitive services under a single endpoint/key. For Form Recognizer access only, create a Form Recognizer resource. Please note that you'lll need a single-service resource if you intend to use [Azure Active Directory authentication](../../../active-directory/authentication/overview-authentication.md).
374-
375-
* After your resource deploys, select **Go to resource**. You need the key and endpoint from the resource you create to connect your application to the Form Recognizer API. You will paste your key and endpoint into the code below later in the quickstart:
376-
377-
:::image type="content" source="../media/containers/keys-and-endpoint.png" alt-text="Screenshot: keys and endpoint location in the Azure portal.":::
378-
379-
## Set up
380-
381-
<!---
382-
### [Option 1: .NET Command-line interface (CLI)](#tab/cli)
383-
384-
In a console window (such as cmd, PowerShell, or Bash), use the `dotnet new` command to create a new console app with the name `formrecognizer-quickstart`. This command creates a simple "Hello World" C# project with a single source file: *Program.cs*.
385-
386-
```console
387-
dotnet new console -n formrecognizer-quickstart
388-
```
389-
390-
Open a command line and switch to the directory that contains your project file. Build the application with:
391-
392-
```console
393-
dotnet build
394-
```
395-
396-
The build output should contain no warnings or errors.
397-
398-
```console
399-
...
400-
Build succeeded.
401-
0 Warning(s)
402-
0 Error(s)
403-
...
404-
```
405-
406-
### Install the client library with NuGet
407-
408-
In the directory that contains your project, install the Form Recognizer client library for .NET with the following command:
409-
410-
```console
411-
dotnet add package Azure.AI.FormRecognizer
412-
```
413-
414-
This version of the client library defaults to the 2021-01-30-preview version of the service.
415-
416-
### [Option 2: Visual Studio](#tab/vs)
417-
418-
1. Start Visual Studio.
419-
420-
1. On the start page, choose Create a new project.
421-
422-
:::image type="content" source="../media/quickstarts/start-window.png" alt-text="Screenshot: Visual Studio start window.":::
423-
424-
1. On the **Create a new project page**, enter **console** in the search box. Choose the **Console Application** template, then choose **Next**.
425-
426-
:::image type="content" source="../media/quickstarts/create-new-project.png" alt-text="Screenshot: Visual Studio's create new project page.":::
427-
428-
1. In the **Configure your new project** dialog window, enter `formRecognizer_quickstart` in the Project name box. Then choose Next.
429-
430-
:::image type="content" source="../media/quickstarts/configure-new-project.png" alt-text="Screenshot: Visual Studio's configure new project dialog window.":::
431-
432-
1. In the **Additional information** dialog window, select **.NET 6.0 (Long-term support)**, and then select **Create**.
433-
434-
:::image type="content" source="../media/quickstarts/additional-information.png" alt-text="Screenshot: Visual Studio's additional information dialog window.":::
435-
436-
### Install the client library with NuGet
437-
438-
1. Right-click on your **formRecognizer_quickstart** project and select **Manage NuGet Packages...** .
439-
440-
:::image type="content" source="../media/quickstarts/select-nuget-package.png" alt-text="Screenshot: select-nuget-package.png":::
441-
442-
1. Select the Browse tab and type Azure.AI.FormRecognizer.
443-
444-
:::image type="content" source="../media/quickstarts/azure-nuget-package.png" alt-text="Screenshot: select-form-recognizer-package.png":::
445-
446-
1. Choose the **Include prerelease** checkbox and select version **4.0.0-beta.3*** from the dropdown menu and install the package in your project.
447-
448-
449-
450-
<!-- --- -->
451-
## Build your application
452-
453-
To interact with the Form Recognizer service, you'll need to create an instance of the `DocumentAnalysisClient` class. To do so, you'll create an `AzureKeyCredential` with your apiKey and a `DocumentAnalysisClient` instance with the `AzureKeyCredential` and your Form Recognizer `endpoint`.
454-
455-
> [!NOTE]
456-
>
457-
> * Starting with .NET 6, new projects using the `console` template generate different code than previous versions.
458-
> * The new output uses recent C# features that simplify the code you need to write for a program.
459-
> * When you use the newer version, you only need to write the body of the `Main` method. You don't need to include the other program elements.
460-
> * For more information, *see* [**New C# templates generate top-level statements**](/dotnet/core/tutorials/top-level-templates).
461-
462-
1. Open the **Program.cs** file.
463-
464-
1. Include the following using directives:
465-
466-
```csharp
467-
using Azure;
468-
using Azure.AI.FormRecognizer.DocumentAnalysis;
469-
```
470-
471-
1. Add the following code snippet to your Program.cs file. Set your `endpoint` and `apiKey` environment variables and create your `AzureKeyCredential` and `DocumentAnalysisClient` instance:
472-
473-
```csharp
474-
string endpoint = "<your-endpoint>";
475-
string apiKey = "<your-apiKey>";
476-
AzureKeyCredential credential = new AzureKeyCredential(apiKey);
477-
DocumentAnalysisClient client = new DocumentAnalysisClient(new Uri(endpoint), credential);
478-
```
479-
1. Add a async modifier to you Main method.
480-
481-
1. Delete the line, `Console.Writeline("Hello World!");` , and add one of the code sample scripts listed below to the file:
482-
483-
:::image type="content" source="../media/quickstarts/add-code-here.png" alt-text="Screenshot: add the sample code to the Main method.":::
484-
485-
> [!TIP]
486-
> If you would like to try more than one code sample:
487-
>
488-
> * Select one of the sample code blocks below to copy and paste into your application.
489-
> * [**Run your application**](#run-your-application).
490-
> * Comment out that sample code block but keep the set-up code and library directives.
491-
> * Select another sample code block to copy and paste into your application.
492-
> * [**Run your application**](#run-your-application).
493-
> * You can continue to comment out, copy/paste, and run the sample blocks of code.
494-
495-
### Select one of the following code samples to copy and paste into your application Program.cs file:
496-
497-
* [**General document model**](#general-document-model)
498-
499-
* [**Layout model**](#layout-model)
500-
501-
* [**Prebuilt model**](#prebuilt-model)
502-
503-
> [!IMPORTANT]
504-
>
505-
> Remember to remove the key from your code when you're done, and never post it publicly. For production, use secure methods to store and access your credentials. For more information, _see_ the Cognitive Services [security](../../../cognitive-services/cognitive-services-security.md) article.
506-
507-
## General document model
508-
509-
Extract text, tables, structure, key-value pairs, and named entities from documents.
510-
511-
> [!div class="checklist"]
512-
>
513-
> * For this example, you'll need a **form document file from a URI**. You can use our [sample form document](https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf) for this quickstart.
514-
> * To analyze a given file at a URI, you'll use the `StartAnalyzeDocumentFromUri` method. The returned value is an `AnalyzeResult` object containing data about the submitted document.
515-
> * We've added the file URI value to the `Uri fileUri` variable at the top of the script.
516-
> * For simplicity, all the entity fields that the service returns are not shown here. To see the list of all supported fields and corresponding types, see our [General document](../concept-general-document.md#named-entity-recognition-ner-categories) concept page.
517-
518-
#### Add the following code to the Program.cs file:
519-
520-
```csharp
521-
522-
// sample form document
523-
Uri fileUri = new Uri ("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf");
524-
525-
AnalyzeDocumentOperation operation = await client.StartAnalyzeDocumentFromUriAsync("prebuilt-document", fileUri);
526-
527-
await operation.WaitForCompletionAsync();
528-
529-
AnalyzeResult result = operation.Value;
530-
531-
Console.WriteLine("Detected key-value pairs:");
532-
533-
foreach (DocumentKeyValuePair kvp in result.KeyValuePairs)
534-
{
535-
if (kvp.Value == null)
536-
{
537-
Console.WriteLine($" Found key with no value: '{kvp.Key.Content}'");
538-
}
539-
else
540-
{
541-
Console.WriteLine($" Found key-value pair: '{kvp.Key.Content}' and '{kvp.Value.Content}'");
542-
}
543-
}
544-
545-
Console.WriteLine("Detected entities:");
546-
547-
foreach (DocumentEntity entity in result.Entities)
548-
{
549-
if (entity.SubCategory == null)
550-
{
551-
Console.WriteLine($" Found entity '{entity.Content}' with category '{entity.Category}'.");
552-
}
553-
else
554-
{
555-
Console.WriteLine($" Found entity '{entity.Content}' with category '{entity.Category}' and sub-category '{entity.SubCategory}'.");
556-
}
557-
}
558-
559-
foreach (DocumentPage page in result.Pages)
560-
{
561-
Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s),");
562-
Console.WriteLine($"and {page.SelectionMarks.Count} selection mark(s).");
563-
564-
for (int i = 0; i < page.Lines.Count; i++)
565-
{
566-
DocumentLine line = page.Lines[i];
567-
Console.WriteLine($" Line {i} has content: '{line.Content}'.");
568-
569-
Console.WriteLine($" Its bounding box is:");
570-
Console.WriteLine($" Upper left => X: {line.BoundingBox[0].X}, Y= {line.BoundingBox[0].Y}");
571-
Console.WriteLine($" Upper right => X: {line.BoundingBox[1].X}, Y= {line.BoundingBox[1].Y}");
572-
Console.WriteLine($" Lower right => X: {line.BoundingBox[2].X}, Y= {line.BoundingBox[2].Y}");
573-
Console.WriteLine($" Lower left => X: {line.BoundingBox[3].X}, Y= {line.BoundingBox[3].Y}");
574-
}
575-
576-
for (int i = 0; i < page.SelectionMarks.Count; i++)
577-
{
578-
DocumentSelectionMark selectionMark = page.SelectionMarks[i];
579-
580-
Console.WriteLine($" Selection Mark {i} is {selectionMark.State}.");
581-
Console.WriteLine($" Its bounding box is:");
582-
Console.WriteLine($" Upper left => X: {selectionMark.BoundingBox[0].X}, Y= {selectionMark.BoundingBox[0].Y}");
583-
Console.WriteLine($" Upper right => X: {selectionMark.BoundingBox[1].X}, Y= {selectionMark.BoundingBox[1].Y}");
584-
Console.WriteLine($" Lower right => X: {selectionMark.BoundingBox[2].X}, Y= {selectionMark.BoundingBox[2].Y}");
585-
Console.WriteLine($" Lower left => X: {selectionMark.BoundingBox[3].X}, Y= {selectionMark.BoundingBox[3].Y}");
586-
}
587-
}
588-
589-
foreach (DocumentStyle style in result.Styles)
590-
{
591-
// Check the style and style confidence to see if text is handwritten.
592-
// Note that value '0.8' is used as an example.
593-
594-
bool isHandwritten = style.IsHandwritten.HasValue && style.IsHandwritten == true;
595-
596-
if (isHandwritten && style.Confidence > 0.8)
597-
{
598-
Console.WriteLine($"Handwritten content found:");
599-
600-
foreach (DocumentSpan span in style.Spans)
601-
{
602-
Console.WriteLine($" Content: {result.Content.Substring(span.Offset, span.Length)}");
603-
}
604-
}
605-
}
606-
607-
Console.WriteLine("The following tables were extracted:");
608-
609-
for (int i = 0; i < result.Tables.Count; i++)
610-
{
611-
DocumentTable table = result.Tables[i];
612-
Console.WriteLine($" Table {i} has {table.RowCount} rows and {table.ColumnCount} columns.");
613-
614-
foreach (DocumentTableCell cell in table.Cells)
615-
{
616-
Console.WriteLine($" Cell ({cell.RowIndex}, {cell.ColumnIndex}) has kind '{cell.Kind}' and content: '{cell.Content}'.");
617-
}
618-
}
619-
620-
```
621-
622-
## Layout model
623-
624-
Extract text, selection marks, text styles, table structures, and bounding region coordinates from documents.
625-
626-
> [!div class="checklist"]
627-
>
628-
> * For this example, you'll need a **form document file from a URI**. You can use our [sample form document](https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf) for this quickstart.
629-
> * We've added the file URI value to the `Uri fileUri` variable at the top of the script.
630-
> * To extract the layout from a given file at a URI, use the `StartAnalyzeDocumentFromUri` method and pass `prebuilt-layout` as the model ID. The returned value is an `AnalyzeResult` object containing data from the submitted document.
631-
632-
#### Add the following code to the Program.cs file:
633-
634-
```csharp
635-
636-
Uri fileUri = new Uri ("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf");
637-
638-
AnalyzeDocumentOperation operation = await client.StartAnalyzeDocumentFromUriAsync("prebuilt-layout", fileUri);
639-
640-
await operation.WaitForCompletionAsync();
641-
642-
AnalyzeResult result = operation.Value;
643-
644-
foreach (DocumentPage page in result.Pages)
645-
{
646-
Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s),");
647-
Console.WriteLine($"and {page.SelectionMarks.Count} selection mark(s).");
648-
649-
for (int i = 0; i < page.Lines.Count; i++)
650-
{
651-
DocumentLine line = page.Lines[i];
652-
Console.WriteLine($" Line {i} has content: '{line.Content}'.");
653-
654-
Console.WriteLine($" Its bounding box is:");
655-
Console.WriteLine($" Upper left => X: {line.BoundingBox[0].X}, Y= {line.BoundingBox[0].Y}");
656-
Console.WriteLine($" Upper right => X: {line.BoundingBox[1].X}, Y= {line.BoundingBox[1].Y}");
657-
Console.WriteLine($" Lower right => X: {line.BoundingBox[2].X}, Y= {line.BoundingBox[2].Y}");
658-
Console.WriteLine($" Lower left => X: {line.BoundingBox[3].X}, Y= {line.BoundingBox[3].Y}");
659-
}
660-
661-
for (int i = 0; i < page.SelectionMarks.Count; i++)
662-
{
663-
DocumentSelectionMark selectionMark = page.SelectionMarks[i];
664-
665-
Console.WriteLine($" Selection Mark {i} is {selectionMark.State}.");
666-
Console.WriteLine($" Its bounding box is:");
667-
Console.WriteLine($" Upper left => X: {selectionMark.BoundingBox[0].X}, Y= {selectionMark.BoundingBox[0].Y}");
668-
Console.WriteLine($" Upper right => X: {selectionMark.BoundingBox[1].X}, Y= {selectionMark.BoundingBox[1].Y}");
669-
Console.WriteLine($" Lower right => X: {selectionMark.BoundingBox[2].X}, Y= {selectionMark.BoundingBox[2].Y}");
670-
Console.WriteLine($" Lower left => X: {selectionMark.BoundingBox[3].X}, Y= {selectionMark.BoundingBox[3].Y}");
671-
}
672-
}
673-
674347
foreach (DocumentStyle style in result.Styles)
675348
{
676349
// Check the style and style confidence to see if text is handwritten.
@@ -716,7 +389,6 @@ You are not limited to invoices—there are several prebuilt models to choose fr
716389
* [**prebuilt-receipt**](../concept-receipt.md): extracts text and key information from receipts.
717390
* [**prebuilt-idDocument**](../concept-id-document.md): extracts text and key information from driver licenses and international passports.
718391
* [**prebuilt-businessCard**](../concept-business-card.md): extracts text and key information from business cards.
719-
* [**prebuilt-w-2**](../concept-w2.md): extracts text and key information from business cards.
720392

721393
#### Try the prebuilt invoice model
722394

@@ -855,7 +527,7 @@ In this quickstart, you used the Form Recognizer C# SDK to analyze various forms
855527
## Next steps
856528

857529
> [!div class="nextstepaction"]
858-
> [REST API v3.0 reference documentation](https://westus.dev.cognitive.microsoft.com/docs/services/form-recognizer-api-v3-0-preview-2/operations/AnalyzeDocument)
530+
> [REST API v3.0 reference documentation](https://westus.dev.cognitive.microsoft.com/docs/services/form-recognizer-api-v3-0-preview-1/operations/AnalyzeDocument)
859531
860532
> [!div class="nextstepaction"]
861533
> [Form Recognizer C#/.NET reference library](/dotnet/api/overview/azure/ai.formrecognizer-readme?view=azure-dotnet&preserve-view=true)

0 commit comments

Comments
 (0)