開始使用文件智慧服務

重要

  • Azure 認知服務 表格辨識器 現在是 Azure AI 檔智慧。
  • 某些平台仍在等候重新命名更新。
  • 我們文件中的 Azure 表格辨識器或文件智慧服務全都是指相同的 Azure 服務。

此內容適用於:複選標記v4.0 (預覽)舊版:blue-checkmarkv3.1 (GA)blue-checkmarkv3.0 (GA)

  • 開始使用 Azure AI 檔智慧最新預覽版本 (2024-02-29-preview)。

此內容適用於:複選標記v3.1 (GA)舊版:blue-checkmarkv3.0blue-checkmarkv2.1

  • 開始使用 Azure 表格辨識器 最新 GA 版本 (2023-07-31)。

此內容適用於:複選標記v3.0 (GA)較新版本:blue-checkmarkv3.1 v2.1blue-checkmark

  • 開始使用 Azure 表格辨識器 舊版 GA 版本 (2022-08-31)。
  • Azure AI 檔案智慧/表格辨識器 是雲端式 Azure AI 服務,使用機器學習從您的檔案擷取索引鍵/值組、文字、數據表和密鑰數據。

  • 您可以使用程式設計語言 SDK 或呼叫 REST API,輕鬆地將檔案處理模型整合到工作流程和應用程式中。

  • 在本快速入門中,建議您在學習技術時使用免費服務。 請記住,免費頁面數目限制為每月 500 個。

若要深入瞭解 API 功能和開發選項,請瀏覽我們的 [概觀 ] 頁面。

在本快速入門中,使用下列功能來分析及擷取表單和文件中的數據和值:

  • 版面配置模型—分析及擷取檔中的單選按鈕和複選框等表格、線條、文字和選取標記,而不需要定型模型。

  • 預先建置的模型— 使用預先建置的模型,分析及擷取特定文件類型的一般欄位。

必要條件

  • Azure AI 服務或文件智慧服務資源。 擁有 Azure 訂用帳戶之後,請在 Azure 入口網站 中建立單一服務Azure AI 多服務資源,以取得您的密鑰和端點。

  • 您可以使用免費定價層 (F0) 來試用服務,之後可升級至付費層以用於實際執行環境。

提示

如果您打算在單一端點/金鑰下存取多個 Azure AI 服務,請建立 Azure AI 服務資源。 若為僅限文件智慧服務存取,請建立文件智慧服務資源。 請注意,如果您想要使用 Microsoft Entra 驗證,您將需要單一服務資源。

  • 部署資源之後,選取 [移至資源]。 您需要使用已建立資源的金鑰和端點,將應用程式連線至文件智慧服務 API。 您稍後會在快速入門中將金鑰和端點貼上至程式碼中:

    Azure 入口網站 中金鑰和端點位置的螢幕快照。

  • Azure AI 服務或 表格辨識器 資源。 擁有 Azure 訂用帳戶之後,請在 Azure 入口網站 中建立單一服務Azure AI 多服務資源,以取得您的密鑰和端點。

  • 您可以使用免費定價層 (F0) 來試用服務,之後可升級至付費層以用於實際執行環境。

提示

如果您打算在單一端點/金鑰下存取多個 Azure AI 服務,請建立 Azure AI 服務資源。 僅針對 Azure 表格辨識器的存取,請建立 Azure 表格辨識器資源。 請注意,如果您想要使用 Microsoft Entra 驗證,您將需要單一服務資源。

  • 部署資源之後,選取 [移至資源]。 您需要從您建立的資源取得密鑰和端點,才能將應用程式連線到 表格辨識器 API。 您稍後會在快速入門中將金鑰和端點貼上至程式碼中:

    Azure 入口網站 中金鑰和端點位置的螢幕快照。

設定

  1. 啟動 Visual Studio。

  2. 在開始頁面中,選擇 [建立新的專案]。

    Visual Studio 開始視窗之螢幕擷取畫面。

  3. 在 [建立新的專案] 頁面的搜尋方塊中,輸入主控台。 選擇主控台 應用程式 範本,然後選擇 [ 下一步]。

    Visual Studio 的建立新專案頁面之螢幕擷取畫面。

  1. 在 [ 設定新專案] 對話框視窗中,於 [項目 名稱] 方塊中輸入 doc_intel_quickstart 。 接著,選擇 [下一步]。
  1. 在 [ 設定新專案] 對話框視窗中,於 [項目 名稱] 方塊中輸入 form_recognizer_quickstart 。 接著,選擇 [下一步]。
  1. 在 [ 其他資訊 ] 對話框視窗中,選取 [.NET 6.0(長期支援),然後選取 [ 建立]。

    Visual Studio 的其他資訊對話視窗之螢幕擷取畫面。

使用 NuGet 安裝客戶端連結庫

  1. 以滑鼠右鍵按兩下doc_intel_quickstart項目,然後選取 [管理 NuGet 套件...]。

    在 Visual Studio 中選取 [NuGet 發行前版本套件] 視窗的螢幕快照。

  2. 選取 [流覽] 索引標籤,然後輸入 Azure.AI.DocumentIntelligence。 選擇複選框, Include prerelease 然後從下拉功能表中選取 1.0.0-beta.1 版

    在 Visual Studio 中選取 [文件智慧發行前版本 NuGet 套件] 的螢幕快照。

  1. 以滑鼠右鍵按兩下form_recognizer_quickstart項目,然後選取 [管理 NuGet 套件...]。

    在 Visual Studio 中尋找 NuGet 套件視窗的螢幕快照。

  2. 選取 [流覽] 索引標籤,然後輸入 Azure.AI.FormRecognizer。 從下拉功能表中選取 4.1.0 版

    在 Visual Studio 中選取 [NuGet 表格辨識器 套件] 的螢幕快照。

  1. 以滑鼠右鍵按兩下form_recognizer_quickstart項目,然後選取 [管理 NuGet 套件...]。

    Visual Studio 中 NuGet 套件視窗的螢幕快照。

  2. 選取 [流覽] 索引標籤,然後輸入 Azure.AI.FormRecognizer。 從下拉功能表中選取 4.0.0 版

    在 Visual Studio 中選取 [NuGet 舊版套件] 的螢幕快照。

建置您的 應用程式

若要與此文件智慧服務互動,您必須建立 DocumentIntelligenceClient 類別的執行個體。 若要這樣做,請使用 key 從 Azure 入口網站建立 AzureKeyCredential,並使用 AzureKeyCredential 和文件智慧服務 endpoint 來建立 DocumentIntelligenceClient 執行個體。

若要與 表格辨識器 服務互動,您必須建立 類別的DocumentAnalysisClient實例。 若要這樣做,您可以使用 Azure 入口網站 建立 ,AzureKeyCredential並使用 和 表格辨識器 endpoint來建立 實例AzureKeyCredentialDocumentAnalysisClientkey

注意

  • 從 .NET 6 開始,使用 console 範本的新專案會產生與舊版不同的新程序樣式。
  • 新的輸出會使用最新的 C# 功能,以簡化您需要撰寫的程式代碼。
  • 當您使用較新版本時,只需要撰寫 Main 方法的本文。 您不需要包含最上層語句、全域 using 指示詞,或隱含 using using 指示詞。
  • 如需詳細資訊,請參閱新的 C# 範本產生最上層語句。
  1. 開啟 Program.cs 檔案。

  2. 刪除預先存在的程式代碼,包括 這一行 Console.Writeline("Hello World!"),然後選取下列其中一個程式代碼範例,以複製並貼到應用程式的 Program.cs 檔案中:

重要

完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性

版面配置模型

從檔擷取文字、選取標記、文字樣式、表格結構和周框區域座標。

  • 在此範例中,您需要 來自 URI 的檔案檔案。 您可以使用本快速入門的 範例檔
  • 我們已將檔案 URI 值新增至 Uri fileUri 腳本頂端的變數。
  • 若要從 URI 上的指定檔案擷取配置,請使用 StartAnalyzeDocumentFromUri 方法並傳遞 prebuilt-layout 作為模型標識碼。 傳回的值是物件 AnalyzeResult ,其中包含送出之文件的數據。

將下列程式代碼範例新增至 Program.cs 檔案。 請確定您已使用 Azure 入口網站 Document Intelligence 實例中的值來更新金鑰和端點變數:


using Azure;
using Azure.AI.DocumentIntelligence;

//set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal to create your `AzureKeyCredential` and `DocumentIntelligenceClient` instance
string endpoint = "<your-endpoint>";
string key = "<your-key>";
AzureKeyCredential credential = new AzureKeyCredential(key);
DocumentIntelligenceClient client = new DocumentIntelligenceClient(new Uri(endpoint), credential);

//sample document
Uri fileUri = new Uri ("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf");

Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-layout", fileUri);

AnalyzeResult result = operation.Value;

foreach (DocumentPage page in result.Pages)
{
    Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s)," +
        $" and {page.SelectionMarks.Count} selection mark(s).");

    for (int i = 0; i < page.Lines.Count; i++)
    {
        DocumentLine line = page.Lines[i];

        Console.WriteLine($"  Line {i}:");
        Console.WriteLine($"    Content: '{line.Content}'");

        Console.Write("    Bounding polygon, with points ordered clockwise:");
        for (int j = 0; j < line.Polygon.Count; j += 2)
        {
            Console.Write($" ({line.Polygon[j]}, {line.Polygon[j + 1]})");
        }

        Console.WriteLine();
    }

    for (int i = 0; i < page.SelectionMarks.Count; i++)
    {
        DocumentSelectionMark selectionMark = page.SelectionMarks[i];

        Console.WriteLine($"  Selection Mark {i} is {selectionMark.State}.");
        Console.WriteLine($"    State: {selectionMark.State}");

        Console.Write("    Bounding polygon, with points ordered clockwise:");
        for (int j = 0; j < selectionMark.Polygon.Count; j++)
        {
            Console.Write($" ({selectionMark.Polygon[j]}, {selectionMark.Polygon[j + 1]})");
        }

        Console.WriteLine();
    }
}

for (int i = 0; i < result.Paragraphs.Count; i++)
{
    DocumentParagraph paragraph = result.Paragraphs[i];

    Console.WriteLine($"Paragraph {i}:");
    Console.WriteLine($"  Content: {paragraph.Content}");

    if (paragraph.Role != null)
    {
        Console.WriteLine($"  Role: {paragraph.Role}");
    }
}

foreach (DocumentStyle style in result.Styles)
{
    // Check the style and style confidence to see if text is handwritten.
    // Note that value '0.8' is used as an example.

    bool isHandwritten = style.IsHandwritten.HasValue && style.IsHandwritten == true;

    if (isHandwritten && style.Confidence > 0.8)
    {
        Console.WriteLine($"Handwritten content found:");

        foreach (DocumentSpan span in style.Spans)
        {
            var handwrittenContent = result.Content.Substring(span.Offset, span.Length);
            Console.WriteLine($"  {handwrittenContent}");
        }
    }
}

for (int i = 0; i < result.Tables.Count; i++)
{
    DocumentTable table = result.Tables[i];

    Console.WriteLine($"Table {i} has {table.RowCount} rows and {table.ColumnCount} columns.");

    foreach (DocumentTableCell cell in table.Cells)
    {
        Console.WriteLine($"  Cell ({cell.RowIndex}, {cell.ColumnIndex}) is a '{cell.Kind}' with content: {cell.Content}");
    }
}

執行您的應用程式

將程式代碼範例新增至應用程式之後,請選擇formRecognizer_quickstart旁的綠色 [開始 ] 按鈕,以建置和執行程式,或按 F5

執行 Visual Studio 程式按鈕的螢幕快照。

將下列程式代碼範例新增至 Program.cs 檔案。 請務必使用來自 Azure 入口網站 表格辨識器 實體的值來更新金鑰和端點變數:

using Azure;
using Azure.AI.FormRecognizer.DocumentAnalysis;

//set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal to create your `AzureKeyCredential` and `DocumentAnalysisClient` instance
string endpoint = "<your-endpoint>";
string key = "<your-key>";
AzureKeyCredential credential = new AzureKeyCredential(key);
DocumentAnalysisClient client = new DocumentAnalysisClient(new Uri(endpoint), credential);

//sample document
Uri fileUri = new Uri ("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf");

AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-layout", fileUri);

AnalyzeResult result = operation.Value;

foreach (DocumentPage page in result.Pages)
{
    Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s),");
    Console.WriteLine($"and {page.SelectionMarks.Count} selection mark(s).");

    for (int i = 0; i < page.Lines.Count; i++)
    {
        DocumentLine line = page.Lines[i];
        Console.WriteLine($"  Line {i} has content: '{line.Content}'.");

        Console.WriteLine($"    Its bounding box is:");
        Console.WriteLine($"      Upper left => X: {line.BoundingPolygon[0].X}, Y= {line.BoundingPolygon[0].Y}");
        Console.WriteLine($"      Upper right => X: {line.BoundingPolygon[1].X}, Y= {line.BoundingPolygon[1].Y}");
        Console.WriteLine($"      Lower right => X: {line.BoundingPolygon[2].X}, Y= {line.BoundingPolygon[2].Y}");
        Console.WriteLine($"      Lower left => X: {line.BoundingPolygon[3].X}, Y= {line.BoundingPolygon[3].Y}");
    }

    for (int i = 0; i < page.SelectionMarks.Count; i++)
    {
        DocumentSelectionMark selectionMark = page.SelectionMarks[i];

        Console.WriteLine($"  Selection Mark {i} is {selectionMark.State}.");
        Console.WriteLine($"    Its bounding box is:");
        Console.WriteLine($"      Upper left => X: {selectionMark.BoundingPolygon[0].X}, Y= {selectionMark.BoundingPolygon[0].Y}");
        Console.WriteLine($"      Upper right => X: {selectionMark.BoundingPolygon[1].X}, Y= {selectionMark.BoundingPolygon[1].Y}");
        Console.WriteLine($"      Lower right => X: {selectionMark.BoundingPolygon[2].X}, Y= {selectionMark.BoundingPolygon[2].Y}");
        Console.WriteLine($"      Lower left => X: {selectionMark.BoundingPolygon[3].X}, Y= {selectionMark.BoundingPolygon[3].Y}");
    }
}

foreach (DocumentStyle style in result.Styles)
{
    // Check the style and style confidence to see if text is handwritten.
    // Note that value '0.8' is used as an example.

    bool isHandwritten = style.IsHandwritten.HasValue && style.IsHandwritten == true;

    if (isHandwritten && style.Confidence > 0.8)
    {
        Console.WriteLine($"Handwritten content found:");

        foreach (DocumentSpan span in style.Spans)
        {
            Console.WriteLine($"  Content: {result.Content.Substring(span.Index, span.Length)}");
        }
    }
}

Console.WriteLine("The following tables were extracted:");

for (int i = 0; i < result.Tables.Count; i++)
{
    DocumentTable table = result.Tables[i];
    Console.WriteLine($"  Table {i} has {table.RowCount} rows and {table.ColumnCount} columns.");

    foreach (DocumentTableCell cell in table.Cells)
    {
        Console.WriteLine($"    Cell ({cell.RowIndex}, {cell.ColumnIndex}) has kind '{cell.Kind}' and content: '{cell.Content}'.");
    }
}

執行您的應用程式

將程式代碼範例新增至應用程式之後,請選擇formRecognizer_quickstart旁的綠色 [開始 ] 按鈕,以建置和執行程式,或按 F5

執行 Visual Studio 程式按鈕位置的螢幕快照。

版面配置模型輸出

以下是預期的輸出代碼段:

  Document Page 1 has 69 line(s), 425 word(s), and 15 selection mark(s).
  Line 0 has content: 'UNITED STATES'.
    Its bounding box is:
      Upper left => X: 3.4915, Y= 0.6828
      Upper right => X: 5.0116, Y= 0.6828
      Lower right => X: 5.0116, Y= 0.8265
      Lower left => X: 3.4915, Y= 0.8265
  Line 1 has content: 'SECURITIES AND EXCHANGE COMMISSION'.
    Its bounding box is:
      Upper left => X: 2.1937, Y= 0.9061
      Upper right => X: 6.297, Y= 0.9061
      Lower right => X: 6.297, Y= 1.0498
      Lower left => X: 2.1937, Y= 1.0498

若要檢視整個輸出,請造訪 GitHub 上的 Azure 範例存放庫,以檢視 版面配置模型輸出

將下列程式代碼範例新增至 Program.cs 檔案。 請務必使用來自 Azure 入口網站 表格辨識器 實體的值來更新金鑰和端點變數:

using Azure;
using Azure.AI.FormRecognizer.DocumentAnalysis;

//set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal to create your `AzureKeyCredential` and `DocumentAnalysisClient` instance
string endpoint = "<your-endpoint>";
string key = "<your-key>";
AzureKeyCredential credential = new AzureKeyCredential(key);
DocumentAnalysisClient client = new DocumentAnalysisClient(new Uri(endpoint), credential);

//sample document
Uri fileUri = new Uri ("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf");

AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-layout", fileUri);

AnalyzeResult result = operation.Value;

foreach (DocumentPage page in result.Pages)
{
    Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s),");
    Console.WriteLine($"and {page.SelectionMarks.Count} selection mark(s).");

    for (int i = 0; i < page.Lines.Count; i++)
    {
        DocumentLine line = page.Lines[i];
        Console.WriteLine($"  Line {i} has content: '{line.Content}'.");

        Console.WriteLine($"    Its bounding polygon (points ordered clockwise):");

        for (int j = 0; j < line.BoundingPolygon.Count; j++)
        {
            Console.WriteLine($"      Point {j} => X: {line.BoundingPolygon[j].X}, Y: {line.BoundingPolygon[j].Y}");
        }
    }

    for (int i = 0; i < page.SelectionMarks.Count; i++)
    {
        DocumentSelectionMark selectionMark = page.SelectionMarks[i];

        Console.WriteLine($"  Selection Mark {i} is {selectionMark.State}.");
        Console.WriteLine($"    Its bounding polygon (points ordered clockwise):");

        for (int j = 0; j < selectionMark.BoundingPolygon.Count; j++)
        {
            Console.WriteLine($"      Point {j} => X: {selectionMark.BoundingPolygon[j].X}, Y: {selectionMark.BoundingPolygon[j].Y}");
        }
    }
}

Console.WriteLine("Paragraphs:");

foreach (DocumentParagraph paragraph in result.Paragraphs)
{
    Console.WriteLine($"  Paragraph content: {paragraph.Content}");

    if (paragraph.Role != null)
    {
        Console.WriteLine($"    Role: {paragraph.Role}");
    }
}

foreach (DocumentStyle style in result.Styles)
{
    // Check the style and style confidence to see if text is handwritten.
    // Note that value '0.8' is used as an example.

    bool isHandwritten = style.IsHandwritten.HasValue && style.IsHandwritten == true;

    if (isHandwritten && style.Confidence > 0.8)
    {
        Console.WriteLine($"Handwritten content found:");

        foreach (DocumentSpan span in style.Spans)
        {
            Console.WriteLine($"  Content: {result.Content.Substring(span.Index, span.Length)}");
        }
    }
}

Console.WriteLine("The following tables were extracted:");

for (int i = 0; i < result.Tables.Count; i++)
{
    DocumentTable table = result.Tables[i];
    Console.WriteLine($"  Table {i} has {table.RowCount} rows and {table.ColumnCount} columns.");

    foreach (DocumentTableCell cell in table.Cells)
    {
        Console.WriteLine($"    Cell ({cell.RowIndex}, {cell.ColumnIndex}) has kind '{cell.Kind}' and content: '{cell.Content}'.");
    }
}
Extract the layout of a document from a file stream
To extract the layout from a given file at a file stream, use the AnalyzeDocument method and pass prebuilt-layout as the model ID. The returned value is an AnalyzeResult object containing data about the submitted document.

string filePath = "<filePath>";
using var stream = new FileStream(filePath, FileMode.Open);

AnalyzeDocumentOperation operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-layout", stream);
AnalyzeResult result = operation.Value;

foreach (DocumentPage page in result.Pages)
{
    Console.WriteLine($"Document Page {page.PageNumber} has {page.Lines.Count} line(s), {page.Words.Count} word(s),");
    Console.WriteLine($"and {page.SelectionMarks.Count} selection mark(s).");

    for (int i = 0; i < page.Lines.Count; i++)
    {
        DocumentLine line = page.Lines[i];
        Console.WriteLine($"  Line {i} has content: '{line.Content}'.");

        Console.WriteLine($"    Its bounding polygon (points ordered clockwise):");

        for (int j = 0; j < line.BoundingPolygon.Count; j++)
        {
            Console.WriteLine($"      Point {j} => X: {line.BoundingPolygon[j].X}, Y: {line.BoundingPolygon[j].Y}");
        }
    }

    for (int i = 0; i < page.SelectionMarks.Count; i++)
    {
        DocumentSelectionMark selectionMark = page.SelectionMarks[i];

        Console.WriteLine($"  Selection Mark {i} is {selectionMark.State}.");
        Console.WriteLine($"    Its bounding polygon (points ordered clockwise):");

        for (int j = 0; j < selectionMark.BoundingPolygon.Count; j++)
        {
            Console.WriteLine($"      Point {j} => X: {selectionMark.BoundingPolygon[j].X}, Y: {selectionMark.BoundingPolygon[j].Y}");
        }
    }
}

Console.WriteLine("Paragraphs:");

foreach (DocumentParagraph paragraph in result.Paragraphs)
{
    Console.WriteLine($"  Paragraph content: {paragraph.Content}");

    if (paragraph.Role != null)
    {
        Console.WriteLine($"    Role: {paragraph.Role}");
    }
}

foreach (DocumentStyle style in result.Styles)
{
    // Check the style and style confidence to see if text is handwritten.
    // Note that value '0.8' is used as an example.

    bool isHandwritten = style.IsHandwritten.HasValue && style.IsHandwritten == true;

    if (isHandwritten && style.Confidence > 0.8)
    {
        Console.WriteLine($"Handwritten content found:");

        foreach (DocumentSpan span in style.Spans)
        {
            Console.WriteLine($"  Content: {result.Content.Substring(span.Index, span.Length)}");
        }
    }
}

Console.WriteLine("The following tables were extracted:");

for (int i = 0; i < result.Tables.Count; i++)
{
    DocumentTable table = result.Tables[i];
    Console.WriteLine($"  Table {i} has {table.RowCount} rows and {table.ColumnCount} columns.");

    foreach (DocumentTableCell cell in table.Cells)
    {
        Console.WriteLine($"    Cell ({cell.RowIndex}, {cell.ColumnIndex}) has kind '{cell.Kind}' and content: '{cell.Content}'.");
    }
}

執行您的應用程式

將程式代碼範例新增至應用程式之後,請選擇formRecognizer_quickstart旁的綠色 [開始 ] 按鈕,以建置和執行程式,或按 F5

執行您的 Visual Studio 程式之螢幕擷取畫面。

預先建置的模型

使用預先建置的模型,從特定文件類型分析及擷取一般欄位。 在此範例中,我們會使用 預先建置的發票模型來分析發票

提示

您不限於發票,有數個預先建置的模型可供選擇,每個模型都有自己的支援欄位集。 要用於作業的 analyze 模型取決於要分析的文件類型。 請參閱模型數據擷取

  • 使用預建發票模型分析發票。 您可以針對本快速入門使用我們的 範例發票檔
  • 我們已將檔案 URI 值新增至 Uri invoiceUri Program.cs檔案頂端的 變數。
  • 若要在 URI 分析指定的檔案,請使用 StartAnalyzeDocumentFromUri 方法並傳遞 prebuilt-invoice 作為模型標識碼。 傳回的值是物件 AnalyzeResult ,其中包含送出之文件的數據。
  • 為了簡單起見,服務傳回的所有索引鍵/值組不會在此顯示。 若要查看所有支援的欄位和對應類型清單,請參閱我們的 發票 概念頁面。

將下列程式代碼範例新增至您的Program.cs檔案。 請確定您已使用 Azure 入口網站 Document Intelligence 實例中的值來更新金鑰和端點變數:


using Azure;
using Azure.AI.DocumentIntelligence;

//set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal to create your `AzureKeyCredential` and `DocumentIntelligenceClient` instance
string endpoint = "<your-endpoint>";
string key = "<your-key>";
AzureKeyCredential credential = new AzureKeyCredential(key);
DocumentIntelligenceClient client = new DocumentIntelligenceClient(new Uri(endpoint), credential);

//sample invoice document

Uri invoiceUri = new Uri ("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf");

Operation<AnalyzeResult> operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-invoice", invoiceUri);

AnalyzeResult result = operation.Value;

for (int i = 0; i < result.Documents.Count; i++)
{
    Console.WriteLine($"Document {i}:");

    AnalyzedDocument document = result.Documents[i];

    if (document.Fields.TryGetValue("VendorName", out DocumentField vendorNameField)
        && vendorNameField.Type == DocumentFieldType.String)
    {
        string vendorName = vendorNameField.ValueString;
        Console.WriteLine($"Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}");
    }

    if (document.Fields.TryGetValue("CustomerName", out DocumentField customerNameField)
        && customerNameField.Type == DocumentFieldType.String)
    {
        string customerName = customerNameField.ValueString;
        Console.WriteLine($"Customer Name: '{customerName}', with confidence {customerNameField.Confidence}");
    }

    if (document.Fields.TryGetValue("Items", out DocumentField itemsField)
        && itemsField.Type == DocumentFieldType.Array)
    {
        foreach (DocumentField itemField in itemsField.ValueArray)
        {
            Console.WriteLine("Item:");

            if (itemField.Type == DocumentFieldType.Object)
            {
                IReadOnlyDictionary<string, DocumentField> itemFields = itemField.ValueObject;

                if (itemFields.TryGetValue("Description", out DocumentField itemDescriptionField)
                    && itemDescriptionField.Type == DocumentFieldType.String)
                {
                    string itemDescription = itemDescriptionField.ValueString;
                    Console.WriteLine($"  Description: '{itemDescription}', with confidence {itemDescriptionField.Confidence}");
                }

                if (itemFields.TryGetValue("Amount", out DocumentField itemAmountField)
                    && itemAmountField.Type == DocumentFieldType.Currency)
                {
                    CurrencyValue itemAmount = itemAmountField.ValueCurrency;
                    Console.WriteLine($"  Amount: '{itemAmount.CurrencySymbol}{itemAmount.Amount}', with confidence {itemAmountField.Confidence}");
                }
            }
        }
    }

    if (document.Fields.TryGetValue("SubTotal", out DocumentField subTotalField)
        && subTotalField.Type == DocumentFieldType.Currency)
    {
        CurrencyValue subTotal = subTotalField.ValueCurrency;
        Console.WriteLine($"Sub Total: '{subTotal.CurrencySymbol}{subTotal.Amount}', with confidence {subTotalField.Confidence}");
    }

    if (document.Fields.TryGetValue("TotalTax", out DocumentField totalTaxField)
        && totalTaxField.Type == DocumentFieldType.Currency)
    {
        CurrencyValue totalTax = totalTaxField.ValueCurrency;
        Console.WriteLine($"Total Tax: '{totalTax.CurrencySymbol}{totalTax.Amount}', with confidence {totalTaxField.Confidence}");
    }

    if (document.Fields.TryGetValue("InvoiceTotal", out DocumentField invoiceTotalField)
        && invoiceTotalField.Type == DocumentFieldType.Currency)
    {
        CurrencyValue invoiceTotal = invoiceTotalField.ValueCurrency;
        Console.WriteLine($"Invoice Total: '{invoiceTotal.CurrencySymbol}{invoiceTotal.Amount}', with confidence {invoiceTotalField.Confidence}");
    }
}

執行您的應用程式

將程式代碼範例新增至應用程式之後,請選擇formRecognizer_quickstart旁的綠色 [開始 ] 按鈕,以建置和執行程式,或按 F5

執行 Visual Studio 程式按鈕的螢幕快照。

將下列程式代碼範例新增至您的Program.cs檔案。 請務必使用來自 Azure 入口網站 表格辨識器 實體的值來更新金鑰和端點變數:


using Azure;
using Azure.AI.FormRecognizer.DocumentAnalysis;

//set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal to create your `AzureKeyCredential` and `FormRecognizerClient` instance
string endpoint = "<your-endpoint>";
string key = "<your-key>";
AzureKeyCredential credential = new AzureKeyCredential(key);
DocumentAnalysisClient client = new DocumentAnalysisClient(new Uri(endpoint), credential);

//sample invoice document

Uri invoiceUri = new Uri ("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf");

Operation operation = await client.AnalyzeDocumentAsync(WaitUntil.Completed, "prebuilt-invoice", invoiceUri);

AnalyzeResult result = operation.Value;

for (int i = 0; i < result.Documents.Count; i++)
{
    Console.WriteLine($"Document {i}:");

    AnalyzedDocument document = result.Documents[i];

    if (document.Fields.TryGetValue("VendorName", out DocumentField vendorNameField))
    {
        if (vendorNameField.FieldType == DocumentFieldType.String)
        {
            string vendorName = vendorNameField.Value.AsString();
            Console.WriteLine($"Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}");
        }
    }

    if (document.Fields.TryGetValue("CustomerName", out DocumentField customerNameField))
    {
        if (customerNameField.FieldType == DocumentFieldType.String)
        {
            string customerName = customerNameField.Value.AsString();
            Console.WriteLine($"Customer Name: '{customerName}', with confidence {customerNameField.Confidence}");
        }
    }

    if (document.Fields.TryGetValue("Items", out DocumentField itemsField))
    {
        if (itemsField.FieldType == DocumentFieldType.List)
        {
            foreach (DocumentField itemField in itemsField.Value.AsList())
            {
                Console.WriteLine("Item:");

                if (itemField.FieldType == DocumentFieldType.Dictionary)
                {
                    IReadOnlyDictionary<string, DocumentField> itemFields = itemField.Value.AsDictionary();

                    if (itemFields.TryGetValue("Description", out DocumentField itemDescriptionField))
                    {
                        if (itemDescriptionField.FieldType == DocumentFieldType.String)
                        {
                            string itemDescription = itemDescriptionField.Value.AsString();

                            Console.WriteLine($"  Description: '{itemDescription}', with confidence {itemDescriptionField.Confidence}");
                        }
                    }

                    if (itemFields.TryGetValue("Amount", out DocumentField itemAmountField))
                    {
                        if (itemAmountField.FieldType == DocumentFieldType.Currency)
                        {
                            CurrencyValue itemAmount = itemAmountField.Value.AsCurrency();

                            Console.WriteLine($"  Amount: '{itemAmount.Symbol}{itemAmount.Amount}', with confidence {itemAmountField.Confidence}");
                        }
                    }
                }
            }
        }
    }

    if (document.Fields.TryGetValue("SubTotal", out DocumentField subTotalField))
    {
        if (subTotalField.FieldType == DocumentFieldType.Currency)
        {
            CurrencyValue subTotal = subTotalField.Value.AsCurrency();
            Console.WriteLine($"Sub Total: '{subTotal.Symbol}{subTotal.Amount}', with confidence {subTotalField.Confidence}");
        }
    }

    if (document.Fields.TryGetValue("TotalTax", out DocumentField totalTaxField))
    {
        if (totalTaxField.FieldType == DocumentFieldType.Currency)
        {
            CurrencyValue totalTax = totalTaxField.Value.AsCurrency();
            Console.WriteLine($"Total Tax: '{totalTax.Symbol}{totalTax.Amount}', with confidence {totalTaxField.Confidence}");
        }
    }

    if (document.Fields.TryGetValue("InvoiceTotal", out DocumentField invoiceTotalField))
    {
        if (invoiceTotalField.FieldType == DocumentFieldType.Currency)
        {
            CurrencyValue invoiceTotal = invoiceTotalField.Value.AsCurrency();
            Console.WriteLine($"Invoice Total: '{invoiceTotal.Symbol}{invoiceTotal.Amount}', with confidence {invoiceTotalField.Confidence}");
        }
    }
}

執行您的應用程式

將程式代碼範例新增至應用程式之後,請選擇formRecognizer_quickstart旁的綠色 [開始 ] 按鈕,以建置和執行程式,或按 F5

執行 Visual Studio 程式按鈕位置的螢幕快照。

預先建置的模型輸出

以下是預期的輸出代碼段:

  Document 0:
  Vendor Name: 'CONTOSO LTD.', with confidence 0.962
  Customer Name: 'MICROSOFT CORPORATION', with confidence 0.951
  Item:
    Description: 'Test for 23 fields', with confidence 0.899
    Amount: '100', with confidence 0.902
  Sub Total: '100', with confidence 0.979

若要檢視整個輸出,請造訪 GitHub 上的 Azure 範例存放庫,以檢視 預先建置的發票模型輸出

將下列程式代碼範例新增至您的Program.cs檔案。 請務必使用來自 Azure 入口網站 表格辨識器 實體的值來更新金鑰和端點變數:


using Azure;
using Azure.AI.FormRecognizer.DocumentAnalysis;

//set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal to create your `AzureKeyCredential` and `FormRecognizerClient` instance
string endpoint = "<your-endpoint>";
string key = "<your-key>";
AzureKeyCredential credential = new AzureKeyCredential(key);
DocumentAnalysisClient client = new DocumentAnalysisClient(new Uri(endpoint), credential);

//sample invoice document

Uri invoiceUri = new Uri ("https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf");

AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-invoice", invoiceUri);

AnalyzeResult result = operation.Value;

for (int i = 0; i < result.Documents.Count; i++)
{
    Console.WriteLine($"Document {i}:");

    AnalyzedDocument document = result.Documents[i];

    if (document.Fields.TryGetValue("VendorName", out DocumentField vendorNameField))
    {
        if (vendorNameField.FieldType == DocumentFieldType.String)
        {
            string vendorName = vendorNameField.Value.AsString();
            Console.WriteLine($"Vendor Name: '{vendorName}', with confidence {vendorNameField.Confidence}");
        }
    }

    if (document.Fields.TryGetValue("CustomerName", out DocumentField customerNameField))
    {
        if (customerNameField.FieldType == DocumentFieldType.String)
        {
            string customerName = customerNameField.Value.AsString();
            Console.WriteLine($"Customer Name: '{customerName}', with confidence {customerNameField.Confidence}");
        }
    }

    if (document.Fields.TryGetValue("Items", out DocumentField itemsField))
    {
        if (itemsField.FieldType == DocumentFieldType.List)
        {
            foreach (DocumentField itemField in itemsField.Value.AsList())
            {
                Console.WriteLine("Item:");

                if (itemField.FieldType == DocumentFieldType.Dictionary)
                {
                    IReadOnlyDictionary<string, DocumentField> itemFields = itemField.Value.AsDictionary();

                    if (itemFields.TryGetValue("Description", out DocumentField itemDescriptionField))
                    {
                        if (itemDescriptionField.FieldType == DocumentFieldType.String)
                        {
                            string itemDescription = itemDescriptionField.Value.AsString();

                            Console.WriteLine($"  Description: '{itemDescription}', with confidence {itemDescriptionField.Confidence}");
                        }
                    }

                    if (itemFields.TryGetValue("Amount", out DocumentField itemAmountField))
                    {
                        if (itemAmountField.FieldType == DocumentFieldType.Currency)
                        {
                            CurrencyValue itemAmount = itemAmountField.Value.AsCurrency();

                            Console.WriteLine($"  Amount: '{itemAmount.Symbol}{itemAmount.Amount}', with confidence {itemAmountField.Confidence}");
                        }
                    }
                }
            }
        }
    }

    if (document.Fields.TryGetValue("SubTotal", out DocumentField subTotalField))
    {
        if (subTotalField.FieldType == DocumentFieldType.Currency)
        {
            CurrencyValue subTotal = subTotalField.Value.AsCurrency();
            Console.WriteLine($"Sub Total: '{subTotal.Symbol}{subTotal.Amount}', with confidence {subTotalField.Confidence}");
        }
    }

    if (document.Fields.TryGetValue("TotalTax", out DocumentField totalTaxField))
    {
        if (totalTaxField.FieldType == DocumentFieldType.Currency)
        {
            CurrencyValue totalTax = totalTaxField.Value.AsCurrency();
            Console.WriteLine($"Total Tax: '{totalTax.Symbol}{totalTax.Amount}', with confidence {totalTaxField.Confidence}");
        }
    }

    if (document.Fields.TryGetValue("InvoiceTotal", out DocumentField invoiceTotalField))
    {
        if (invoiceTotalField.FieldType == DocumentFieldType.Currency)
        {
            CurrencyValue invoiceTotal = invoiceTotalField.Value.AsCurrency();
            Console.WriteLine($"Invoice Total: '{invoiceTotal.Symbol}{invoiceTotal.Amount}', with confidence {invoiceTotalField.Confidence}");
        }
    }
}

執行您的應用程式

將程式代碼範例新增至應用程式之後,請選擇formRecognizer_quickstart旁的綠色 [開始 ] 按鈕,以建置和執行程式,或按 F5

執行您的 Visual Studio 程式之螢幕擷取畫面。

在本快速入門中,使用下列功能來分析及擷取表單和文件中的數據和值:

  • 版面配置:分析及擷取檔中的單選按鈕和複選框等表格、線條、單字和選取標記,而不需要定型模型。

  • 預先建置的發票 — 使用預先定型的模型,分析及擷取特定文件類型的一般欄位。

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶

  • 最新版的 Visual Studio Code 或您慣用的 IDE。 請參閱Visual Studio Code 中的 Java。

    提示

    • Visual Studio Code 提供 適用於 Windows 和 macOS 的 Java 編碼套件。程式代碼套件是 VS Code 套件組合、Java 開發工具包 (JDK),以及 Microsoft 建議的延伸模組集合。 程式代碼撰寫套件也可用來修正現有的開發環境。
    • 如果您使用 VS Code 和適用於 Java 的 編碼套件,請安裝適用於 Java 的 Gradle 擴充功能。
  • 如果您未使用 Visual Studio Code,請確保在開發環境中安裝下列項目:

  • Azure AI 服務或文件智慧服務資源。 擁有 Azure 訂閱後,請在 Azure 入口網站中建立單一服務多重服務文件智慧服務資源,以取得您的金鑰和端點。 您可以使用免費定價層 (F0) 來試用服務,之後可升級至付費層以用於實際執行環境。

    提示

    如果您打算在單一端點/金鑰下存取多個 Azure AI 服務,請建立 Azure AI 服務資源。 若為僅限文件智慧服務存取,請建立文件智慧服務資源。 請注意,如果您想要使用 Microsoft Entra 驗證,您將需要單一服務資源。

  • 部署資源之後,選取 [移至資源]。 您需要使用已建立資源的金鑰和端點,將應用程式連線至文件智慧服務 API。 稍後,您會將金鑰和端點貼到程式代碼中:

    Azure 入口網站 中金鑰和端點位置的螢幕快照。

設定

建立新的 Gradle 專案

  1. 在主控台視窗中(例如 cmd、PowerShell 或 Bash),為您的應用程式建立名為 doc-intel-app 的新目錄,然後流覽至該目錄。

    mkdir doc-intel-app && doc-intel-app
    
    mkdir doc-intel-app; cd doc-intel-app
    
  2. gradle init從工作目錄執行 命令。 此命令會建立 Gradle 的基本組建檔案,包括 build.gradle.kts,將在執行階段使用 build.gradle.kts,來建立及設定應用程式。

    gradle init --type basic
    
  3. 當系統提示您選擇 DSL 時,請選取 [Kotlin]。

  4. 選取 [傳回] 或 [Enter],以接受預設項目名稱 (doc-intel-app)。

  1. 在控制台視窗中(例如 cmd、PowerShell 或 Bash),為您的應用程式建立名為 form-recognize-app 的新目錄,然後流覽至該目錄。

    mkdir form-recognize-app && form-recognize-app
    
    mkdir form-recognize-app; cd form-recognize-app
    
  2. gradle init從工作目錄執行 命令。 此命令會建立 Gradle 的基本組建檔案,包括 build.gradle.kts,將在執行階段使用 build.gradle.kts,來建立及設定應用程式。

    gradle init --type basic
    
  3. 當系統提示您選擇 DSL 時,請選取 [Kotlin]。

  4. 選取 [傳回] 或 [Enter],以接受預設項目名稱 (form-recognize-app)。

安裝用戶端程式庫

本快速入門使用 Gradle 相依性管理員。 您可以在 Maven 中央存放庫找到其他相依性管理員的用戶端連結庫和資訊。

在 IDE 中開啟專案的 build.gradle.kts 檔案。 Copay 並貼上下列程式代碼以包含用戶端連結庫做為 implementation 語句,以及必要的外掛程式和設定。

   plugins {
       java
       application
   }
   application {
       mainClass.set("DocIntelligence")
   }
   repositories {
       mavenCentral()
   }
   dependencies {
       implementation group: 'com.azure', name: 'azure-ai-documentintelligence', version: '1.0.0-beta.2'

   }

本快速入門使用 Gradle 相依性管理員。 您可以在 Maven 中央存放庫找到其他相依性管理員的用戶端連結庫和資訊。

在 IDE 中開啟專案的 build.gradle.kts 檔案。 Copay 並貼上下列程式代碼以包含用戶端連結庫做為 implementation 語句,以及必要的外掛程式和設定。

   plugins {
       java
       application
   }
   application {
       mainClass.set("FormRecognizer")
   }
   repositories {
       mavenCentral()
   }
   dependencies {
       implementation group: 'com.azure', name: 'azure-ai-formrecognizer', version: '4.1.0'

   }

本快速入門使用 Gradle 相依性管理員。 您可以在 Maven 中央存放庫找到其他相依性管理員的用戶端連結庫和資訊。

在 IDE 中開啟專案的 build.gradle.kts 檔案。 Copay 並貼上下列程式代碼以包含用戶端連結庫做為 implementation 語句,以及必要的外掛程式和設定。

   plugins {
       java
       application
   }
   application {
       mainClass.set("FormRecognizer")
   }
   repositories {
       mavenCentral()
   }
   dependencies {
       implementation group: 'com.azure', name: 'azure-ai-formrecognizer', version: '4.0.0'


   }

建立 Java 應用程式

若要與此文件智慧服務互動,您必須建立 DocumentIntelligenceClient 類別的執行個體。 若要這樣做,請使用 key 從 Azure 入口網站建立 AzureKeyCredential,並使用 AzureKeyCredential 和文件智慧服務 endpoint 來建立 DocumentIntelligenceClient 執行個體。

若要與此文件智慧服務互動,您必須建立 DocumentAnalysisClient 類別的執行個體。 若要這樣做,請使用 key 從 Azure 入口網站建立 AzureKeyCredential,並使用 AzureKeyCredential 和文件智慧服務 endpoint 來建立 DocumentAnalysisClient 執行個體。

  1. 從 doc-intel-app 目錄執行下列命令:

    mkdir -p src/main/java
    

    您會建立下列目錄結構:

    Java 目錄結構之螢幕擷取畫面

  1. 流覽至目錄, java 並建立名為 DocIntelligence.java的檔案。

    提示

    • 您可以使用 PowerShell 建立新的檔案。
    • 按住 Shift 鍵,並在資料夾上按下滑鼠右鍵,在您的專案目錄中開啟 PowerShell 視窗。
    • 輸入下列命令 New-Item DocIntelligence.java
  2. 開啟 DocIntelligence.java 檔案。 將下列其中一個程式代碼範例複製並貼到您的應用程式中:

  1. 流覽至目錄, java 並建立名為 FormRecognizer.java的檔案。

    提示

    • 您可以使用 PowerShell 建立新的檔案。
    • 按住 Shift 鍵,並在資料夾上按下滑鼠右鍵,在您的專案目錄中開啟 PowerShell 視窗。
    • 輸入下列命令 New-Item FormRecognizer.java
  2. 開啟 FormRecognizer.java 檔案。 將下列其中一個程式代碼範例複製並貼到您的應用程式中:

重要

完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性

版面配置模型

從檔擷取文字、選取標記、文字樣式、表格結構和周框區域座標。

  • 在此範例中,您需要 URI 的檔案檔案。 您可以使用本快速入門的 範例檔
  • 若要在 URI 分析指定的檔案,您將使用 beginAnalyzeDocumentFromUrl 方法,並傳遞 prebuilt-layout 作為模型識別碼。傳回的值是包含 AnalyzeResult 所提交文件相關數據的物件。
  • 我們已將檔案 URI 值新增至 documentUrl main 方法中的 變數。

將下列程式代碼範例新增至 DocIntelligence.java 檔案。 請確定您已使用 Azure 入口網站 Document Intelligence 實例中的值來更新金鑰和端點變數:


import com.azure.ai.documentintelligence;

import com.azure.ai.documentintelligence.models.AnalyzeDocumentRequest;
import com.azure.ai.documentintelligence.models.AnalyzeResult;
import com.azure.ai.documentintelligence.models.AnalyzeResultOperation;
import com.azure.ai.documentintelligence.models.DocumentTable;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.polling.SyncPoller;

import java.util.List;

public class DocIntelligence {

  // set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
  private static final String endpoint = "<your-endpoint>";
  private static final String key = "<your-key>";

  public static void main(String[] args) {

    // create your `DocumentAnalysisClient` instance and `AzureKeyCredential` variable
    DocumentIntelligenceClient client = new DocumentIntelligenceClientBuilder()
      .credential(new AzureKeyCredential(key))
      .endpoint(endpoint)
      .buildClient();

    // sample document
    String documentUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf";
    String modelId = "prebuilt-layout";

    SyncPoller <AnalyzeResultOperation, AnalyzeResultOperation> analyzeLayoutResultPoller =
      client.beginAnalyzeDocument(modelId, documentUrl);

    AnalyzeResult analyzeLayoutResult = analyzeLayoutPoller.getFinalResult().getAnalyzeResult();

    // pages
    analyzeLayoutResult.getPages().forEach(documentPage -> {
      System.out.printf("Page has width: %.2f and height: %.2f, measured with unit: %s%n",
        documentPage.getWidth(),
        documentPage.getHeight(),
        documentPage.getUnit());

      // lines
      documentPage.getLines().forEach(documentLine ->
        System.out.printf("Line '%s' is within a bounding polygon %s.%n",
          documentLine.getContent(),
          documentLine.getPolygon()));

      // words
      documentPage.getWords().forEach(documentWord ->
        System.out.printf("Word '%s' has a confidence score of %.2f.%n",
          documentWord.getContent(),
          documentWord.getConfidence()));

      // selection marks
      documentPage.getSelectionMarks().forEach(documentSelectionMark ->
        System.out.printf("Selection mark is '%s' and is within a bounding polygon %s with confidence %.2f.%n",
          documentSelectionMark.getState().toString(),
          documentSelectionMark.getPolygon(),
          documentSelectionMark.getConfidence()));
    });

    // tables
    List < DocumentTable > tables = analyzeLayoutResult.getTables();
    for (int i = 0; i < tables.size(); i++) {
      DocumentTable documentTable = tables.get(i);
      System.out.printf("Table %d has %d rows and %d columns.%n", i, documentTable.getRowCount(),
        documentTable.getColumnCount());
      documentTable.getCells().forEach(documentTableCell -> {
        System.out.printf("Cell '%s', has row index %d and column index %d.%n", documentTableCell.getContent(),
          documentTableCell.getRowIndex(), documentTableCell.getColumnIndex());
      });
      System.out.println();
    }

    // styles
    analyzeLayoutResult.getStyles().forEach(documentStyle -
      > System.out.printf("Document is handwritten %s.%n", documentStyle.isHandwritten()));
  }
}

建置並執行應用程式

將程式代碼範例新增至應用程式之後,請流覽回主要項目目錄-doc-intel-app

  1. 使用 命令建置 build 您的應用程式:

    gradle build
    
  2. 使用 命令執行 run 您的應用程式:

    gradle run
    

將下列程式代碼範例新增至 FormRecognizer.java 檔案。 請確定您已使用 Azure 入口網站 Document Intelligence 實例中的值來更新金鑰和端點變數:


import com.azure.ai.formrecognizer.*;

import com.azure.ai.formrecognizer.documentanalysis.models.*;
import com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClient;
import com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClientBuilder;

import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.polling.SyncPoller;

import java.io.IOException;
import java.util.List;
import java.util.Arrays;
import java.time.LocalDate;
import java.util.Map;
import java.util.stream.Collectors;

public class FormRecognizer {

  // set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
  private static final String endpoint = "<your-endpoint>";
  private static final String key = "<your-key>";

  public static void main(String[] args) {

    // create your `DocumentAnalysisClient` instance and `AzureKeyCredential` variable
    DocumentAnalysisClient client = new DocumentAnalysisClientBuilder()
      .credential(new AzureKeyCredential(key))
      .endpoint(endpoint)
      .buildClient();

    // sample document
    String documentUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf";
    String modelId = "prebuilt-layout";

    SyncPoller < OperationResult, AnalyzeResult > analyzeLayoutResultPoller =
      client.beginAnalyzeDocumentFromUrl(modelId, documentUrl);

    AnalyzeResult analyzeLayoutResult = analyzeLayoutResultPoller.getFinalResult();

    // pages
    analyzeLayoutResult.getPages().forEach(documentPage -> {
      System.out.printf("Page has width: %.2f and height: %.2f, measured with unit: %s%n",
        documentPage.getWidth(),
        documentPage.getHeight(),
        documentPage.getUnit());

      // lines
      documentPage.getLines().forEach(documentLine ->
        System.out.printf("Line %s is within a bounding polygon %s.%n",
          documentLine.getContent(),
          documentLine.getBoundingPolygon().toString()));

      // words
      documentPage.getWords().forEach(documentWord ->
        System.out.printf("Word '%s' has a confidence score of %.2f%n",
          documentWord.getContent(),
          documentWord.getConfidence()));

      // selection marks
      documentPage.getSelectionMarks().forEach(documentSelectionMark ->
        System.out.printf("Selection mark is %s and is within a bounding polygon %s with confidence %.2f.%n",
          documentSelectionMark.getState().toString(),
          documentSelectionMark.getBoundingPolygon().toString(),
          documentSelectionMark.getConfidence()));
    });

    // tables
    List < DocumentTable > tables = analyzeLayoutResult.getTables();
    for (int i = 0; i < tables.size(); i++) {
      DocumentTable documentTable = tables.get(i);
      System.out.printf("Table %d has %d rows and %d columns.%n", i, documentTable.getRowCount(),
        documentTable.getColumnCount());
      documentTable.getCells().forEach(documentTableCell -> {
        System.out.printf("Cell '%s', has row index %d and column index %d.%n", documentTableCell.getContent(),
          documentTableCell.getRowIndex(), documentTableCell.getColumnIndex());
      });
      System.out.println();
    }
  }
  // Utility function to get the bounding polygon coordinates
  private static String getBoundingCoordinates(List < Point > boundingPolygon) {
    return boundingPolygon.stream().map(point -> String.format("[%.2f, %.2f]", point.getX(),
      point.getY())).collect(Collectors.joining(", "));
  }
}

建置並執行應用程式

將程式代碼範例新增至應用程式之後,請流覽回主要項目目錄-form-recognize-app

  1. 使用 命令建置 build 您的應用程式:

    gradle build
    
  2. 使用 命令執行 run 您的應用程式:

    gradle run
    

版面配置模型輸出

以下是預期的輸出代碼段:

  Table 0 has 5 rows and 3 columns.
  Cell 'Title of each class', has row index 0 and column index 0.
  Cell 'Trading Symbol', has row index 0 and column index 1.
  Cell 'Name of exchange on which registered', has row index 0 and column index 2.
  Cell 'Common stock, $0.00000625 par value per share', has row index 1 and column index 0.
  Cell 'MSFT', has row index 1 and column index 1.
  Cell 'NASDAQ', has row index 1 and column index 2.
  Cell '2.125% Notes due 2021', has row index 2 and column index 0.
  Cell 'MSFT', has row index 2 and column index 1.
  Cell 'NASDAQ', has row index 2 and column index 2.
  Cell '3.125% Notes due 2028', has row index 3 and column index 0.
  Cell 'MSFT', has row index 3 and column index 1.
  Cell 'NASDAQ', has row index 3 and column index 2.
  Cell '2.625% Notes due 2033', has row index 4 and column index 0.
  Cell 'MSFT', has row index 4 and column index 1.
  Cell 'NASDAQ', has row index 4 and column index 2.

若要檢視整個輸出,請造訪 GitHub 上的 Azure 範例存放庫,以檢視 版面配置模型輸出

將下列程式代碼範例新增至 FormRecognizer.java 檔案。 請確定您已使用 Azure 入口網站 Document Intelligence 實例中的值來更新金鑰和端點變數:

import com.azure.ai.formrecognizer;

import com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClient;
import com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClientBuilder;
import com.azure.ai.formrecognizer.documentanalysis.models.AnalyzeResult;
import com.azure.ai.formrecognizer.documentanalysis.models.OperationResult;
import com.azure.ai.formrecognizer.documentanalysis.models.DocumentTable;
import com.azure.ai.formrecognizer.documentanalysis.models.Point;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.polling.SyncPoller;

import java.util.List;
import java.util.stream.Collectors;

public class FormRecognizer {

  // set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
  private static final String endpoint = "<your-endpoint>";
  private static final String key = "<your-key>";

  public static void main(String[] args) {

    // create your `DocumentAnalysisClient` instance and `AzureKeyCredential` variable
    DocumentAnalysisClient client = new DocumentAnalysisClientBuilder()
      .credential(new AzureKeyCredential(key))
      .endpoint(endpoint)
      .buildClient();

    // sample document
    String documentUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf";
    String modelId = "prebuilt-layout";

    SyncPoller < OperationResult, AnalyzeResult > analyzeLayoutPoller =
      client.beginAnalyzeDocumentFromUrl(modelId, documentUrl);

    AnalyzeResult analyzeLayoutResult = analyzeLayoutPoller.getFinalResult();

    // pages
    analyzeLayoutResult.getPages().forEach(documentPage -> {
      System.out.printf("Page has width: %.2f and height: %.2f, measured with unit: %s%n",
        documentPage.getWidth(),
        documentPage.getHeight(),
        documentPage.getUnit());

      // lines
      documentPage.getLines().forEach(documentLine ->
        System.out.printf("Line '%s' is within a bounding polygon %s.%n",
          documentLine.getContent(),
          getBoundingCoordinates(documentLine.getBoundingPolygon())));

      // words
      documentPage.getWords().forEach(documentWord ->
        System.out.printf("Word '%s' has a confidence score of %.2f.%n",
          documentWord.getContent(),
          documentWord.getConfidence()));

      // selection marks
      documentPage.getSelectionMarks().forEach(documentSelectionMark ->
        System.out.printf("Selection mark is '%s' and is within a bounding polygon %s with confidence %.2f.%n",
          documentSelectionMark.getSelectionMarkState().toString(),
          getBoundingCoordinates(documentSelectionMark.getBoundingPolygon()),
          documentSelectionMark.getConfidence()));
    });

    // tables
    List < DocumentTable > tables = analyzeLayoutResult.getTables();
    for (int i = 0; i < tables.size(); i++) {
      DocumentTable documentTable = tables.get(i);
      System.out.printf("Table %d has %d rows and %d columns.%n", i, documentTable.getRowCount(),
        documentTable.getColumnCount());
      documentTable.getCells().forEach(documentTableCell -> {
        System.out.printf("Cell '%s', has row index %d and column index %d.%n", documentTableCell.getContent(),
          documentTableCell.getRowIndex(), documentTableCell.getColumnIndex());
      });
      System.out.println();
    }

    // styles
    analyzeLayoutResult.getStyles().forEach(documentStyle -
      > System.out.printf("Document is handwritten %s.%n", documentStyle.isHandwritten()));
  }

  /**
   * Utility function to get the bounding polygon coordinates.
   */
  private static String getBoundingCoordinates(List < Point > boundingPolygon) {
    return boundingPolygon.stream().map(point -> String.format("[%.2f, %.2f]", point.getX(),
      point.getY())).collect(Collectors.joining(", "));
  }
}

建置並執行應用程式

將程式代碼範例新增至應用程式之後,請流覽回主要項目目錄-form-recognize-app

  1. 使用 命令建置 build 您的應用程式:

    gradle build
    
  2. 使用 命令執行 run 您的應用程式:

    gradle run
    

預先建置的模型

使用預先建置的模型,從特定文件類型分析及擷取一般欄位。 在此範例中,我們會使用 預先建置的發票模型來分析發票

提示

您不限於發票,有數個預先建置的模型可供選擇,每個模型都有自己的支援欄位集。 要用於作業的 analyze 模型取決於要分析的文件類型。 請參閱模型數據擷取

  • 使用預建發票模型分析發票。 您可以針對本快速入門使用我們的 範例發票檔
  • 我們已將檔案 URL 值新增至 invoiceUrl 檔案頂端的 變數。
  • 若要在 URI 分析指定的檔案,您將使用 beginAnalyzeDocuments 方法,並傳遞 PrebuiltModels.Invoice 作為模型識別碼。傳回的值是物件, result 其中包含所提交文件的相關數據。
  • 為了簡單起見,服務傳回的所有索引鍵/值組不會在此顯示。 若要查看所有支援的欄位和對應類型清單,請參閱我們的 發票 概念頁面。

將下列程式代碼範例新增至 DocIntelligence.java 檔案。 請確定您已使用 Azure 入口網站 Document Intelligence 實例中的值來更新金鑰和端點變數:

import com.azure.ai.documentintelligence;

import com.azure.ai.documentintelligence.models.AnalyzeDocumentRequest;
import com.azure.ai.documentintelligence.models.AnalyzeResult;
import com.azure.ai.documentintelligence.models.AnalyzeResultOperation;
import com.azure.ai.documentintelligence.models.Document;
import com.azure.ai.documentintelligence.models.DocumentField;
import com.azure.ai.documentintelligence.models.DocumentFieldType;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.polling.SyncPoller;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

public class DocIntelligence {

  // set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
  private static final String endpoint = "<your-endpoint>";
  private static final String key = "<your-key>";

  public static void main(String[] args) {

    // create your `DocumentAnalysisClient` instance and `AzureKeyCredential` variable
    DocumentIntelligenceClient client = new DocumentIntelligenceClientBuilder()
      .credential(new AzureKeyCredential(key))
      .endpoint(endpoint)
      .buildClient();

    // sample document
    String modelId = "prebuilt-invoice";
    String invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf";

    public static void main(final String[] args) throws IOException {
      // Instantiate a client that will be used to call the service.
      DocumentIntelligenceClient client = new DocumentIntelligenceClientBuilder()
        .credential(new AzureKeyCredential("{key}"))
        .endpoint("https://{endpoint}.cognitiveservices.azure.com/")
        .buildClient();

      SyncPoller < OperationResult, AnalyzeResult > analyzeLayoutResultPoller =
        client.beginAnalyzeDocument(modelId, invoiceUrl);

      AnalyzeResult analyzeInvoiceResult = analyzeInvoicesPoller.getFinalResult().getAnalyzeResult();

      for (int i = 0; i < analyzeInvoiceResult.getDocuments().size(); i++) {
        Document analyzedInvoice = analyzeInvoiceResult.getDocuments().get(i);
        Map < String, DocumentField > invoiceFields = analyzedInvoice.getFields();
        System.out.printf("----------- Analyzing invoice  %d -----------%n", i);
        DocumentField vendorNameField = invoiceFields.get("VendorName");
        if (vendorNameField != null) {
          if (DocumentFieldType.STRING == vendorNameField.getType()) {
            String merchantName = vendorNameField.getValueString();
            System.out.printf("Vendor Name: %s, confidence: %.2f%n",
              merchantName, vendorNameField.getConfidence());
          }
        }

        DocumentField vendorAddressField = invoiceFields.get("VendorAddress");
        if (vendorAddressField != null) {
          if (DocumentFieldType.STRING == vendorAddressField.getType()) {
            String merchantAddress = vendorAddressField.getValueString();
            System.out.printf("Vendor address: %s, confidence: %.2f%n",
              merchantAddress, vendorAddressField.getConfidence());
          }
        }

        DocumentField customerNameField = invoiceFields.get("CustomerName");
        if (customerNameField != null) {
          if (DocumentFieldType.STRING == customerNameField.getType()) {
            String merchantAddress = customerNameField.getValueString();
            System.out.printf("Customer Name: %s, confidence: %.2f%n",
              merchantAddress, customerNameField.getConfidence());
          }
        }

        DocumentField customerAddressRecipientField = invoiceFields.get("CustomerAddressRecipient");
        if (customerAddressRecipientField != null) {
          if (DocumentFieldType.STRING == customerAddressRecipientField.getType()) {
            String customerAddr = customerAddressRecipientField.getValueString();
            System.out.printf("Customer Address Recipient: %s, confidence: %.2f%n",
              customerAddr, customerAddressRecipientField.getConfidence());
          }
        }

        DocumentField invoiceIdField = invoiceFields.get("InvoiceId");
        if (invoiceIdField != null) {
          if (DocumentFieldType.STRING == invoiceIdField.getType()) {
            String invoiceId = invoiceIdField.getValueString();
            System.out.printf("Invoice ID: %s, confidence: %.2f%n",
              invoiceId, invoiceIdField.getConfidence());
          }
        }

        DocumentField invoiceDateField = invoiceFields.get("InvoiceDate");
        if (customerNameField != null) {
          if (DocumentFieldType.DATE == invoiceDateField.getType()) {
            LocalDate invoiceDate = invoiceDateField.getValueDate();
            System.out.printf("Invoice Date: %s, confidence: %.2f%n",
              invoiceDate, invoiceDateField.getConfidence());
          }
        }

        DocumentField invoiceTotalField = invoiceFields.get("InvoiceTotal");
        if (customerAddressRecipientField != null) {
          if (DocumentFieldType.NUMBER == invoiceTotalField.getType()) {
            Double invoiceTotal = invoiceTotalField.getValueNumber();
            System.out.printf("Invoice Total: %.2f, confidence: %.2f%n",
              invoiceTotal, invoiceTotalField.getConfidence());
          }
        }

        DocumentField invoiceItemsField = invoiceFields.get("Items");
        if (invoiceItemsField != null) {
          System.out.printf("Invoice Items: %n");
          if (DocumentFieldType.ARRAY == invoiceItemsField.getType()) {
            List < DocumentField > invoiceItems = invoiceItemsField.getValueArray();
            invoiceItems.stream()
              .filter(invoiceItem -> DocumentFieldType.OBJECT == invoiceItem.getType())
              .map(documentField -> documentField.getValueObject())
              .forEach(documentFieldMap -> documentFieldMap.forEach((key, documentField) -> {

                // See a full list of fields found on an invoice here:
                // https://aka.ms/documentintelligence/invoicefields

                if ("Description".equals(key)) {
                  if (DocumentFieldType.STRING == documentField.getType()) {
                    String name = documentField.getValueString();
                    System.out.printf("Description: %s, confidence: %.2fs%n",
                      name, documentField.getConfidence());
                  }
                }
                if ("Quantity".equals(key)) {
                  if (DocumentFieldType.NUMBER == documentField.getType()) {
                    Double quantity = documentField.getValueNumber();
                    System.out.printf("Quantity: %f, confidence: %.2f%n",
                      quantity, documentField.getConfidence());
                  }
                }
                if ("UnitPrice".equals(key)) {
                  if (DocumentFieldType.NUMBER == documentField.getType()) {
                    Double unitPrice = documentField.getValueNumber();
                    System.out.printf("Unit Price: %f, confidence: %.2f%n",
                      unitPrice, documentField.getConfidence());
                  }
                }
                if ("ProductCode".equals(key)) {
                  if (DocumentFieldType.NUMBER == documentField.getType()) {
                    Double productCode = documentField.getValueNumber();
                    System.out.printf("Product Code: %f, confidence: %.2f%n",
                      productCode, documentField.getConfidence());
                  }
                }
              }));
          }
        }
      }
    }
  }
}

建置並執行應用程式

將程式代碼範例新增至應用程式之後,請流覽回主要項目目錄-doc-intel-app

  1. 使用 命令建置 build 您的應用程式:

    gradle build
    
  2. 使用 命令執行 run 您的應用程式:

    gradle run
    

將下列程式代碼範例新增至 FormRecognizer.java 檔案。 請確定您已使用 Azure 入口網站 Document Intelligence 實例中的值來更新金鑰和端點變數:


import com.azure.ai.formrecognizer.*;

import com.azure.ai.formrecognizer.documentanalysis.models.*;
import com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClient;
import com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClientBuilder;

import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.polling.SyncPoller;

import java.io.IOException;
import java.util.List;
import java.util.Arrays;
import java.time.LocalDate;
import java.util.Map;
import java.util.stream.Collectors;

public class FormRecognizer {

  // set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
  private static final String endpoint = "<your-endpoint>";
  private static final String key = "<your-key>";

  public static void main(final String[] args) throws IOException {

    // create your `DocumentAnalysisClient` instance and `AzureKeyCredential` variable
    DocumentAnalysisClient client = new DocumentAnalysisClientBuilder()
      .credential(new AzureKeyCredential(key))
      .endpoint(endpoint)
      .buildClient();

    // sample document
    String modelId = "prebuilt-invoice";
    String invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf";

    SyncPoller < OperationResult, AnalyzeResult > analyzeInvoicePoller = client.beginAnalyzeDocumentFromUrl(modelId, invoiceUrl);

    AnalyzeResult analyzeInvoiceResult = analyzeInvoicePoller.getFinalResult();

    for (int i = 0; i < analyzeInvoiceResult.getDocuments().size(); i++) {
      AnalyzedDocument analyzedInvoice = analyzeInvoiceResult.getDocuments().get(i);
      Map < String, DocumentField > invoiceFields = analyzedInvoice.getFields();
      System.out.printf("----------- Analyzing invoice  %d -----------%n", i);
      DocumentField vendorNameField = invoiceFields.get("VendorName");
      if (vendorNameField != null) {
        if (DocumentFieldType.STRING == vendorNameField.getType()) {
          String merchantName = vendorNameField.getValueAsString();
          System.out.printf("Vendor Name: %s, confidence: %.2f%n",
            merchantName, vendorNameField.getConfidence());
        }
      }

      DocumentField vendorAddressField = invoiceFields.get("VendorAddress");
      if (vendorAddressField != null) {
        if (DocumentFieldType.STRING == vendorAddressField.getType()) {
          String merchantAddress = vendorAddressField.getValueAsString();
          System.out.printf("Vendor address: %s, confidence: %.2f%n",
            merchantAddress, vendorAddressField.getConfidence());
        }
      }

      DocumentField customerNameField = invoiceFields.get("CustomerName");
      if (customerNameField != null) {
        if (DocumentFieldType.STRING == customerNameField.getType()) {
          String merchantAddress = customerNameField.getValueAsString();
          System.out.printf("Customer Name: %s, confidence: %.2f%n",
            merchantAddress, customerNameField.getConfidence());
        }
      }

      DocumentField customerAddressRecipientField = invoiceFields.get("CustomerAddressRecipient");
      if (customerAddressRecipientField != null) {
        if (DocumentFieldType.STRING == customerAddressRecipientField.getType()) {
          String customerAddr = customerAddressRecipientField.getValueAsString();
          System.out.printf("Customer Address Recipient: %s, confidence: %.2f%n",
            customerAddr, customerAddressRecipientField.getConfidence());
        }
      }

      DocumentField invoiceIdField = invoiceFields.get("InvoiceId");
      if (invoiceIdField != null) {
        if (DocumentFieldType.STRING == invoiceIdField.getType()) {
          String invoiceId = invoiceIdField.getValueAsString();
          System.out.printf("Invoice ID: %s, confidence: %.2f%n",
            invoiceId, invoiceIdField.getConfidence());
        }
      }

      DocumentField invoiceDateField = invoiceFields.get("InvoiceDate");
      if (customerNameField != null) {
        if (DocumentFieldType.DATE == invoiceDateField.getType()) {
          LocalDate invoiceDate = invoiceDateField.getValueAsDate();
          System.out.printf("Invoice Date: %s, confidence: %.2f%n",
            invoiceDate, invoiceDateField.getConfidence());
        }
      }

      DocumentField invoiceTotalField = invoiceFields.get("InvoiceTotal");
      if (customerAddressRecipientField != null) {
        if (DocumentFieldType.DOUBLE == invoiceTotalField.getType()) {
          Double invoiceTotal = invoiceTotalField.getValueAsDouble();
          System.out.printf("Invoice Total: %.2f, confidence: %.2f%n",
            invoiceTotal, invoiceTotalField.getConfidence());
        }
      }

      DocumentField invoiceItemsField = invoiceFields.get("Items");
      if (invoiceItemsField != null) {
        System.out.printf("Invoice Items: %n");
        if (DocumentFieldType.LIST == invoiceItemsField.getType()) {
          List < DocumentField > invoiceItems = invoiceItemsField.getValueAsList();
          invoiceItems.stream()
            .filter(invoiceItem -> DocumentFieldType.MAP == invoiceItem.getType())
            .map(documentField -> documentField.getValueAsMap())
            .forEach(documentFieldMap -> documentFieldMap.forEach((key, documentField) -> {

              // See a full list of fields found on an invoice here:
              // https://aka.ms/formrecognizer/invoicefields

              if ("Description".equals(key)) {
                if (DocumentFieldType.STRING == documentField.getType()) {
                  String name = documentField.getValueAsString();
                  System.out.printf("Description: %s, confidence: %.2fs%n",
                    name, documentField.getConfidence());
                }
              }
              if ("Quantity".equals(key)) {
                if (DocumentFieldType.DOUBLE == documentField.getType()) {
                  Double quantity = documentField.getValueAsDouble();
                  System.out.printf("Quantity: %f, confidence: %.2f%n",
                    quantity, documentField.getConfidence());
                }
              }
              if ("UnitPrice".equals(key)) {
                if (DocumentFieldType.DOUBLE == documentField.getType()) {
                  Double unitPrice = documentField.getValueAsDouble();
                  System.out.printf("Unit Price: %f, confidence: %.2f%n",
                    unitPrice, documentField.getConfidence());
                }
              }
              if ("ProductCode".equals(key)) {
                if (DocumentFieldType.DOUBLE == documentField.getType()) {
                  Double productCode = documentField.getValueAsDouble();
                  System.out.printf("Product Code: %f, confidence: %.2f%n",
                    productCode, documentField.getConfidence());
                }
              }
            }));
        }
      }
    }
  }
}

建置並執行應用程式

將程式代碼範例新增至應用程式之後,請流覽回主要項目目錄-doc-intel-app

  1. 使用 命令建置 build 您的應用程式:

    gradle build
    
  2. 使用 命令執行 run 您的應用程式:

    gradle run
    

預先建置的模型輸出

以下是預期的輸出代碼段:

  ----------- Analyzing invoice  0 -----------
  Analyzed document has doc type invoice with confidence : 1.00
  Vendor Name: CONTOSO LTD., confidence: 0.92
  Vendor address: 123 456th St New York, NY, 10001, confidence: 0.91
  Customer Name: MICROSOFT CORPORATION, confidence: 0.84
  Customer Address Recipient: Microsoft Corp, confidence: 0.92
  Invoice ID: INV-100, confidence: 0.97
  Invoice Date: 2019-11-15, confidence: 0.97

若要檢視整個輸出,請造訪 GitHub 上的 Azure 範例存放庫,以檢視 預先建置的發票模型輸出

將下列程式代碼範例新增至 FormRecognizer.java 檔案。 請確定您已使用 Azure 入口網站 Document Intelligence 實例中的值來更新金鑰和端點變數:

import com.azure.ai.formrecognizer;

import com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClient;
import com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClientBuilder;
import com.azure.ai.formrecognizer.documentanalysis.models.AnalyzeResult;
import com.azure.ai.formrecognizer.documentanalysis.models.AnalyzedDocument;
import com.azure.ai.formrecognizer.documentanalysis.models.DocumentField;
import com.azure.ai.formrecognizer.documentanalysis.models.DocumentFieldType;
import com.azure.ai.formrecognizer.documentanalysis.models.OperationResult;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.polling.SyncPoller;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

public class FormRecognizer {

  // set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
  private static final String endpoint = "<your-endpoint>";
  private static final String key = "<your-key>";

  public static void main(String[] args) {

    // create your `DocumentAnalysisClient` instance and `AzureKeyCredential` variable
    DocumentAnalysisClient client = new DocumentAnalysisClientBuilder()
      .credential(new AzureKeyCredential(key))
      .endpoint(endpoint)
      .buildClient();

    // sample document
    String modelId = "prebuilt-invoice";
    String invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf";

    SyncPoller < OperationResult, AnalyzeResult > analyzeInvoicePoller = client.beginAnalyzeDocumentFromUrl(modelId, invoiceUrl);

    AnalyzeResult analyzeInvoiceResult = analyzeInvoicePoller.getFinalResult();

    for (int i = 0; i < analyzeInvoiceResult.getDocuments().size(); i++) {
      AnalyzedDocument analyzedInvoice = analyzeInvoiceResult.getDocuments().get(i);
      Map < String, DocumentField > invoiceFields = analyzedInvoice.getFields();
      System.out.printf("----------- Analyzing invoice  %d -----------%n", i);
      DocumentField vendorNameField = invoiceFields.get("VendorName");
      if (vendorNameField != null) {
        if (DocumentFieldType.STRING == vendorNameField.getType()) {
          String merchantName = vendorNameField.getValueAsString();
          System.out.printf("Vendor Name: %s, confidence: %.2f%n",
            merchantName, vendorNameField.getConfidence());
        }
      }

      DocumentField vendorAddressField = invoiceFields.get("VendorAddress");
      if (vendorAddressField != null) {
        if (DocumentFieldType.STRING == vendorAddressField.getType()) {
          String merchantAddress = vendorAddressField.getValueAsString();
          System.out.printf("Vendor address: %s, confidence: %.2f%n",
            merchantAddress, vendorAddressField.getConfidence());
        }
      }

      DocumentField customerNameField = invoiceFields.get("CustomerName");
      if (customerNameField != null) {
        if (DocumentFieldType.STRING == customerNameField.getType()) {
          String merchantAddress = customerNameField.getValueAsString();
          System.out.printf("Customer Name: %s, confidence: %.2f%n",
            merchantAddress, customerNameField.getConfidence());
        }
      }

      DocumentField customerAddressRecipientField = invoiceFields.get("CustomerAddressRecipient");
      if (customerAddressRecipientField != null) {
        if (DocumentFieldType.STRING == customerAddressRecipientField.getType()) {
          String customerAddr = customerAddressRecipientField.getValueAsString();
          System.out.printf("Customer Address Recipient: %s, confidence: %.2f%n",
            customerAddr, customerAddressRecipientField.getConfidence());
        }
      }

      DocumentField invoiceIdField = invoiceFields.get("InvoiceId");
      if (invoiceIdField != null) {
        if (DocumentFieldType.STRING == invoiceIdField.getType()) {
          String invoiceId = invoiceIdField.getValueAsString();
          System.out.printf("Invoice ID: %s, confidence: %.2f%n",
            invoiceId, invoiceIdField.getConfidence());
        }
      }

      DocumentField invoiceDateField = invoiceFields.get("InvoiceDate");
      if (customerNameField != null) {
        if (DocumentFieldType.DATE == invoiceDateField.getType()) {
          LocalDate invoiceDate = invoiceDateField.getValueAsDate();
          System.out.printf("Invoice Date: %s, confidence: %.2f%n",
            invoiceDate, invoiceDateField.getConfidence());
        }
      }

      DocumentField invoiceTotalField = invoiceFields.get("InvoiceTotal");
      if (customerAddressRecipientField != null) {
        if (DocumentFieldType.DOUBLE == invoiceTotalField.getType()) {
          Double invoiceTotal = invoiceTotalField.getValueAsDouble();
          System.out.printf("Invoice Total: %.2f, confidence: %.2f%n",
            invoiceTotal, invoiceTotalField.getConfidence());
        }
      }

      DocumentField invoiceItemsField = invoiceFields.get("Items");
      if (invoiceItemsField != null) {
        System.out.printf("Invoice Items: %n");
        if (DocumentFieldType.LIST == invoiceItemsField.getType()) {
          List < DocumentField > invoiceItems = invoiceItemsField.getValueAsList();
          invoiceItems.stream()
            .filter(invoiceItem -> DocumentFieldType.MAP == invoiceItem.getType())
            .map(documentField -> documentField.getValueAsMap())
            .forEach(documentFieldMap -> documentFieldMap.forEach((key, documentField) -> {

              // See a full list of fields found on an invoice here:
              // https://aka.ms/formrecognizer/invoicefields

              if ("Description".equals(key)) {
                if (DocumentFieldType.STRING == documentField.getType()) {
                  String name = documentField.getValueAsString();
                  System.out.printf("Description: %s, confidence: %.2fs%n",
                    name, documentField.getConfidence());
                }
              }
              if ("Quantity".equals(key)) {
                if (DocumentFieldType.DOUBLE == documentField.getType()) {
                  Double quantity = documentField.getValueAsDouble();
                  System.out.printf("Quantity: %f, confidence: %.2f%n",
                    quantity, documentField.getConfidence());
                }
              }
              if ("UnitPrice".equals(key)) {
                if (DocumentFieldType.DOUBLE == documentField.getType()) {
                  Double unitPrice = documentField.getValueAsDouble();
                  System.out.printf("Unit Price: %f, confidence: %.2f%n",
                    unitPrice, documentField.getConfidence());
                }
              }
              if ("ProductCode".equals(key)) {
                if (DocumentFieldType.DOUBLE == documentField.getType()) {
                  Double productCode = documentField.getValueAsDouble();
                  System.out.printf("Product Code: %f, confidence: %.2f%n",
                    productCode, documentField.getConfidence());
                }
              }
            }));
        }
      }
    }
  }
}

建置並執行應用程式

將程式代碼範例新增至應用程式之後,請流覽回主要項目目錄-doc-intel-app

  1. 使用 命令建置 build 您的應用程式:

    gradle build
    
  2. 使用 命令執行 run 您的應用程式:

    gradle run
    

在本快速入門中,使用下列功能來分析及擷取表單和文件中的數據和值:

  • 版面配置:分析及擷取檔中的單選按鈕和複選框等表格、線條、單字和選取標記,而不需要定型模型。

  • 預先建置的發票 — 使用預先定型的發票模型,分析及擷取特定文件類型的一般字段。

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶

  • 最新版的 Visual Studio Code 或您慣用的 IDE。 如需詳細資訊, 請參閱Visual Studio Code 中的Node.js。

  • 最新版本 LTSNode.js

  • Azure AI 服務或文件智慧服務資源。 擁有 Azure 訂閱後,請在 Azure 入口網站中建立單一服務多重服務文件智慧服務資源,以取得您的金鑰和端點。 您可以使用免費定價層 (F0) 來試用服務,之後可升級至付費層以用於實際執行環境。

    提示

    如果您打算在單一端點/金鑰下存取多個 Azure AI 服務,請建立 Azure AI 服務資源。 若為僅限文件智慧服務存取,請建立文件智慧服務資源。 請注意,如果您想要使用 Microsoft Entra 驗證,您將需要單一服務資源。

  • 部署資源之後,選取 [移至資源]。 您需要使用已建立資源的金鑰和端點,將應用程式連線至文件智慧服務 API。 您稍後會在快速入門中將金鑰和端點貼上至程式碼中:

    Azure 入口網站 中金鑰和端點位置的螢幕快照。

設定

  1. 建立新的 Node.js Express 應用程式:在控制台視窗中(例如 cmd、PowerShell 或 Bash),建立並流覽至名為 doc-intel-app的應用程式的新目錄。

    mkdir doc-intel-app && cd doc-intel-app
    
  2. npm init執行 命令來初始化應用程式,並建構您的專案。

    npm init
    
  3. 使用終端機中顯示的提示來指定項目的屬性。

    • 最重要的屬性是名稱、版本號碼和進入點。
    • 我們建議保留 index.js 進入點名稱。 描述、測試命令、GitHub 存放庫、關鍵詞、作者和授權資訊都是選擇性屬性,因此可以略過這個專案。
    • 選取 [傳回] 或 [Enter],以接受括弧中的建議。
    • 完成提示之後, package.json 將會在 doc-intel-app 目錄中建立檔案。
  1. 安裝客戶端連結 ai-document-intelligence 庫與 azure/identity npm 套件:

    npm i @azure-rest/ai-document-intelligence@1.0.0-beta.2 @azure/identity
    
    

    您的應用程式檔案 package.json 會隨著相依性更新。

  1. 安裝客戶端連結 ai-form-recognizer 庫與 azure/identity npm 套件:

    npm i @azure/ai-form-recognizer@5.0.0 @azure/identity
    
    • 您的應用程式檔案 package.json 會隨著相依性更新。
  1. 安裝客戶端連結 ai-form-recognizer 庫與 azure/identity npm 套件:

    npm i @azure/ai-form-recognizer@4.0.0 @azure/identity
    
  1. 在應用程式目錄中建立名為 index.js 的檔案。

    提示

    • 您可以使用 PowerShell 建立新的檔案。
    • 按住 Shift 鍵,並在資料夾上按下滑鼠右鍵,在您的專案目錄中開啟 PowerShell 視窗。
    • 輸入下列命令 New-Item index.js

建置您的 應用程式

若要與此文件智慧服務互動,您必須建立 DocumentIntelligenceClient 類別的執行個體。 若要這樣做,請使用 key 從 Azure 入口網站建立 AzureKeyCredential,並使用 AzureKeyCredential 和文件智慧服務 endpoint 來建立 DocumentIntelligenceClient 執行個體。

若要與此文件智慧服務互動,您必須建立 DocumentAnalysisClient 類別的執行個體。 若要這樣做,您可以使用 Azure 入口網站 建立 ,AzureKeyCredential並使用 和 表格辨識器 endpoint來建立 實例AzureKeyCredentialDocumentAnalysisClientkey

  1. index.js Visual Studio Code 或您慣用的 IDE 中開啟檔案。 將下列其中一個程式代碼範例複製並貼到您的應用程式中:

重要

完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性

版面配置模型

從檔擷取文字、選取標記、文字樣式、表格結構和周框區域座標。

  • 在此範例中,您需要 來自 URL 的檔案檔案。 您可以使用本快速入門的 範例檔
  • 我們已將檔案 URL 值新增至 formUrl 靠近檔案頂端的變數。
  • 若要從 URL 分析指定的檔案,您將使用 beginAnalyzeDocuments 方法並傳入 prebuilt-layout 作為模型識別碼。
    const { DocumentIntelligenceClient } = require("@azure-rest/ai-document-intelligence");
    const  { AzureKeyCredential } = require("@azure/core-auth");

    // set `<your-key>` and `<your-endpoint>` variables with the values from the Azure portal.
    const key = "<your-key";
    const endpoint = "<your-endpoint>";

    // sample document
  const formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"

async function main() {
    const client = DocumentIntelligenceClient(endpoint, new AzureKeyCredential(key));

    const poller = await client.beginAnalyzeDocument("prebuilt-layout", formUrl);

const {
        pages,
        tables
    } = await poller.pollUntilDone();

    if (pages.length <= 0) {
        console.log("No pages were extracted from the document.");
    } else {
        console.log("Pages:");
        for (const page of pages) {
            console.log("- Page", page.pageNumber, `(unit: ${page.unit})`);
            console.log(`  ${page.width}x${page.height}, angle: ${page.angle}`);
            console.log(`  ${page.lines.length} lines, ${page.words.length} words`);
        }
    }

    if (tables.length <= 0) {
        console.log("No tables were extracted from the document.");
    } else {
        console.log("Tables:");
        for (const table of tables) {
            console.log(
                `- Extracted table: ${table.columnCount} columns, ${table.rowCount} rows (${table.cells.length} cells)`
            );
        }
    }
}

main().catch((error) => {
    console.error("An error occurred:", error);
    process.exit(1);
});

執行您的應用程式

將程式代碼範例新增至應用程式之後,請執行程式:

  1. 流覽至您有 Document Intelligence 應用程式的資料夾(doc-intel-app)。

  2. 在您的終端機中輸入下列命令:

    node index.js
    

將下列程式代碼範例新增至 index.js 檔案。 請確定您已使用 Azure 入口網站 Document Intelligence 實例中的值來更新金鑰和端點變數:


 const { AzureKeyCredential, DocumentAnalysisClient } = require("@azure/ai-form-recognizer");

    // set `<your-key>` and `<your-endpoint>` variables with the values from the Azure portal.
    const key = "<your-key>";
    const endpoint = "<your-endpoint>";

    // sample document
  const formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"

  async function main() {
    const client = new DocumentAnalysisClient(endpoint, new AzureKeyCredential(key));

    const poller = await client.beginAnalyzeDocumentFromUrl("prebuilt-layout", formUrl);

    const {
        pages,
        tables
    } = await poller.pollUntilDone();

    if (pages.length <= 0) {
        console.log("No pages were extracted from the document.");
    } else {
        console.log("Pages:");
        for (const page of pages) {
            console.log("- Page", page.pageNumber, `(unit: ${page.unit})`);
            console.log(`  ${page.width}x${page.height}, angle: ${page.angle}`);
            console.log(`  ${page.lines.length} lines, ${page.words.length} words`);
        }
    }

    if (tables.length <= 0) {
        console.log("No tables were extracted from the document.");
    } else {
        console.log("Tables:");
        for (const table of tables) {
            console.log(
                `- Extracted table: ${table.columnCount} columns, ${table.rowCount} rows (${table.cells.length} cells)`
            );
        }
    }
}

main().catch((error) => {
    console.error("An error occurred:", error);
    process.exit(1);
});

執行您的應用程式

將程式代碼範例新增至應用程式之後,請執行程式:

  1. 流覽至您有 Document Intelligence 應用程式的資料夾(doc-intel-app)。

  2. 在您的終端機中輸入下列命令:

    node index.js
    

版面配置模型輸出

以下是預期的輸出代碼段:

Pages:
- Page 1 (unit: inch)
  8.5x11, angle: 0
  69 lines, 425 words
Tables:
- Extracted table: 3 columns, 5 rows (15 cells)

若要檢視整個輸出,請造訪 GitHub 上的 Azure 範例存放庫,以檢視 版面配置模型輸出

預先建置的模型

在此範例中,我們會使用 預先建置的發票模型來分析發票

提示

您不限於發票,有數個預先建置的模型可供選擇,每個模型都有自己的支援欄位集。 要用於作業的 analyze 模型取決於要分析的文件類型。 請參閱模型數據擷取

  • 使用預建發票模型分析發票。 您可以針對本快速入門使用我們的 範例發票檔
  • 我們已將檔案 URL 值新增至 invoiceUrl 檔案頂端的 變數。
  • 若要在 URI 分析指定的檔案,您將使用 beginAnalyzeDocuments 方法,並傳遞 PrebuiltModels.Invoice 作為模型識別碼。傳回的值是物件, result 其中包含所提交文件的相關數據。
  • 為了簡單起見,服務傳回的所有索引鍵/值組不會在此顯示。 若要查看所有支援的欄位和對應類型清單,請參閱我們的 發票 概念頁面。

const { DocumentIntelligenceClient } = require("@azure-rest/ai-document-intelligence");
const  { AzureKeyCredential } = require("@azure/core-auth");

    // set `<your-key>` and `<your-endpoint>` variables with the values from the Azure portal.
    const key = "<your-key>";
    const endpoint = "<your-endpoint>";

    // sample document
    invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"

async function main() {
    const client = DocumentIntelligenceClient(endpoint, new AzureKeyCredential(key));

    const poller = await client.beginAnalyzeDocument("prebuilt-invoice", invoiceUrl);
if (pages.length <= 0) {
        console.log("No pages were extracted from the document.");
    } else {
        console.log("Pages:");
        for (const page of pages) {
            console.log("- Page", page.pageNumber, `(unit: ${page.unit})`);
            console.log(`  ${page.width}x${page.height}, angle: ${page.angle}`);
            console.log(`  ${page.lines.length} lines, ${page.words.length} words`);
        }
    }

    if (tables.length <= 0) {
        console.log("No tables were extracted from the document.");
    } else {
        console.log("Tables:");
        for (const table of tables) {
            console.log(
                `- Extracted table: ${table.columnCount} columns, ${table.rowCount} rows (${table.cells.length} cells)`
            );
        }
    }
}

main().catch((error) => {
    console.error("An error occurred:", error);
    process.exit(1);
});

執行您的應用程式

將程式代碼範例新增至應用程式之後,請執行程式:

  1. 流覽至您有 Document Intelligence 應用程式的資料夾(doc-intel-app)。

  2. 在您的終端機中輸入下列命令:

    node index.js
    

 const {
    AzureKeyCredential,
    DocumentAnalysisClient
} = require("@azure/ai-form-recognizer");

// set `<your-key>` and `<your-endpoint>` variables with the values from the Azure portal.
const key = "<your-key>";
const endpoint = "<your-endpoint>";
// sample document
invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"

async function main() {
    const client = new DocumentAnalysisClient(endpoint, new AzureKeyCredential(key));

    const poller = await client.beginAnalyzeDocumentFromUrl("prebuilt-invoice", invoiceUrl);

    const {
        pages,
        tables
    } = await poller.pollUntilDone();

    if (pages.length <= 0) {
        console.log("No pages were extracted from the document.");
    } else {
        console.log("Pages:");
        for (const page of pages) {
            console.log("- Page", page.pageNumber, `(unit: ${page.unit})`);
            console.log(`  ${page.width}x${page.height}, angle: ${page.angle}`);
            console.log(`  ${page.lines.length} lines, ${page.words.length} words`);

            if (page.lines && page.lines.length > 0) {
                console.log("  Lines:");

                for (const line of page.lines) {
                    console.log(`  - "${line.content}"`);

                    // The words of the line can also be iterated independently. The words are computed based on their
                    // corresponding spans.
                    for (const word of line.words()) {
                        console.log(`    - "${word.content}"`);
                    }
                }
            }
        }
    }

    if (tables.length <= 0) {
        console.log("No tables were extracted from the document.");
    } else {
        console.log("Tables:");
        for (const table of tables) {
            console.log(
                `- Extracted table: ${table.columnCount} columns, ${table.rowCount} rows (${table.cells.length} cells)`
            );
        }
    }
}

main().catch((error) => {
    console.error("An error occurred:", error);
    process.exit(1);
});

執行您的應用程式

將程式代碼範例新增至應用程式之後,請執行程式:

  1. 流覽至您有 Document Intelligence 應用程式的資料夾(doc-intel-app)。

  2. 在您的終端機中輸入下列命令:

    node index.js
    

預先建置的模型輸出

以下是預期的輸出代碼段:

  Vendor Name: CONTOSO LTD.
  Customer Name: MICROSOFT CORPORATION
  Invoice Date: 2019-11-15T00:00:00.000Z
  Due Date: 2019-12-15T00:00:00.000Z
  Items:
  - <no product code>
    Description: Test for 23 fields
    Quantity: 1
    Date: undefined
    Unit: undefined
    Unit Price: 1
    Tax: undefined
    Amount: 100

若要檢視整個輸出,請造訪 GitHub 上的 Azure 範例存放庫,以檢視 預先建置的發票模型輸出

const { AzureKeyCredential, DocumentAnalysisClient } = require("@azure/ai-form-recognizer");

  // set `<your-key>` and `<your-endpoint>` variables with the values from the Azure portal.
      const key = "<your-key>";
      const endpoint = "<your-endpoint>";
// sample document
    invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"

async function main() {
    const client = new DocumentAnalysisClient(endpoint, new AzureKeyCredential(key));

    const poller = await client.beginAnalyzeDocument("prebuilt-invoice", invoiceUrl);

    const {
    documents: [document],
  } = await poller.pollUntilDone();


  if (document) {
    const {
      vendorName,
      customerName,
      invoiceDate,
      dueDate,
      items,
      subTotal,
      previousUnpaidBalance,
      totalTax,
      amountDue,
    } = document.fields;

    // The invoice model has many fields. For details, *see* [Invoice model field extraction](../../concept-invoice.md#field-extraction)
    console.log("Vendor Name:", vendorName && vendorName.value);
    console.log("Customer Name:", customerName && customerName.value);
    console.log("Invoice Date:", invoiceDate && invoiceDate.value);
    console.log("Due Date:", dueDate && dueDate.value);

    console.log("Items:");
    for (const item of (items && items.values) || []) {
      const { productCode, description, quantity, date, unit, unitPrice, tax, amount } =
        item.properties;

      console.log("-", (productCode && productCode.value) || "<no product code>");
      console.log("  Description:", description && description.value);
      console.log("  Quantity:", quantity && quantity.value);
      console.log("  Date:", date && date.value);
      console.log("  Unit:", unit && unit.value);
      console.log("  Unit Price:", unitPrice && unitPrice.value);
      console.log("  Tax:", tax && tax.value);
      console.log("  Amount:", amount && amount.value);
    }

    console.log("Subtotal:", subTotal && subTotal.value);
    console.log("Previous Unpaid Balance:", previousUnpaidBalance && previousUnpaidBalance.value);
    console.log("Tax:", totalTax && totalTax.value);
    console.log("Amount Due:", amountDue && amountDue.value);
  } else {
    throw new Error("Expected at least one receipt in the result.");
  }
}


main().catch((error) => {
    console.error("An error occurred:", error);
    process.exit(1);
});

執行您的應用程式

將程式代碼範例新增至應用程式之後,請執行程式:

  1. 流覽至您有 Document Intelligence 應用程式的資料夾(doc-intel-app)。

  2. 在您的終端機中輸入下列命令:

    node index.js
    

在本快速入門中,使用下列功能來分析及擷取表單和文件中的數據:

  • 版面配置:分析及擷取數據表、線條、單字和選取標記,例如單選按鈕和複選框,以及索引鍵/值組,而不需要定型模型。

  • 預先建置的發票 — 使用預先定型的模型,分析及擷取特定文件類型的一般欄位。

必要條件

提示

如果您打算在單一端點/金鑰下存取多個 Azure AI 服務,請建立 Azure AI 服務資源。 若為僅限文件智慧服務存取,請建立文件智慧服務資源。 請注意,如果您想要使用 Microsoft Entra 驗證,您將需要單一服務資源。

  • 部署資源之後,選取 [移至資源]。 您需要使用已建立資源的金鑰和端點,將應用程式連線至文件智慧服務 API。 您稍後會在快速入門中將金鑰和端點貼上至程式碼中:

    Azure 入口網站 中金鑰和端點位置的螢幕快照。

設定

在本機環境中開啟終端機視窗,並使用 pip 安裝適用於 Python 的 Azure AI 文件智慧服務用戶端程式庫:

pip install azure-ai-documentintelligence==1.0.0b2

pip install azure-ai-formrecognizer==3.3.0

pip install azure-ai-formrecognizer==3.2.0b6

建立 Python 應用程式

若要與此文件智慧服務互動,您必須建立 DocumentIntelligenceClient 類別的執行個體。 若要這樣做,請使用 key 從 Azure 入口網站建立 AzureKeyCredential,並使用 AzureKeyCredential 和文件智慧服務 endpoint 來建立 DocumentIntelligenceClient 執行個體。

  1. 在慣用的編輯器或 IDE 中建立名為 doc_intel_quickstart.py 的新 Python 檔案。

  2. 開啟doc_intel_quickstart.py檔案,然後選取下列其中一個程式代碼範例,以複製並貼到您的應用程式:

若要與此文件智慧服務互動,您必須建立 DocumentAnalysisClient 類別的執行個體。 若要這樣做,請使用 key 從 Azure 入口網站建立 AzureKeyCredential,並使用 AzureKeyCredential 和文件智慧服務 endpoint 來建立 DocumentAnalysisClient 執行個體。

  1. 在慣用的編輯器或 IDE 中建立名為 form_recognizer_quickstart.py 的新 Python 檔案。

  2. 開啟form_recognizer_quickstart.py檔案,然後選取下列其中一個程式代碼範例,以複製並貼到您的應用程式:

重要

完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性

版面配置模型

從檔擷取文字、選取標記、文字樣式、表格結構和周框區域座標。

  • 在此範例中,您需要 來自 URL 的檔案檔案。 您可以使用本快速入門的 範例檔
  • 我們已將檔案 URL 值新增至 formUrl 函式中的 analyze_layout 變數。
  • 若要在 URL 分析指定的檔案,您將使用 begin_analyze_document_from_url 方法並傳入 prebuilt-layout 作為模型識別碼。傳回的值是物件, result 其中包含所提交文件的相關數據。

將下列程式代碼範例新增至doc_intel_quickstart.py應用程式。 請確定您已使用 Azure 入口網站 Document Intelligence 實例中的值來更新金鑰和端點變數:


# import libraries
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeResult

# set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
endpoint = "<your-endpoint>"
key = "<your-key>"

# helper functions

def get_words(page, line):
    result = []
    for word in page.words:
        if _in_span(word, line.spans):
            result.append(word)
    return result


def _in_span(word, spans):
    for span in spans:
        if word.span.offset >= span.offset and (
            word.span.offset + word.span.length
        ) <= (span.offset + span.length):
            return True
    return False


def analyze_layout():
    # sample document
    formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"

    document_intelligence_client = DocumentIntelligenceClient(
        endpoint=endpoint, credential=AzureKeyCredential(key)
    )

    poller = document_intelligence_client.begin_analyze_document(
        "prebuilt-layout", formUrl
    )

    analyze_request = AnalyzeDocumentRequest(
        url_source=formUrl
    )

    result: AnalyzeResult = poller.result()

    if result.styles and any([style.is_handwritten for style in result.styles]):
        print("Document contains handwritten content")
    else:
        print("Document does not contain handwritten content")

    for page in result.pages:
        print(f"----Analyzing layout from page #{page.page_number}----")
        print(
            f"Page has width: {page.width} and height: {page.height}, measured with unit: {page.unit}"
        )

        if page.lines:
            for line_idx, line in enumerate(page.lines):
                words = get_words(page, line)
                print(
                    f"...Line # {line_idx} has word count {len(words)} and text '{line.content}' "
                    f"within bounding polygon '{line.polygon}'"
                )

                for word in words:
                    print(
                        f"......Word '{word.content}' has a confidence of {word.confidence}"
                    )

        if page.selection_marks:
            for selection_mark in page.selection_marks:
                print(
                    f"Selection mark is '{selection_mark.state}' within bounding polygon "
                    f"'{selection_mark.polygon}' and has a confidence of {selection_mark.confidence}"
                )

    if result.tables:
        for table_idx, table in enumerate(result.tables):
            print(
                f"Table # {table_idx} has {table.row_count} rows and "
                f"{table.column_count} columns"
            )
            if table.bounding_regions:
                for region in table.bounding_regions:
                    print(
                        f"Table # {table_idx} location on page: {region.page_number} is {region.polygon}"
                    )
            for cell in table.cells:
                print(
                    f"...Cell[{cell.row_index}][{cell.column_index}] has text '{cell.content}'"
                )
                if cell.bounding_regions:
                    for region in cell.bounding_regions:
                        print(
                            f"...content on page {region.page_number} is within bounding polygon '{region.polygon}'"
                        )

    print("----------------------------------------")


if __name__ == "__main__":
    analyze_layout()

執行應用程式

將程式代碼範例新增至應用程式之後,請建置並執行程式:

  1. 流覽至您 擁有doc_intel_quickstart.py 檔案的資料夾。

  2. 在您的終端機中輸入下列命令:

    python doc_intel_quickstart.py
    

將下列程式代碼範例新增至form_recognizer_quickstart.py應用程式。 請務必使用來自 Azure 入口網站 表格辨識器 實體的值來更新金鑰和端點變數:


# import libraries
import os
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential

# set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
endpoint = "<your-endpoint>"
key = "<your-key>"

def format_polygon(polygon):
    if not polygon:
        return "N/A"
    return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon])

def analyze_layout():
    # sample document
    formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"

    document_analysis_client = DocumentAnalysisClient(
        endpoint=endpoint, credential=AzureKeyCredential(key)
    )

    poller = document_analysis_client.begin_analyze_document(
            "prebuilt-layout", formUrl)
    result = poller.result()

    for idx, style in enumerate(result.styles):
        print(
            "Document contains {} content".format(
                "handwritten" if style.is_handwritten else "no handwritten"
            )
        )

    for page in result.pages:
        print("----Analyzing layout from page #{}----".format(page.page_number))
        print(
            "Page has width: {} and height: {}, measured with unit: {}".format(
                page.width, page.height, page.unit
            )
        )

        for line_idx, line in enumerate(page.lines):
            words = line.get_words()
            print(
                "...Line # {} has word count {} and text '{}' within bounding box '{}'".format(
                    line_idx,
                    len(words),
                    line.content,
                    format_polygon(line.polygon),
                )
            )

            for word in words:
                print(
                    "......Word '{}' has a confidence of {}".format(
                        word.content, word.confidence
                    )
                )

        for selection_mark in page.selection_marks:
            print(
                "...Selection mark is '{}' within bounding box '{}' and has a confidence of {}".format(
                    selection_mark.state,
                    format_polygon(selection_mark.polygon),
                    selection_mark.confidence,
                )
            )

    for table_idx, table in enumerate(result.tables):
        print(
            "Table # {} has {} rows and {} columns".format(
                table_idx, table.row_count, table.column_count
            )
        )
        for region in table.bounding_regions:
            print(
                "Table # {} location on page: {} is {}".format(
                    table_idx,
                    region.page_number,
                    format_polygon(region.polygon),
                )
            )
        for cell in table.cells:
            print(
                "...Cell[{}][{}] has content '{}'".format(
                    cell.row_index,
                    cell.column_index,
                    cell.content,
                )
            )
            for region in cell.bounding_regions:
                print(
                    "...content on page {} is within bounding box '{}'".format(
                        region.page_number,
                        format_polygon(region.polygon),
                    )
                )

    print("----------------------------------------")


if __name__ == "__main__":
    analyze_layout()

執行應用程式

將程式代碼範例新增至應用程式之後,請建置並執行程式:

  1. 流覽至您 擁有form_recognizer_quickstart.py 檔案的資料夾。

  2. 在您的終端機中輸入下列命令:

    python form_recognizer_quickstart.py
    

版面配置模型輸出

以下是預期的輸出代碼段:

  ----Analyzing layout from page #1----
  Page has width: 8.5 and height: 11.0, measured with unit: inch
  ...Line # 0 has word count 2 and text 'UNITED STATES' within bounding box '[3.4915, 0.6828], [5.0116, 0.6828], [5.0116, 0.8265], [3.4915, 0.8265]'
  ......Word 'UNITED' has a confidence of 1.0
  ......Word 'STATES' has a confidence of 1.0
  ...Line # 1 has word count 4 and text 'SECURITIES AND EXCHANGE COMMISSION' within bounding box '[2.1937, 0.9061], [6.297, 0.9061], [6.297, 1.0498], [2.1937, 1.0498]'
  ......Word 'SECURITIES' has a confidence of 1.0
  ......Word 'AND' has a confidence of 1.0
  ......Word 'EXCHANGE' has a confidence of 1.0
  ......Word 'COMMISSION' has a confidence of 1.0
  ...Line # 2 has word count 3 and text 'Washington, D.C. 20549' within bounding box '[3.4629, 1.1179], [5.031, 1.1179], [5.031, 1.2483], [3.4629, 1.2483]'
  ......Word 'Washington,' has a confidence of 1.0
  ......Word 'D.C.' has a confidence of 1.0

若要檢視整個輸出,請造訪 GitHub 上的 Azure 範例存放庫,以檢視 版面配置模型輸出

將下列程式代碼範例新增至form_recognizer_quickstart.py應用程式。 請務必使用來自 Azure 入口網站 表格辨識器 實體的值來更新金鑰和端點變數:


# import libraries
import os
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential

# set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
endpoint = "<your-endpoint>"
key = "<your-key>"


def analyze_layout():
    # sample document
    formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"

    document_analysis_client = DocumentAnalysisClient(
        endpoint=endpoint, credential=AzureKeyCredential(key)
    )

    poller = document_analysis_client.begin_analyze_document_from_url(
        "prebuilt-layout", formUrl
    )
    result = poller.result()

    for idx, style in enumerate(result.styles):
        print(
            "Document contains {} content".format(
                "handwritten" if style.is_handwritten else "no handwritten"
            )
        )

    for page in result.pages:
        print("----Analyzing layout from page #{}----".format(page.page_number))
        print(
            "Page has width: {} and height: {}, measured with unit: {}".format(
                page.width, page.height, page.unit
            )
        )

        for line_idx, line in enumerate(page.lines):
            words = line.get_words()
            print(
                "...Line # {} has word count {} and text '{}' within bounding polygon '{}'".format(
                    line_idx,
                    len(words),
                    line.content,
                    format_polygon(line.polygon),
                )
            )

            for word in words:
                print(
                    "......Word '{}' has a confidence of {}".format(
                        word.content, word.confidence
                    )
                )

        for selection_mark in page.selection_marks:
            print(
                "...Selection mark is '{}' within bounding polygon '{}' and has a confidence of {}".format(
                    selection_mark.state,
                    format_polygon(selection_mark.polygon),
                    selection_mark.confidence,
                )
            )

    for table_idx, table in enumerate(result.tables):
        print(
            "Table # {} has {} rows and {} columns".format(
                table_idx, table.row_count, table.column_count
            )
        )
        for region in table.bounding_regions:
            print(
                "Table # {} location on page: {} is {}".format(
                    table_idx,
                    region.page_number,
                    format_polygon(region.polygon),
                )
            )
        for cell in table.cells:
            print(
                "...Cell[{}][{}] has content '{}'".format(
                    cell.row_index,
                    cell.column_index,
                    cell.content,
                )
            )
            for region in cell.bounding_regions:
                print(
                    "...content on page {} is within bounding polygon '{}'".format(
                        region.page_number,
                        format_polygon(region.polygon),
                    )
                )

    print("----------------------------------------")


if __name__ == "__main__":
    analyze_layout()


執行應用程式

將程式代碼範例新增至應用程式之後,請建置並執行程式:

  1. 流覽至您 擁有form_recognizer_quickstart.py 檔案的資料夾。

  2. 在您的終端機中輸入下列命令:

    python form_recognizer_quickstart.py
    

預先建置的模型

使用預先建置的模型,從特定文件類型分析及擷取一般欄位。 在此範例中,我們會使用 預先建置的發票模型來分析發票

提示

您不限於發票,有數個預先建置的模型可供選擇,每個模型都有自己的支援欄位集。 要用於作業的 analyze 模型取決於要分析的文件類型。 請參閱模型數據擷取

  • 使用預建發票模型分析發票。 您可以針對本快速入門使用我們的 範例發票檔
  • 我們已將檔案 URL 值新增至 invoiceUrl 檔案頂端的 變數。
  • 若要在 URI 分析指定的檔案,您將使用 begin_analyze_document_from_url 方法,並傳遞 prebuilt-invoice 作為模型識別碼。傳回的值是物件, result 其中包含所提交文件的相關數據。
  • 為了簡單起見,服務傳回的所有索引鍵/值組不會在此顯示。 若要查看所有支援的欄位和對應類型清單,請參閱我們的 發票 概念頁面。

將下列程式代碼範例新增至doc_intel_quickstart.py應用程式。 請確定您已使用 Azure 入口網站 Document Intelligence 實例中的值來更新金鑰和端點變數:


# import libraries
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient


# set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
endpoint = "<your-endpoint>"
key = "<your-key>"

def analyze_invoice():
    # sample document

    invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"

    document_intelligence_client = DocumentIntelligenceClient(
        endpoint=endpoint, credential=AzureKeyCredential(key)
    )

    poller = document_intelligence_client.begin_analyze_document(
        "prebuilt-invoice", invoiceUrl
    )
    invoices = poller.result()

    if invoices.documents:
        for idx, invoice in enumerate(invoices.documents):
            print(f"--------Analyzing invoice #{idx + 1}--------")
            vendor_name = invoice.fields.get("VendorName")
            if vendor_name:
                print(
                    f"Vendor Name: {vendor_name.get('content')} has confidence: {vendor_name.get('confidence')}"
                )
            vendor_address = invoice.fields.get("VendorAddress")
            if vendor_address:
                print(
                    f"Vendor Address: {vendor_address.get('content')} has confidence: {vendor_address.get('confidence')}"
                )
            vendor_address_recipient = invoice.fields.get("VendorAddressRecipient")
            if vendor_address_recipient:
                print(
                    f"Vendor Address Recipient: {vendor_address_recipient.get('content')} has confidence: {vendor_address_recipient.get('confidence')}"
                )
            customer_name = invoice.fields.get("CustomerName")
            if customer_name:
                print(
                    f"Customer Name: {customer_name.get('content')} has confidence: {customer_name.get('confidence')}"
                )
            customer_id = invoice.fields.get("CustomerId")
            if customer_id:
                print(
                    f"Customer Id: {customer_id.get('content')} has confidence: {customer_id.get('confidence')}"
                )
            customer_address = invoice.fields.get("CustomerAddress")
            if customer_address:
                print(
                    f"Customer Address: {customer_address.get('content')} has confidence: {customer_address.get('confidence')}"
                )
            customer_address_recipient = invoice.fields.get("CustomerAddressRecipient")
            if customer_address_recipient:
                print(
                    f"Customer Address Recipient: {customer_address_recipient.get('content')} has confidence: {customer_address_recipient.get('confidence')}"
                )
            invoice_id = invoice.fields.get("InvoiceId")
            if invoice_id:
                print(
                    f"Invoice Id: {invoice_id.get('content')} has confidence: {invoice_id.get('confidence')}"
                )
            invoice_date = invoice.fields.get("InvoiceDate")
            if invoice_date:
                print(
                    f"Invoice Date: {invoice_date.get('content')} has confidence: {invoice_date.get('confidence')}"
                )
            invoice_total = invoice.fields.get("InvoiceTotal")
            if invoice_total:
                print(
                    f"Invoice Total: {invoice_total.get('content')} has confidence: {invoice_total.get('confidence')}"
                )
            due_date = invoice.fields.get("DueDate")
            if due_date:
                print(
                    f"Due Date: {due_date.get('content')} has confidence: {due_date.get('confidence')}"
                )
            purchase_order = invoice.fields.get("PurchaseOrder")
            if purchase_order:
                print(
                    f"Purchase Order: {purchase_order.get('content')} has confidence: {purchase_order.get('confidence')}"
                )
            billing_address = invoice.fields.get("BillingAddress")
            if billing_address:
                print(
                    f"Billing Address: {billing_address.get('content')} has confidence: {billing_address.get('confidence')}"
                )
            billing_address_recipient = invoice.fields.get("BillingAddressRecipient")
            if billing_address_recipient:
                print(
                    f"Billing Address Recipient: {billing_address_recipient.get('content')} has confidence: {billing_address_recipient.get('confidence')}"
                )
            shipping_address = invoice.fields.get("ShippingAddress")
            if shipping_address:
                print(
                    f"Shipping Address: {shipping_address.get('content')} has confidence: {shipping_address.get('confidence')}"
                )
            shipping_address_recipient = invoice.fields.get("ShippingAddressRecipient")
            if shipping_address_recipient:
                print(
                    f"Shipping Address Recipient: {shipping_address_recipient.get('content')} has confidence: {shipping_address_recipient.get('confidence')}"
                )
            print("Invoice items:")
            for idx, item in enumerate(invoice.fields.get("Items").get("valueArray")):
                print(f"...Item #{idx + 1}")
                item_description = item.get("valueObject").get("Description")
                if item_description:
                    print(
                        f"......Description: {item_description.get('content')} has confidence: {item_description.get('confidence')}"
                    )
                item_quantity = item.get("valueObject").get("Quantity")
                if item_quantity:
                    print(
                        f"......Quantity: {item_quantity.get('content')} has confidence: {item_quantity.get('confidence')}"
                    )
                unit = item.get("valueObject").get("Unit")
                if unit:
                    print(
                        f"......Unit: {unit.get('content')} has confidence: {unit.get('confidence')}"
                    )
                unit_price = item.get("valueObject").get("UnitPrice")
                if unit_price:
                    unit_price_code = (
                        unit_price.get("valueCurrency").get("currencyCode")
                        if unit_price.get("valueCurrency").get("currencyCode")
                        else ""
                    )
                    print(
                        f"......Unit Price: {unit_price.get('content')}{unit_price_code} has confidence: {unit_price.get('confidence')}"
                    )
                product_code = item.get("valueObject").get("ProductCode")
                if product_code:
                    print(
                        f"......Product Code: {product_code.get('content')} has confidence: {product_code.get('confidence')}"
                    )
                item_date = item.get("valueObject").get("Date")
                if item_date:
                    print(
                        f"......Date: {item_date.get('content')} has confidence: {item_date.get('confidence')}"
                    )
                tax = item.get("valueObject").get("Tax")
                if tax:
                    print(
                        f"......Tax: {tax.get('content')} has confidence: {tax.get('confidence')}"
                    )
                amount = item.get("valueObject").get("Amount")
                if amount:
                    print(
                        f"......Amount: {amount.get('content')} has confidence: {amount.get('confidence')}"
                    )
            subtotal = invoice.fields.get("SubTotal")
            if subtotal:
                print(
                    f"Subtotal: {subtotal.get('content')} has confidence: {subtotal.get('confidence')}"
                )
            total_tax = invoice.fields.get("TotalTax")
            if total_tax:
                print(
                    f"Total Tax: {total_tax.get('content')} has confidence: {total_tax.get('confidence')}"
                )
            previous_unpaid_balance = invoice.fields.get("PreviousUnpaidBalance")
            if previous_unpaid_balance:
                print(
                    f"Previous Unpaid Balance: {previous_unpaid_balance.get('content')} has confidence: {previous_unpaid_balance.get('confidence')}"
                )
            amount_due = invoice.fields.get("AmountDue")
            if amount_due:
                print(
                    f"Amount Due: {amount_due.get('content')} has confidence: {amount_due.get('confidence')}"
                )
            service_start_date = invoice.fields.get("ServiceStartDate")
            if service_start_date:
                print(
                    f"Service Start Date: {service_start_date.get('content')} has confidence: {service_start_date.get('confidence')}"
                )
            service_end_date = invoice.fields.get("ServiceEndDate")
            if service_end_date:
                print(
                    f"Service End Date: {service_end_date.get('content')} has confidence: {service_end_date.get('confidence')}"
                )
            service_address = invoice.fields.get("ServiceAddress")
            if service_address:
                print(
                    f"Service Address: {service_address.get('content')} has confidence: {service_address.get('confidence')}"
                )
            service_address_recipient = invoice.fields.get("ServiceAddressRecipient")
            if service_address_recipient:
                print(
                    f"Service Address Recipient: {service_address_recipient.get('content')} has confidence: {service_address_recipient.get('confidence')}"
                )
            remittance_address = invoice.fields.get("RemittanceAddress")
            if remittance_address:
                print(
                    f"Remittance Address: {remittance_address.get('content')} has confidence: {remittance_address.get('confidence')}"
                )
            remittance_address_recipient = invoice.fields.get(
                "RemittanceAddressRecipient"
            )
            if remittance_address_recipient:
                print(
                    f"Remittance Address Recipient: {remittance_address_recipient.get('content')} has confidence: {remittance_address_recipient.get('confidence')}"
                )


          print("----------------------------------------")


if __name__ == "__main__":
    analyze_invoice()


執行應用程式

將程式代碼範例新增至應用程式之後,請建置並執行程式:

  1. 流覽至您 擁有doc_intel_quickstart.py 檔案的資料夾。

  2. 在您的終端機中輸入下列命令:

    python doc_intel_quickstart.py
    

將下列程式代碼範例新增至form_recognizer_quickstart.py應用程式。 請務必使用來自 Azure 入口網站 表格辨識器 實體的值來更新金鑰和端點變數:

# import libraries
import os
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential

# set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
endpoint = "<your-endpoint>"
key = "<your-key>"


def format_bounding_region(bounding_regions):
    if not bounding_regions:
        return "N/A"
    return ", ".join(
        "Page #{}: {}".format(region.page_number, format_polygon(region.polygon))
        for region in bounding_regions
    )


def format_polygon(polygon):
    if not polygon:
        return "N/A"
    return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon])


def analyze_invoice():

    invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"

    document_analysis_client = DocumentAnalysisClient(
        endpoint=endpoint, credential=AzureKeyCredential(key)
    )

    poller = document_analysis_client.begin_analyze_document_from_url(
        "prebuilt-invoice", invoiceUrl
    )
    invoices = poller.result()

    for idx, invoice in enumerate(invoices.documents):
        print("--------Recognizing invoice #{}--------".format(idx + 1))
        vendor_name = invoice.fields.get("VendorName")
        if vendor_name:
            print(
                "Vendor Name: {} has confidence: {}".format(
                    vendor_name.value, vendor_name.confidence
                )
            )
        vendor_address = invoice.fields.get("VendorAddress")
        if vendor_address:
            print(
                "Vendor Address: {} has confidence: {}".format(
                    vendor_address.value, vendor_address.confidence
                )
            )
        vendor_address_recipient = invoice.fields.get("VendorAddressRecipient")
        if vendor_address_recipient:
            print(
                "Vendor Address Recipient: {} has confidence: {}".format(
                    vendor_address_recipient.value, vendor_address_recipient.confidence
                )
            )
        customer_name = invoice.fields.get("CustomerName")
        if customer_name:
            print(
                "Customer Name: {} has confidence: {}".format(
                    customer_name.value, customer_name.confidence
                )
            )
        customer_id = invoice.fields.get("CustomerId")
        if customer_id:
            print(
                "Customer Id: {} has confidence: {}".format(
                    customer_id.value, customer_id.confidence
                )
            )
        customer_address = invoice.fields.get("CustomerAddress")
        if customer_address:
            print(
                "Customer Address: {} has confidence: {}".format(
                    customer_address.value, customer_address.confidence
                )
            )
        customer_address_recipient = invoice.fields.get("CustomerAddressRecipient")
        if customer_address_recipient:
            print(
                "Customer Address Recipient: {} has confidence: {}".format(
                    customer_address_recipient.value,
                    customer_address_recipient.confidence,
                )
            )
        invoice_id = invoice.fields.get("InvoiceId")
        if invoice_id:
            print(
                "Invoice Id: {} has confidence: {}".format(
                    invoice_id.value, invoice_id.confidence
                )
            )
        invoice_date = invoice.fields.get("InvoiceDate")
        if invoice_date:
            print(
                "Invoice Date: {} has confidence: {}".format(
                    invoice_date.value, invoice_date.confidence
                )
            )
        invoice_total = invoice.fields.get("InvoiceTotal")
        if invoice_total:
            print(
                "Invoice Total: {} has confidence: {}".format(
                    invoice_total.value, invoice_total.confidence
                )
            )
        due_date = invoice.fields.get("DueDate")
        if due_date:
            print(
                "Due Date: {} has confidence: {}".format(
                    due_date.value, due_date.confidence
                )
            )
        purchase_order = invoice.fields.get("PurchaseOrder")
        if purchase_order:
            print(
                "Purchase Order: {} has confidence: {}".format(
                    purchase_order.value, purchase_order.confidence
                )
            )
        billing_address = invoice.fields.get("BillingAddress")
        if billing_address:
            print(
                "Billing Address: {} has confidence: {}".format(
                    billing_address.value, billing_address.confidence
                )
            )
        billing_address_recipient = invoice.fields.get("BillingAddressRecipient")
        if billing_address_recipient:
            print(
                "Billing Address Recipient: {} has confidence: {}".format(
                    billing_address_recipient.value,
                    billing_address_recipient.confidence,
                )
            )
        shipping_address = invoice.fields.get("ShippingAddress")
        if shipping_address:
            print(
                "Shipping Address: {} has confidence: {}".format(
                    shipping_address.value, shipping_address.confidence
                )
            )
        shipping_address_recipient = invoice.fields.get("ShippingAddressRecipient")
        if shipping_address_recipient:
            print(
                "Shipping Address Recipient: {} has confidence: {}".format(
                    shipping_address_recipient.value,
                    shipping_address_recipient.confidence,
                )
            )
        print("Invoice items:")
        for idx, item in enumerate(invoice.fields.get("Items").value):
            print("...Item #{}".format(idx + 1))
            item_description = item.value.get("Description")
            if item_description:
                print(
                    "......Description: {} has confidence: {}".format(
                        item_description.value, item_description.confidence
                    )
                )
            item_quantity = item.value.get("Quantity")
            if item_quantity:
                print(
                    "......Quantity: {} has confidence: {}".format(
                        item_quantity.value, item_quantity.confidence
                    )
                )
            unit = item.value.get("Unit")
            if unit:
                print(
                    "......Unit: {} has confidence: {}".format(
                        unit.value, unit.confidence
                    )
                )
            unit_price = item.value.get("UnitPrice")
            if unit_price:
                print(
                    "......Unit Price: {} has confidence: {}".format(
                        unit_price.value, unit_price.confidence
                    )
                )
            product_code = item.value.get("ProductCode")
            if product_code:
                print(
                    "......Product Code: {} has confidence: {}".format(
                        product_code.value, product_code.confidence
                    )
                )
            item_date = item.value.get("Date")
            if item_date:
                print(
                    "......Date: {} has confidence: {}".format(
                        item_date.value, item_date.confidence
                    )
                )
            tax = item.value.get("Tax")
            if tax:
                print(
                    "......Tax: {} has confidence: {}".format(tax.value, tax.confidence)
                )
            amount = item.value.get("Amount")
            if amount:
                print(
                    "......Amount: {} has confidence: {}".format(
                        amount.value, amount.confidence
                    )
                )
        subtotal = invoice.fields.get("SubTotal")
        if subtotal:
            print(
                "Subtotal: {} has confidence: {}".format(
                    subtotal.value, subtotal.confidence
                )
            )
        total_tax = invoice.fields.get("TotalTax")
        if total_tax:
            print(
                "Total Tax: {} has confidence: {}".format(
                    total_tax.value, total_tax.confidence
                )
            )
        previous_unpaid_balance = invoice.fields.get("PreviousUnpaidBalance")
        if previous_unpaid_balance:
            print(
                "Previous Unpaid Balance: {} has confidence: {}".format(
                    previous_unpaid_balance.value, previous_unpaid_balance.confidence
                )
            )
        amount_due = invoice.fields.get("AmountDue")
        if amount_due:
            print(
                "Amount Due: {} has confidence: {}".format(
                    amount_due.value, amount_due.confidence
                )
            )
        service_start_date = invoice.fields.get("ServiceStartDate")
        if service_start_date:
            print(
                "Service Start Date: {} has confidence: {}".format(
                    service_start_date.value, service_start_date.confidence
                )
            )
        service_end_date = invoice.fields.get("ServiceEndDate")
        if service_end_date:
            print(
                "Service End Date: {} has confidence: {}".format(
                    service_end_date.value, service_end_date.confidence
                )
            )
        service_address = invoice.fields.get("ServiceAddress")
        if service_address:
            print(
                "Service Address: {} has confidence: {}".format(
                    service_address.value, service_address.confidence
                )
            )
        service_address_recipient = invoice.fields.get("ServiceAddressRecipient")
        if service_address_recipient:
            print(
                "Service Address Recipient: {} has confidence: {}".format(
                    service_address_recipient.value,
                    service_address_recipient.confidence,
                )
            )
        remittance_address = invoice.fields.get("RemittanceAddress")
        if remittance_address:
            print(
                "Remittance Address: {} has confidence: {}".format(
                    remittance_address.value, remittance_address.confidence
                )
            )
        remittance_address_recipient = invoice.fields.get("RemittanceAddressRecipient")
        if remittance_address_recipient:
            print(
                "Remittance Address Recipient: {} has confidence: {}".format(
                    remittance_address_recipient.value,
                    remittance_address_recipient.confidence,
                )
            )

        print("----------------------------------------")

if __name__ == "__main__":
    analyze_invoice()


執行應用程式

將程式代碼範例新增至應用程式之後,請建置並執行程式:

  1. 流覽至您 擁有form_recognizer_quickstart.py 檔案的資料夾。

  2. 在您的終端機中輸入下列命令:

    python form_recognizer_quickstart.py
    

預先建置的模型輸出

以下是預期的輸出代碼段:

  --------Recognizing invoice #1--------
  Vendor Name: CONTOSO LTD. has confidence: 0.919
  Vendor Address: 123 456th St New York, NY, 10001 has confidence: 0.907
  Vendor Address Recipient: Contoso Headquarters has confidence: 0.919
  Customer Name: MICROSOFT CORPORATION has confidence: 0.84
  Customer Id: CID-12345 has confidence: 0.956
  Customer Address: 123 Other St, Redmond WA, 98052 has confidence: 0.909
  Customer Address Recipient: Microsoft Corp has confidence: 0.917
  Invoice Id: INV-100 has confidence: 0.972
  Invoice Date: 2019-11-15 has confidence: 0.971
  Invoice Total: CurrencyValue(amount=110.0, symbol=$) has confidence: 0.97
  Due Date: 2019-12-15 has confidence: 0.973

若要檢視整個輸出,請造訪 GitHub 上的 Azure 範例存放庫,以檢視 預先建置的發票模型輸出

將下列程式代碼範例新增至form_recognizer_quickstart.py應用程式。 請務必使用來自 Azure 入口網站 表格辨識器 實體的值來更新金鑰和端點變數:


# import libraries
import os
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential

# set `<your-endpoint>` and `<your-key>` variables with the values from the Azure portal
endpoint = "<your-endpoint>"
key = "<your-key>"


def format_polygon(polygon):
    if not polygon:
        return "N/A"
    return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon])


def analyze_layout():
    # sample document
    formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"

    document_analysis_client = DocumentAnalysisClient(
        endpoint=endpoint, credential=AzureKeyCredential(key)
    )

    poller = document_analysis_client.begin_analyze_document_from_url(
        "prebuilt-layout", formUrl
    )
    result = poller.result()

    for idx, style in enumerate(result.styles):
        print(
            "Document contains {} content".format(
                "handwritten" if style.is_handwritten else "no handwritten"
            )
        )


for page in result.pages:
    print("----Analyzing layout from page #{}----".format(page.page_number))
    print(
        "Page has width: {} and height: {}, measured with unit: {}".format(
            page.width, page.height, page.unit
        )
    )

    for line_idx, line in enumerate(page.lines):
        words = line.get_words()
        print(
            "...Line # {} has word count {} and text '{}' within bounding polygon '{}'".format(
                line_idx,
                len(words),
                line.content,
                format_polygon(line.polygon),
            )
        )

        for word in words:
            print(
                "......Word '{}' has a confidence of {}".format(
                    word.content, word.confidence
                )
            )

    for selection_mark in page.selection_marks:
        print(
            "...Selection mark is '{}' within bounding polygon '{}' and has a confidence of {}".format(
                selection_mark.state,
                format_polygon(selection_mark.polygon),
                selection_mark.confidence,
            )
        )

for table_idx, table in enumerate(result.tables):
    print(
        "Table # {} has {} rows and {} columns".format(
            table_idx, table.row_count, table.column_count
        )
    )
    for region in table.bounding_regions:
        print(
            "Table # {} location on page: {} is {}".format(
                table_idx,
                region.page_number,
                format_polygon(region.polygon),
            )
        )
    for cell in table.cells:
        print(
            "...Cell[{}][{}] has content '{}'".format(
                cell.row_index,
                cell.column_index,
                cell.content,
            )
        )
        for region in cell.bounding_regions:
            print(
                "...content on page {} is within bounding polygon '{}'".format(
                    region.page_number,
                    format_polygon(region.polygon),
                )
            )

print("----------------------------------------")


if __name__ == "__main__":
    analyze_layout()


執行應用程式

將程式代碼範例新增至應用程式之後,請建置並執行程式:

  1. 流覽至您 擁有form_recognizer_quickstart.py 檔案的資料夾。

  2. 在您的終端機中輸入下列命令:

    python form_recognizer_quickstart.py
    

在本快速入門中,瞭解如何使用檔智慧 REST API 來分析及擷取檔中的數據和值:

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶

  • 已安裝 curl 命令行工具。

  • PowerShell 版本 7.*+ (或類似的命令行應用程式。):

  • 若要檢查 PowerShell 版本,請輸入下列相對於作業系統的命令:

    • Windows:Get-Host | Select-Object Version
    • macOS 或 Linux: $PSVersionTable
  • 文件智慧服務 (單一服務) 或 Azure AI 服務 (多重服務) 資源。 擁有 Azure 訂閱後,請在 Azure 入口網站中建立單一服務多重服務文件智慧服務資源,以取得您的金鑰和端點。 您可以使用免費定價層 (F0) 來試用服務,之後可升級至付費層以用於實際執行環境。

提示

如果您打算在單一端點/金鑰下存取多個 Azure AI 服務,請建立 Azure AI 服務資源。 若為僅限文件智慧服務存取,請建立文件智慧服務資源。 請注意,如果您想要使用 Microsoft Entra 驗證,您將需要單一服務資源。

  • 部署資源之後,選取 [移至資源]。 您需要使用已建立資源的金鑰和端點,將應用程式連線至文件智慧服務 API。 您稍後會在快速入門中將金鑰和端點貼上至程式碼中:

    Azure 入口網站 中金鑰和端點位置的螢幕快照。

分析檔並取得結果

POST 要求可用來使用預先建置或自定義模型來分析檔。 GET 要求可用來擷取檔分析呼叫的結果。 modelId與 POST 和 resultId GET 作業搭配使用。

分析檔案 (POST 要求)

執行 cURL 命令之前,請先對後續要求進行下列變更

  1. 將 取代{endpoint}為您 Azure 入口網站 Document Intelligence 實例中的端點值。

  2. 將 取代{key}為您 Azure 入口網站 Document Intelligence 實例中的索引鍵值。

  3. 使用下表做為參考,將 和 {your-document-url} 取代{modelID}為您所需的值。

  4. 您需要文件檔案的 URL。 在本快速入門中,您可以使用下表中針對每項功能的範例表單:

範例檔

功能 {modelID} {your-document-url}
讀取 prebuilt-read https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/read.png
版面配置 prebuilt-layout https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/layout.png
健保卡 prebuilt-healthInsuranceCard.us https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/insurance-card.png
W-2 prebuilt-tax.us.w2 https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/w2.png
發票 預建發票 https://github.com/Azure-Samples/cognitive-services-REST-api-samples/raw/master/curl/form-recognizer/rest-api/invoice.pdf
收據 prebuilt-receipt https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/receipt.png
身分證明文件 prebuilt-idDocument https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/identity_documents.png

範例檔

功能 {modelID} {your-document-url}
一般檔 prebuilt-document https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf
讀取 prebuilt-read https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/read.png
版面配置 prebuilt-layout https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/layout.png
健保卡 prebuilt-healthInsuranceCard.us https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/insurance-card.png
W-2 prebuilt-tax.us.w2 https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/w2.png
發票 預建發票 https://github.com/Azure-Samples/cognitive-services-REST-api-samples/raw/master/curl/form-recognizer/rest-api/invoice.pdf
收據 prebuilt-receipt https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/receipt.png
身分證明文件 prebuilt-idDocument https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/rest-api/identity_documents.png
名片 prebuilt-businessCard https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/de5e0d8982ab754823c54de47a47e8e499351523/curl/form-recognizer/rest-api/business_card.jpg

重要

完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性

POST 要求

curl -v -i POST "{endpoint}/documentintelligence/documentModels/{modelId}:analyze?api-version=2024-02-29-preview" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: {key}" --data-ascii "{'urlSource': '{your-document-url}'}"
curl -v -i POST "{endpoint}/formrecognizer/documentModels/{modelID}:analyze?api-version=2023-07-31" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: {key}" --data-ascii "{'urlSource': '{your-document-url}'}"
curl -v -i POST "{endpoint}/formrecognizer/documentModels/{modelId}:analyze?api-version=2022-08-31" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: {key}" --data-ascii "{'urlSource': '{your-document-url}'}"

POST 回應 (resultID)

您收到 202 (Success) 回應,其中包含唯讀 Operation-Location 標頭。 此標頭的值包含 resultID,可透過查詢以取得非同步作業的狀態,並且可使用 GET 要求搭配您的相同資源訂用帳戶金鑰來擷取結果:

{alt-text}

取得分析結果 (GET 要求)

呼叫 Analyze document API 之後,請呼叫 取得分析結果 API,以取得作業的狀態和擷取的數據。 執行命令之前,請先進行下列變更:

呼叫 Analyze document API 之後,請呼叫 取得分析結果 API,以取得作業的狀態和擷取的數據。 執行命令之前,請先進行下列變更:

呼叫 Analyze document API 之後,請呼叫 取得分析結果 API,以取得作業的狀態和擷取的數據。 執行命令之前,請先進行下列變更:

  1. 從 POST 回應取代 {resultID} Operation-Location 標頭

  2. 將 取代{key}為 Azure 入口網站 中 Document Intelligence 實例的索引鍵值。

GET 要求

curl -v -X GET "{endpoint}/documentintelligence/documentModels/{modelId}/analyzeResults/{resultId}?api-version=2024-02-29-preview" -H "Ocp-Apim-Subscription-Key: {key}"
curl -v -X GET "{endpoint}/formrecognizer/documentModels/{modelId}/analyzeResults/{resultId}?api-version=2023-07-31" -H "Ocp-Apim-Subscription-Key: {key}"

curl -v -X GET "{endpoint}/formrecognizer/documentModels/{modelId}/analyzeResults/{resultId}?api-version=2022-08-31" -H "Ocp-Apim-Subscription-Key: {key}"

檢查回應

您收到 200 (Success) 回應及 JSON 輸出。 第一個字段 "status"表示作業的狀態。 如果作業未完成,而 "status" 的值是 "running""notStarted",此時您應該以手動方式或透過指令碼再次呼叫 API。 我們建議在呼叫之間間隔一秒以上。

預建發票的範例回應

{
    "status": "succeeded",
    "createdDateTime": "2024-03-25T19:31:37Z",
    "lastUpdatedDateTime": "2024-03-25T19:31:43Z",
    "analyzeResult": {
        "apiVersion": "2024-02-29-preview",
        "modelId": "prebuilt-invoice",
        "stringIndexType": "textElements"...
    ..."pages": [
            {
                "pageNumber": 1,
                "angle": 0,
                "width": 8.5,
                "height": 11,
                "unit": "inch",
                "words": [
                    {
                        "content": "CONTOSO",
                        "boundingBox": [
                            0.5911,
                            0.6857,
                            1.7451,
                            0.6857,
                            1.7451,
                            0.8664,
                            0.5911,
                            0.8664
                        ],
                        "confidence": 1,
                        "span": {
                            "offset": 0,
                            "length": 7
                                }
                      }],
              }]
      }
}
{
    "status": "succeeded",
    "createdDateTime": "2023-08-25T19:31:37Z",
    "lastUpdatedDateTime": "2023-08-25T19:31:43Z",
    "analyzeResult": {
        "apiVersion": "2023-07-31",
        "modelId": "prebuilt-invoice",
        "stringIndexType": "textElements"...
    ..."pages": [
            {
                "pageNumber": 1,
                "angle": 0,
                "width": 8.5,
                "height": 11,
                "unit": "inch",
                "words": [
                    {
                        "content": "CONTOSO",
                        "boundingBox": [
                            0.5911,
                            0.6857,
                            1.7451,
                            0.6857,
                            1.7451,
                            0.8664,
                            0.5911,
                            0.8664
                        ],
                        "confidence": 1,
                        "span": {
                            "offset": 0,
                            "length": 7
                                }
                      }],
              }]
      }
}
{
    "status": "succeeded",
    "createdDateTime": "2022-09-25T19:31:37Z",
    "lastUpdatedDateTime": "2022-09-25T19:31:43Z",
    "analyzeResult": {
        "apiVersion": "2022-08-31",
        "modelId": "prebuilt-invoice",
        "stringIndexType": "textElements"...
    ..."pages": [
            {
                "pageNumber": 1,
                "angle": 0,
                "width": 8.5,
                "height": 11,
                "unit": "inch",
                "words": [
                    {
                        "content": "CONTOSO",
                        "boundingBox": [
                            0.5911,
                            0.6857,
                            1.7451,
                            0.6857,
                            1.7451,
                            0.8664,
                            0.5911,
                            0.8664
                        ],
                        "confidence": 1,
                        "span": {
                            "offset": 0,
                            "length": 7
                                }
                      }],
              }]
      }
}

支援的檔欄位

預先建置的模型會擷取預先定義的檔欄位集。 如需擷取的功能變數名稱、類型、描述和範例,請參閱 模型數據擷取

就是這樣,恭喜!

在本快速入門中,您已使用文件智慧模型來分析各種表單和檔。 接下來,探索 Document Intelligence Studio 和參考檔,以深入瞭解檔智慧 API。

下一步

此內容適用於:複選標記v2.1 | 最新版本:blue-checkmarkv4.0(預覽)

使用您選擇的程式設計語言或 REST API 開始使用 Azure AI 檔案智慧。 Document Intelligence 是雲端式 Azure AI 服務,使用機器學習從您的檔中擷取索引鍵/值組、文字和數據表。 建議您在學習技術時使用免費服務。 請記住,免費頁面數目限制為每月 500 個。

若要深入瞭解檔智慧功能和開發選項,請瀏覽我們的 [概觀 ] 頁面。

參考文檔 | 庫原始程式碼 | 套件 (NuGet)範例 |

在本快速入門中,您會使用下列 API 從窗體和檔擷取結構化數據:

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶

  • Visual Studio IDE目前版本。

  • Azure AI 服務或文件智慧服務資源。 擁有 Azure 訂用帳戶之後,請在 Azure 入口網站 中建立單一服務多服務 Document Intelligence 資源,以取得您的密鑰和端點。 您可以使用免費定價層 (F0) 來試用服務,之後可升級至付費層以用於實際執行環境。

    提示

    如果您打算在單一端點/金鑰下存取多個 Azure AI 服務,請建立 Azure AI 服務資源。 若為僅限文件智慧服務存取,請建立文件智慧服務資源。 請注意,如果您想要使用 Microsoft Entra 驗證,您將需要單一服務資源。

  • 部署資源之後,選取 [移至資源]。 您需要使用已建立資源的金鑰和端點,將應用程式連線至文件智慧服務 API。 您稍後會在快速入門中將金鑰和端點貼上至程式碼中:

    Azure 入口網站 中金鑰和端點位置的螢幕快照。

設定

  1. 啟動 Visual Studio 2019。

  2. 在開始頁面中,選擇 [建立新的專案]。

    Visual Studio 開始視窗之螢幕擷取畫面。

  3. 在 [建立新的專案] 頁面的搜尋方塊中,輸入主控台。 選擇主控台 應用程式 範本,然後選擇 [ 下一步]。

    Visual Studio 建立新項目頁面的螢幕快照。

  4. 在 [ 設定新專案] 對話框視窗中,於 [項目 名稱] 方塊中輸入 formRecognizer_quickstart 。 接著,選擇 [下一步]。

    Visual Studios 設定新專案對話框視窗的螢幕快照。

  5. 在 [ 其他資訊 ] 對話框視窗中,選取 [.NET 5.0(目前],然後選取 [ 建立]。

    Visual Studio 其他資訊對話框視窗的螢幕快照。

使用 NuGet 安裝客戶端連結庫

  1. 以滑鼠右鍵按兩下您的 formRecognizer_quickstart 項目,然後選取 [ 管理 NuGet 套件... ]。

    顯示選取 [NuGet 套件] 視窗的螢幕快照。

  2. 選取 [流覽] 索引標籤,然後輸入 Azure.AI.FormRecognizer。

    顯示選取 [文件智慧套件] 下拉功能表的螢幕快照。

  3. 從下拉功能表中選取 3.1.1 版,然後選取 [安裝]。

建置您的 應用程式

若要與此文件智慧服務互動,您必須建立 FormRecognizerClient 類別的執行個體。 若要這樣做,請使用 您的金鑰和 FormRecognizerClient 具有和 Document Intelligence endpoint的 實體AzureKeyCredential來建立 AzureKeyCredential

注意

  • 從 .NET 6 開始,使用 console 範本的新專案會產生與舊版不同的新程序樣式。
  • 新的輸出會使用最新的 C# 功能,以簡化您需要撰寫的程式代碼。
  • 當您使用較新版本時,只需要撰寫 Main 方法的本文。 您不需要包含最上層語句、全域 using 指示詞,或隱含 using using 指示詞。
  • 如需詳細資訊,請參閱新的 C# 範本產生最上層語句。
  1. 開啟 Program.cs 檔案。

  2. 包含下列 using 指示詞:

using Azure;
using Azure.AI.FormRecognizer;
using Azure.AI.FormRecognizer.Models;
using System.Threading.Tasks;
  1. 設定和 endpoint 環境變數, key 並建立和 AzureKeyCredentialFormRecognizerClient 實例:
private static readonly string endpoint = "your-form-recognizer-endpoint";
private static readonly string key = "your-api-key";
private static readonly AzureKeyCredential credential = new AzureKeyCredential(key);
  1. 刪除這一行, Console.Writeline("Hello World!"); 並新增其中 一個 Try It 程式 代碼範例來 Program.cs 檔案:

    將範例程式代碼新增至 Main 方法的螢幕快照。

  2. 選取程式代碼範例,以複製並貼到應用程式的 Main 方法中:

重要

完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性一文。

試試看:版面配置模型

從檔擷取文字、選取標記、文字樣式和表格結構及其周框區域座標。

  • 在此範例中,您需要 URI 的檔案檔案。 您可以使用本快速入門的 範例檔
  • 我們已將檔案 URI 值新增至 formUri 變數。
  • 若要從 URI 上的指定檔案擷取配置,請使用 StartRecognizeContentFromUriAsync 方法。

將下列程式代碼新增至設定應用程式Program.cs檔案:


FormRecognizerClient recognizerClient = AuthenticateClient();

Task recognizeContent = RecognizeContent(recognizerClient);
Task.WaitAll(recognizeContent);

private static FormRecognizerClient AuthenticateClient()
            {
                var credential = new AzureKeyCredential(key);
                var client = new FormRecognizerClient(new Uri(endpoint), credential);
                return client;
            }

            private static async Task RecognizeContent(FormRecognizerClient recognizerClient)
        {
            string formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf";
            FormPageCollection formPages = await recognizerClient
        .StartRecognizeContentFromUri(new Uri(formUrl))
        .WaitForCompletionAsync();

            foreach (FormPage page in formPages)
            {
                Console.WriteLine($"Form Page {page.PageNumber} has {page.Lines.Count} lines.");

                for (int i = 0; i < page.Lines.Count; i++)
                {
                    FormLine line = page.Lines[i];
                    Console.WriteLine($"    Line {i} has {line.Words.Count} word{(line.Words.Count > 1 ? "s" : "")}, and text: '{line.Text}'.");
                }

                for (int i = 0; i < page.Tables.Count; i++)
                {
                    FormTable table = page.Tables[i];
                    Console.WriteLine($"Table {i} has {table.RowCount} rows and {table.ColumnCount} columns.");
                    foreach (FormTableCell cell in table.Cells)
                    {
                        Console.WriteLine($"    Cell ({cell.RowIndex}, {cell.ColumnIndex}) contains text: '{cell.Text}'.");
                    }
                }
            }
        }
    }
}

試試看:預先建置的模型

此範例示範如何使用發票作為範例,使用預先定型的模型,分析來自特定類型通用文件的數據。

  • 在此範例中,我們會使用預先建置的模型來分析發票檔。 您可以針對本快速入門使用我們的 範例發票檔
  • 我們已將檔案 URI 值新增至 invoiceUri Main 方法頂端的 變數。
  • 若要在 URI 分析指定的檔案,請使用 StartRecognizeInvoicesFromUriAsync 方法。
  • 為了簡單起見,服務傳回的所有字段都不會在此顯示。 若要查看所有支援的欄位和對應類型清單,請參閱我們的 發票 概念頁面。

選擇預先建置的模型

您不限於發票,有數個預先建置的模型可供選擇,每個模型都有自己的支援欄位集。 要用於分析作業的模型取決於要分析的檔類型。 以下是 Document Intelligence 服務目前支援的預建模型:

  • 發票:從發票擷取文字、選取標記、數據表、欄位和密鑰資訊。
  • 收據:從收據擷取文字和重要資訊。
  • 標識符檔:從駕駛執照和國際護照中擷取文字和密鑰資訊。
  • 名片:從名片擷取文字和重要資訊。

將下列程式代碼新增至預先建置的發票應用程式Program.cs檔案方法

FormRecognizerClient recognizerClient = AuthenticateClient();

  Task analyzeinvoice = AnalyzeInvoice(recognizerClient, invoiceUrl);
  Task.WaitAll(analyzeinvoice);

   private static FormRecognizerClient AuthenticateClient() {
     var credential = new AzureKeyCredential(key);
     var client = new FormRecognizerClient(new Uri(endpoint), credential);
     return client;
   }

   static string invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf";

   private static async Task AnalyzeInvoice(FormRecognizerClient recognizerClient, string invoiceUrl) {
     var options = new RecognizeInvoicesOptions() {
       Locale = "en-US"
     };
     RecognizedFormCollection invoices = await recognizerClient.StartRecognizeInvoicesFromUriAsync(new Uri(invoiceUrl), options).WaitForCompletionAsync();

     RecognizedForm invoice = invoices[0];

     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}");
       }
     }
   }
 }
}

執行您的應用程式

選擇formRecognizer_quickstart旁的綠色 [開始 ] 按鈕來建置和執行程式,或按 F5

執行您的 Visual Studio 程式之螢幕擷取畫面。

參考文檔 | 庫原始程式碼 | 套件 (Maven)範例 |

在本快速入門中,您會使用下列 API 從窗體和檔擷取結構化數據:

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶

  • Java 開發工具套件 (JDK) 第 8 版或更新版本。 如需詳細資訊, 請參閱支援的 Java 版本和更新排程

  • Azure AI 服務或文件智慧服務資源。 擁有 Azure 訂用帳戶之後,請在 Azure 入口網站 中建立單一服務多服務 Document Intelligence 資源,以取得您的密鑰和端點。 您可以使用免費定價層 (F0) 來試用服務,之後可升級至付費層以用於實際執行環境。

  • 部署資源之後,選取 [移至資源]。 您需要使用已建立資源的金鑰和端點,將應用程式連線至文件智慧服務 API。 您稍後會在快速入門中將金鑰和端點貼上至程式碼中:

    Azure 入口網站 中金鑰和端點位置的螢幕快照。

設定

建立新的 Gradle 專案

在控制台視窗中(例如 cmd、PowerShell 或 Bash),為您的應用程式建立名為 form-recognizer-app 的新目錄,並流覽至該目錄。

mkdir form-recognizer-app && form-recognizer-app
  1. gradle init從工作目錄執行 命令。 此命令會建立 Gradle 的基本組建檔案,包括 build.gradle.kts,將在執行階段使用 build.gradle.kts,來建立及設定應用程式。

    gradle init --type basic
    
  2. 當系統提示您選擇 DSL 時,請選取 [Kotlin]。

  3. 接受預設項目名稱 (form-recognizer-app)

安裝用戶端程式庫

本快速入門使用 Gradle 相依性管理員。 您可以在 Maven 中央存放庫找到其他相依性管理員的用戶端連結庫和資訊。

在專案的 build.gradle.kts 檔案中,包含客戶端連結庫做為implementation語句,以及必要的外掛程式和設定。

plugins {
    java
    application
}
application {
    mainClass.set("FormRecognizer")
}
repositories {
    mavenCentral()
}
dependencies {
    implementation(group = "com.azure", name = "azure-ai-formrecognizer", version = "3.1.1")
}

建立 Java 檔案

從您的工作目錄中,執行下列命令:

mkdir -p src/main/java

您會建立下列目錄結構:

應用程式的 Java 目錄結構的螢幕快照。

流覽至 Java 目錄,並建立名為 FormRecognizer.java 的檔案。 在慣用的編輯器或 IDE 中開啟它,並新增下列套件宣告和 import 語句:

import com.azure.ai.formrecognizer.*;
import com.azure.ai.formrecognizer.models.*;

import java.util.concurrent.atomic.AtomicReference;
import java.util.List;
import java.util.Map;
import java.time.LocalDate;

import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.util.Context;
import com.azure.core.util.polling.SyncPoller;

選取程式代碼範例,以複製並貼到應用程式的主要方法:

重要

完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性

試試看:版面配置模型

從檔擷取文字、選取標記、文字樣式和表格結構及其周框區域座標。

  • 在此範例中,您需要 URI 的檔案檔案。 您可以使用本快速入門的 範例檔
  • 若要在 URI 分析指定的檔案,您將使用 beginRecognizeContentFromUrl 方法。
  • 我們已將檔案 URI 值新增至 formUrl main 方法中的 變數。

使用下列程式代碼更新應用程式的 FormRecognizer 類別(請務必使用您 Azure 入口網站 Document Intelligence 實例的值來更新密鑰和端點變數):


static final String key = "PASTE_YOUR_FORM_RECOGNIZER_KEY_HERE";
static final String endpoint = "PASTE_YOUR_FORM_RECOGNIZER_ENDPOINT_HERE";

public static void main(String[] args) {FormRecognizerClient recognizerClient = new FormRecognizerClientBuilder()
                .credential(new AzureKeyCredential(key)).endpoint(endpoint).buildClient();

    String formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf";

    System.out.println("Get form content...");
        GetContent(recognizerClient, formUrl);
  }
    private static void GetContent(FormRecognizerClient recognizerClient, String invoiceUri) {
        String analyzeFilePath = invoiceUri;
        SyncPoller<FormRecognizerOperationResult, List<FormPage>> recognizeContentPoller = recognizerClient
                .beginRecognizeContentFromUrl(analyzeFilePath);

        List<FormPage> contentResult = recognizeContentPoller.getFinalResult();
        // </snippet_getcontent_call>
        // <snippet_getcontent_print>
        contentResult.forEach(formPage -> {
            // Table information
            System.out.println("----Recognizing content ----");
            System.out.printf("Has width: %f and height: %f, measured with unit: %s.%n", formPage.getWidth(),
                    formPage.getHeight(), formPage.getUnit());
            formPage.getTables().forEach(formTable -> {
                System.out.printf("Table has %d rows and %d columns.%n", formTable.getRowCount(),
                        formTable.getColumnCount());
                formTable.getCells().forEach(formTableCell -> {
                    System.out.printf("Cell has text %s.%n", formTableCell.getText());
                });
                System.out.println();
            });
        });
    }

試試看:預先建置的模型

此範例示範如何使用發票作為範例,使用預先定型的模型,分析來自特定類型通用文件的數據。

  • 在此範例中,我們會使用預先建置的模型來分析發票檔。 您可以針對本快速入門使用我們的 範例發票檔
  • 若要在 URI 分析指定的檔案,您將使用 beginRecognizeInvoicesFromUrl
  • 我們已將檔案 URI 值新增至 invoiceUrl main 方法中的 變數。
  • 為了簡單起見,服務傳回的所有字段都不會在此顯示。 若要查看所有支援的欄位和對應類型清單,請參閱我們的 發票 概念頁面。

選擇預先建置的模型

您不限於發票,有數個預先建置的模型可供選擇,每個模型都有自己的支援欄位集。 要用於分析作業的模型取決於要分析的檔類型。 以下是 Document Intelligence 服務目前支援的預建模型:

  • 發票:從發票擷取文字、選取標記、數據表、欄位和密鑰資訊。
  • 收據:從收據擷取文字和重要資訊。
  • 標識符檔:從駕駛執照和國際護照中擷取文字和密鑰資訊。
  • 名片:從名片擷取文字和重要資訊。

使用下列程式代碼更新應用程式的 FormRecognizer 類別(請務必以您 Azure 入口網站 Document Intelligence 實例的值更新金鑰和端點變數):


static final String key = "PASTE_YOUR_FORM_RECOGNIZER_KEY_HERE";
static final String endpoint = "PASTE_YOUR_FORM_RECOGNIZER_ENDPOINT_HERE";

public static void main(String[] args) {
    FormRecognizerClient recognizerClient = new FormRecognizerClientBuilder().credential(new AzureKeyCredential(key)).endpoint(endpoint).buildClient();

    String invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf";

    System.out.println("Analyze invoice...");
        AnalyzeInvoice(recognizerClient, invoiceUrl);
  }
    private static void AnalyzeInvoice(FormRecognizerClient recognizerClient, String invoiceUrl) {
      SyncPoller < FormRecognizerOperationResult,
        List < RecognizedForm >> recognizeInvoicesPoller = recognizerClient.beginRecognizeInvoicesFromUrl(invoiceUrl);
      List < RecognizedForm > recognizedInvoices = recognizeInvoicesPoller.getFinalResult();

      for (int i = 0; i < recognizedInvoices.size(); i++) {
        RecognizedForm recognizedInvoice = recognizedInvoices.get(i);
        Map < String,
        FormField > recognizedFields = recognizedInvoice.getFields();
        System.out.printf("----------- Recognized invoice info for page %d -----------%n", i);
        FormField vendorNameField = recognizedFields.get("VendorName");
        if (vendorNameField != null) {
            if (FieldValueType.STRING == vendorNameField.getValue().getValueType()) {
                String merchantName = vendorNameField.getValue().asString();
                System.out.printf("Vendor Name: %s, confidence: %.2f%n", merchantName, vendorNameField.getConfidence());
            }
        }

        FormField vendorAddressField = recognizedFields.get("VendorAddress");
        if (vendorAddressField != null) {
            if (FieldValueType.STRING == vendorAddressField.getValue().getValueType()) {
                String merchantAddress = vendorAddressField.getValue().asString();
                System.out.printf("Vendor address: %s, confidence: %.2f%n", merchantAddress, vendorAddressField.getConfidence());
            }
        }

        FormField customerNameField = recognizedFields.get("CustomerName");
        if (customerNameField != null) {
            if (FieldValueType.STRING == customerNameField.getValue().getValueType()) {
                String merchantAddress = customerNameField.getValue().asString();
                System.out.printf("Customer Name: %s, confidence: %.2f%n", merchantAddress, customerNameField.getConfidence());
            }
        }

        FormField customerAddressRecipientField = recognizedFields.get("CustomerAddressRecipient");
        if (customerAddressRecipientField != null) {
            if (FieldValueType.STRING == customerAddressRecipientField.getValue().getValueType()) {
                String customerAddr = customerAddressRecipientField.getValue().asString();
                System.out.printf("Customer Address Recipient: %s, confidence: %.2f%n", customerAddr, customerAddressRecipientField.getConfidence());
            }
        }

        FormField invoiceIdField = recognizedFields.get("InvoiceId");
        if (invoiceIdField != null) {
            if (FieldValueType.STRING == invoiceIdField.getValue().getValueType()) {
                String invoiceId = invoiceIdField.getValue().asString();
                System.out.printf("Invoice Id: %s, confidence: %.2f%n", invoiceId, invoiceIdField.getConfidence());
            }
        }

        FormField invoiceDateField = recognizedFields.get("InvoiceDate");
        if (customerNameField != null) {
            if (FieldValueType.DATE == invoiceDateField.getValue().getValueType()) {
                LocalDate invoiceDate = invoiceDateField.getValue().asDate();
                System.out.printf("Invoice Date: %s, confidence: %.2f%n", invoiceDate, invoiceDateField.getConfidence());
            }
        }

        FormField invoiceTotalField = recognizedFields.get("InvoiceTotal");
        if (customerAddressRecipientField != null) {
            if (FieldValueType.FLOAT == invoiceTotalField.getValue().getValueType()) {
                Float invoiceTotal = invoiceTotalField.getValue().asFloat();
                System.out.printf("Invoice Total: %.2f, confidence: %.2f%n", invoiceTotal, invoiceTotalField.getConfidence());
            }
        }
    }
}

建置並執行您的應用程式

流覽回您的主要項目目錄-form-recognizer-app

  1. 使用 命令建置 build 您的應用程式:
gradle build
  1. 使用 命令執行 run 您的應用程式:
gradle run

參考文檔 | 庫原始程式碼 | 套件 (npm)範例 |

在本快速入門中,您會使用下列 API 從窗體和檔擷取結構化數據:

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶

  • 最新版的 Visual Studio Code 或您慣用的 IDE。

  • 最新版的 Node.js

  • Azure AI 服務或文件智慧服務資源。 擁有 Azure 訂用帳戶之後,請在 Azure 入口網站 中建立單一服務多服務 Document Intelligence 資源,以取得您的密鑰和端點。 您可以使用免費定價層 (F0) 來試用服務,之後可升級至付費層以用於實際執行環境。

    提示

    如果您打算在單一端點/金鑰下存取多個 Azure AI 服務,請建立 Azure AI 服務資源。 若為僅限文件智慧服務存取,請建立文件智慧服務資源。 請注意,如果您想要使用 Microsoft Entra 驗證,您將需要單一服務資源。

  • 部署資源之後,選取 [移至資源]。 您需要使用已建立資源的金鑰和端點,將應用程式連線至文件智慧服務 API。 您稍後會在快速入門中將金鑰和端點貼上至程式碼中:

    Azure 入口網站 中金鑰和端點位置的螢幕快照。

設定

  1. 建立新的Node.js應用程式。 在主控台視窗中(例如 cmd、PowerShell 或 Bash),為您的應用程式建立新的目錄,然後流覽至它。

    mkdir form-recognizer-app && cd form-recognizer-app
    
  2. npm init執行 命令以使用 package.json 檔案建立節點應用程式。

    npm init
    
  3. 安裝客戶端連結 ai-form-recognizer 庫 npm 套件:

    npm install @azure/ai-form-recognizer
    

    您的應用程式檔案 package.json 會隨著相依性更新。

  4. 建立名為 index.js的檔案,加以開啟,然後匯入下列連結庫:

    const { FormRecognizerClient, AzureKeyCredential } = require("@azure/ai-form-recognizer");
    
  5. 為資源的 Azure 端點和金鑰建立變數:

    const key = "PASTE_YOUR_FORM_RECOGNIZER_KEY_HERE";
    const endpoint = "PASTE_YOUR_FORM_RECOGNIZER_ENDPOINT_HERE";
    
  6. 此時,您的 JavaScript 應用程式應該包含下列幾行程式代碼:

    
    const { FormRecognizerClient, AzureKeyCredential } = require("@azure/ai-form-recognizer");
    
    const endpoint = "PASTE_YOUR_FORM_RECOGNIZER_ENDPOINT_HERE";
    const key = "PASTE_YOUR_FORM_RECOGNIZER_KEY_HERE";
    

選取程式代碼範例,以複製並貼到您的應用程式:

重要

完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性

試試看:版面配置模型

  • 在此範例中,您需要 URI 的檔案檔案。 您可以使用本快速入門的 範例檔
  • 我們已將檔案 URI 值新增至 formUrl 靠近檔案頂端的變數。
  • 若要在 URI 分析指定的檔案,您將使用 beginRecognizeContent 方法。

將下列程式代碼新增至變數下方這一行的版面配置 key 應用程式

const formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf";

const formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf";

async function recognizeContent() {
    const client = new FormRecognizerClient(endpoint, new AzureKeyCredential(key));
    const poller = await client.beginRecognizeContentFromUrl(formUrl);
    const pages = await poller.pollUntilDone();

    if (!pages || pages.length === 0) {
        throw new Error("Expecting non-empty list of pages!");
    }

    for (const page of pages) {
        console.log(
            `Page ${page.pageNumber}: width ${page.width} and height ${page.height} with unit ${page.unit}`
        );
        for (const table of page.tables) {
            for (const cell of table.cells) {
                console.log(`cell [${cell.rowIndex},${cell.columnIndex}] has text ${cell.text}`);
            }
        }
    }
}

recognizeContent().catch((err) => {
    console.error("The sample encountered an error:", err);
});

試試看:預先建置的模型

此範例示範如何使用發票作為範例,使用預先定型的模型,分析來自特定類型通用文件的數據。 如需發票欄位的完整清單,請參閱我們預先建置的概念頁面

  • 在此範例中,我們會使用預先建置的模型來分析發票檔。 您可以針對本快速入門使用我們的 範例發票檔
  • 我們已將檔案 URI 值新增至 invoiceUrl 檔案頂端的變數。
  • 若要在 URI 分析指定的檔案,您將使用 beginRecognizeInvoices 方法。
  • 為了簡單起見,服務傳回的所有字段都不會在此顯示。 若要查看所有支援的欄位和對應類型清單,請參閱我們的 發票 概念頁面。

選擇預先建置的模型

您不限於發票,有數個預先建置的模型可供選擇,每個模型都有自己的支援欄位集。 要用於分析作業的模型取決於要分析的檔類型。 以下是 Document Intelligence 服務目前支援的預建模型:

  • 發票:從發票擷取文字、選取標記、數據表、欄位和密鑰資訊。
  • 收據:從收據擷取文字和重要資訊。
  • 標識符檔:從駕駛執照和國際護照中擷取文字和密鑰資訊。
  • 名片:從名片擷取文字和重要資訊。

將下列程式代碼新增至變數下方 key 預先建置的發票應用程式


const invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf";

async function recognizeInvoices() {

    const client = new FormRecognizerClient(endpoint, new AzureKeyCredential(key));

    const poller = await client.beginRecognizeInvoicesFromUrl(invoiceUrl);
    const [invoice] = await poller.pollUntilDone();

    if (invoice === undefined) {
        throw new Error("Failed to extract data from at least one invoice.");
    }

    /**
     * This is a helper function for printing a simple field with an elemental type.
     */
    function fieldToString(field) {
        const {
            name,
            valueType,
            value,
            confidence
        } = field;
        return `${name} (${valueType}): '${value}' with confidence ${confidence}'`;
    }

    console.log("Invoice fields:");

    /**
     * Invoices contain a lot of optional fields, but they are all of elemental types
     * such as strings, numbers, and dates, so we will just enumerate them all.
     */
    for (const [name, field] of Object.entries(invoice.fields)) {
        if (field.valueType !== "array" && field.valueType !== "object") {
            console.log(`- ${name} ${fieldToString(field)}`);
        }
    }

    // Invoices also support nested line items, so we can iterate over them.
    let idx = 0;

    console.log("- Items:");

    const items = invoice.fields["Items"]?.value;
    for (const item of items ?? []) {
        const value = item.value;

        // Each item has several subfields that are nested within the item. We'll
        // map over this list of the subfields and filter out any fields that
        // weren't found. Not all fields will be returned every time, only those
        // that the service identified for the particular document in question.

        const subFields = [
                "Description",
                "Quantity",
                "Unit",
                "UnitPrice",
                "ProductCode",
                "Date",
                "Tax",
                "Amount"
            ]
            .map((fieldName) => value[fieldName])
            .filter((field) => field !== undefined);

        console.log(
            [
                `  - Item #${idx}`,
                // Now we will convert those fields into strings to display
                ...subFields.map((field) => `    - ${fieldToString(field)}`)
            ].join("\n")
        );
    }
}

recognizeInvoices().catch((err) => {
    console.error("The sample encountered an error:", err);
});

參考文件 | 連結庫原始程式碼 | 套件 (PyPi)範例 |

在本快速入門中,您會使用下列 API 從窗體和檔擷取結構化數據:

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶

  • Python 3.x

    • 您的 Python 安裝應該包含 pip。 您可以藉由在命令列上執行 pip --version 來檢查您是否已安裝 pip。 安裝最新版本的 Python 以取得 pip。
  • Azure AI 服務或文件智慧服務資源。 擁有 Azure 訂用帳戶之後,請在 Azure 入口網站 中建立單一服務多服務 Document Intelligence 資源,以取得您的密鑰和端點。 您可以使用免費定價層 (F0) 來試用服務,之後可升級至付費層以用於實際執行環境。

    提示

    如果您打算在單一端點/金鑰下存取多個 Azure AI 服務,請建立 Azure AI 服務資源。 若為僅限文件智慧服務存取,請建立文件智慧服務資源。 請注意,如果您想要使用 Microsoft Entra 驗證,您將需要單一服務資源。

  • 部署資源之後,選取 [移至資源]。 您需要使用已建立資源的金鑰和端點,將應用程式連線至文件智慧服務 API。 您稍後會在快速入門中將金鑰和端點貼上至程式碼中:

    Azure 入口網站 中金鑰和端點位置的螢幕快照。

設定

在本機環境中開啟終端機視窗,並使用 pip 安裝適用於 Python 的 Azure AI 文件智慧服務用戶端程式庫:

pip install azure-ai-formrecognizer

建立新的 Python 應用程式

在慣用的編輯器或 IDE 中建立名為 form_recognizer_quickstart.py 的新 Python 應用程式。 然後匯入下列連結庫:

import os
from azure.ai.formrecognizer import FormRecognizerClient
from azure.core.credentials import AzureKeyCredential

為您的 Azure 資源端點和金鑰建立變數

endpoint = "YOUR_FORM_RECOGNIZER_ENDPOINT"
key = "YOUR_FORM_RECOGNIZER_KEY"

此時,您的 Python 應用程式應該包含下列幾行程式代碼:

import os
from azure.core.exceptions import ResourceNotFoundError
from azure.ai.formrecognizer import DocumentAnalysisClient
from azure.core.credentials import AzureKeyCredential

endpoint = "YOUR_FORM_RECOGNIZER_ENDPOINT"
key = "YOUR_FORM_RECOGNIZER_KEY"

選取程式代碼範例,以複製並貼到您的應用程式:

重要

完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性

試試看:版面配置模型

  • 在此範例中,您需要 URI 的檔案檔案。 您可以使用本快速入門的 範例檔
  • 我們已將檔案 URI 值新增至 formUrl 靠近檔案頂端的變數。
  • 若要在 URI 分析指定的檔案,您將使用 begin_recognize_content_from_url 方法。

將下列程式代碼新增至變數下方這一行的版面配置 key 應用程式


  def format_bounding_box(bounding_box):
    if not bounding_box:
        return "N/A"
    return ", ".join(["[{}, {}]".format(p.x, p.y) for p in bounding_box])

 def recognize_content():
    # sample document
    formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"

    form_recognizer_client = FormRecognizerClient(
        endpoint=endpoint, credential=AzureKeyCredential(key)
    )

    poller = form_recognizer_client.begin_recognize_content_from_url(formUrl)
    form_pages = poller.result()

    for idx, content in enumerate(form_pages):
        print(
            "Page has width: {} and height: {}, measured with unit: {}".format(
                content.width, content.height, content.unit
            )
        )
        for table_idx, table in enumerate(content.tables):
            print(
                "Table # {} has {} rows and {} columns".format(
                    table_idx, table.row_count, table.column_count
                )
            )
            print(
                "Table # {} location on page: {}".format(
                    table_idx, format_bounding_box(table.bounding_box)
                )
            )
            for cell in table.cells:
                print(
                    "...Cell[{}][{}] has text '{}' within bounding box '{}'".format(
                        cell.row_index,
                        cell.column_index,
                        cell.text,
                        format_bounding_box(cell.bounding_box),
                    )
                )

        for line_idx, line in enumerate(content.lines):
            print(
                "Line # {} has word count '{}' and text '{}' within bounding box '{}'".format(
                    line_idx,
                    len(line.words),
                    line.text,
                    format_bounding_box(line.bounding_box),
                )
            )
            if line.appearance:
                if (
                    line.appearance.style_name == "handwriting"
                    and line.appearance.style_confidence > 0.8
                ):
                    print(
                        "Text line '{}' is handwritten and might be a signature.".format(
                            line.text
                        )
                    )
            for word in line.words:
                print(
                    "...Word '{}' has a confidence of {}".format(
                        word.text, word.confidence
                    )
                )

        for selection_mark in content.selection_marks:
            print(
                "Selection mark is '{}' within bounding box '{}' and has a confidence of {}".format(
                    selection_mark.state,
                    format_bounding_box(selection_mark.bounding_box),
                    selection_mark.confidence,
                )
            )
        print("----------------------------------------")


if __name__ == "__main__":
    recognize_content()

試試看:預先建置的模型

此範例示範如何使用發票作為範例,使用預先定型的模型,分析來自特定類型通用文件的數據。 如需發票欄位的完整清單,請參閱我們預先建置的概念頁面

  • 在此範例中,我們會使用預先建置的模型來分析發票檔。 您可以針對本快速入門使用我們的 範例發票檔
  • 我們已將檔案 URI 值新增至檔案頂端的 ''formUrl' 變數。
  • 若要在 URI 分析指定的檔案,您將使用 ''begin_recognize_invoices_from_url' 方法。
  • 為了簡單起見,服務傳回的所有字段都不會在此顯示。 若要查看所有支援的欄位和對應類型清單,請參閱我們的 發票 概念頁面。

選擇預先建置的模型

您不限於發票,有數個預先建置的模型可供選擇,每個模型都有自己的支援欄位集。 要用於分析作業的模型取決於要分析的檔類型。 以下是 Document Intelligence 服務目前支援的預建模型:

  • 發票:從發票擷取文字、選取標記、數據表、欄位和密鑰資訊。
  • 收據:從收據擷取文字和重要資訊。
  • 標識符檔:從駕駛執照和國際護照中擷取文字和密鑰資訊。
  • 名片:從名片擷取文字和重要資訊。

將下列程式代碼新增至變數下方 key 預先建置的發票應用程式


def recognize_invoice():

    invoiceUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"

    form_recognizer_client = FormRecognizerClient(
        endpoint=endpoint, credential=AzureKeyCredential(key)
    )

    poller = form_recognizer_client.begin_recognize_invoices_from_url(
        invoiceUrl, locale="en-US"
    )
    invoices = poller.result()

    for idx, invoice in enumerate(invoices):
        vendor_name = invoice.fields.get("VendorName")
        if vendor_name:
            print(
                "Vendor Name: {} has confidence: {}".format(
                    vendor_name.value, vendor_name.confidence
                )
            )
        vendor_address = invoice.fields.get("VendorAddress")
        if vendor_address:
            print(
                "Vendor Address: {} has confidence: {}".format(
                    vendor_address.value, vendor_address.confidence
                )
            )
        vendor_address_recipient = invoice.fields.get("VendorAddressRecipient")
        if vendor_address_recipient:
            print(
                "Vendor Address Recipient: {} has confidence: {}".format(
                    vendor_address_recipient.value, vendor_address_recipient.confidence
                )
            )
        customer_name = invoice.fields.get("CustomerName")
        if customer_name:
            print(
                "Customer Name: {} has confidence: {}".format(
                    customer_name.value, customer_name.confidence
                )
            )
        customer_id = invoice.fields.get("CustomerId")
        if customer_id:
            print(
                "Customer Id: {} has confidence: {}".format(
                    customer_id.value, customer_id.confidence
                )
            )
        customer_address = invoice.fields.get("CustomerAddress")
        if customer_address:
            print(
                "Customer Address: {} has confidence: {}".format(
                    customer_address.value, customer_address.confidence
                )
            )
        customer_address_recipient = invoice.fields.get("CustomerAddressRecipient")
        if customer_address_recipient:
            print(
                "Customer Address Recipient: {} has confidence: {}".format(
                    customer_address_recipient.value,
                    customer_address_recipient.confidence,
                )
            )
        invoice_id = invoice.fields.get("InvoiceId")
        if invoice_id:
            print(
                "Invoice Id: {} has confidence: {}".format(
                    invoice_id.value, invoice_id.confidence
                )
            )
        invoice_date = invoice.fields.get("InvoiceDate")
        if invoice_date:
            print(
                "Invoice Date: {} has confidence: {}".format(
                    invoice_date.value, invoice_date.confidence
                )
            )
        invoice_total = invoice.fields.get("InvoiceTotal")
        if invoice_total:
            print(
                "Invoice Total: {} has confidence: {}".format(
                    invoice_total.value, invoice_total.confidence
                )
            )
        due_date = invoice.fields.get("DueDate")
        if due_date:
            print(
                "Due Date: {} has confidence: {}".format(
                    due_date.value, due_date.confidence
                )
            )
        purchase_order = invoice.fields.get("PurchaseOrder")
        if purchase_order:
            print(
                "Purchase Order: {} has confidence: {}".format(
                    purchase_order.value, purchase_order.confidence
                )
            )
        billing_address = invoice.fields.get("BillingAddress")
        if billing_address:
            print(
                "Billing Address: {} has confidence: {}".format(
                    billing_address.value, billing_address.confidence
                )
            )
        billing_address_recipient = invoice.fields.get("BillingAddressRecipient")
        if billing_address_recipient:
            print(
                "Billing Address Recipient: {} has confidence: {}".format(
                    billing_address_recipient.value,
                    billing_address_recipient.confidence,
                )
            )
        shipping_address = invoice.fields.get("ShippingAddress")
        if shipping_address:
            print(
                "Shipping Address: {} has confidence: {}".format(
                    shipping_address.value, shipping_address.confidence
                )
            )
        shipping_address_recipient = invoice.fields.get("ShippingAddressRecipient")
        if shipping_address_recipient:
            print(
                "Shipping Address Recipient: {} has confidence: {}".format(
                    shipping_address_recipient.value,
                    shipping_address_recipient.confidence,
                )
            )
        print("Invoice items:")
        for idx, item in enumerate(invoice.fields.get("Items").value):
            item_description = item.value.get("Description")
            if item_description:
                print(
                    "......Description: {} has confidence: {}".format(
                        item_description.value, item_description.confidence
                    )
                )
            item_quantity = item.value.get("Quantity")
            if item_quantity:
                print(
                    "......Quantity: {} has confidence: {}".format(
                        item_quantity.value, item_quantity.confidence
                    )
                )
            unit = item.value.get("Unit")
            if unit:
                print(
                    "......Unit: {} has confidence: {}".format(
                        unit.value, unit.confidence
                    )
                )
            unit_price = item.value.get("UnitPrice")
            if unit_price:
                print(
                    "......Unit Price: {} has confidence: {}".format(
                        unit_price.value, unit_price.confidence
                    )
                )
            product_code = item.value.get("ProductCode")
            if product_code:
                print(
                    "......Product Code: {} has confidence: {}".format(
                        product_code.value, product_code.confidence
                    )
                )
            item_date = item.value.get("Date")
            if item_date:
                print(
                    "......Date: {} has confidence: {}".format(
                        item_date.value, item_date.confidence
                    )
                )
            tax = item.value.get("Tax")
            if tax:
                print(
                    "......Tax: {} has confidence: {}".format(tax.value, tax.confidence)
                )
            amount = item.value.get("Amount")
            if amount:
                print(
                    "......Amount: {} has confidence: {}".format(
                        amount.value, amount.confidence
                    )
                )
        subtotal = invoice.fields.get("SubTotal")
        if subtotal:
            print(
                "Subtotal: {} has confidence: {}".format(
                    subtotal.value, subtotal.confidence
                )
            )
        total_tax = invoice.fields.get("TotalTax")
        if total_tax:
            print(
                "Total Tax: {} has confidence: {}".format(
                    total_tax.value, total_tax.confidence
                )
            )
        previous_unpaid_balance = invoice.fields.get("PreviousUnpaidBalance")
        if previous_unpaid_balance:
            print(
                "Previous Unpaid Balance: {} has confidence: {}".format(
                    previous_unpaid_balance.value, previous_unpaid_balance.confidence
                )
            )
        amount_due = invoice.fields.get("AmountDue")
        if amount_due:
            print(
                "Amount Due: {} has confidence: {}".format(
                    amount_due.value, amount_due.confidence
                )
            )
        service_start_date = invoice.fields.get("ServiceStartDate")
        if service_start_date:
            print(
                "Service Start Date: {} has confidence: {}".format(
                    service_start_date.value, service_start_date.confidence
                )
            )
        service_end_date = invoice.fields.get("ServiceEndDate")
        if service_end_date:
            print(
                "Service End Date: {} has confidence: {}".format(
                    service_end_date.value, service_end_date.confidence
                )
            )
        service_address = invoice.fields.get("ServiceAddress")
        if service_address:
            print(
                "Service Address: {} has confidence: {}".format(
                    service_address.value, service_address.confidence
                )
            )
        service_address_recipient = invoice.fields.get("ServiceAddressRecipient")
        if service_address_recipient:
            print(
                "Service Address Recipient: {} has confidence: {}".format(
                    service_address_recipient.value,
                    service_address_recipient.confidence,
                )
            )
        remittance_address = invoice.fields.get("RemittanceAddress")
        if remittance_address:
            print(
                "Remittance Address: {} has confidence: {}".format(
                    remittance_address.value, remittance_address.confidence
                )
            )
        remittance_address_recipient = invoice.fields.get("RemittanceAddressRecipient")
        if remittance_address_recipient:
            print(
                "Remittance Address Recipient: {} has confidence: {}".format(
                    remittance_address_recipient.value,
                    remittance_address_recipient.confidence,
                )
            )


if __name__ == "__main__":
    recognize_invoice()

執行您的應用程式

  1. 流覽至您 擁有form_recognizer_quickstart.py 檔案的資料夾。

  2. 在您的終端機中輸入下列命令:

python form_recognizer_quickstart.py

| 文件智慧服務 REST API | Azure REST API 參考 |

在本快速入門中,您會使用下列 API 從窗體和檔擷取結構化數據:

必要條件

  • Azure 訂用帳戶 - 建立免費帳戶

  • 已安裝 cURL

  • PowerShell 6.0 版以上,或類似的命令列應用程式。

  • Azure AI 服務或文件智慧服務資源。 擁有 Azure 訂用帳戶之後,請在 Azure 入口網站 中建立單一服務多服務 Document Intelligence 資源,以取得您的密鑰和端點。 您可以使用免費定價層 (F0) 來試用服務,之後可升級至付費層以用於實際執行環境。

    提示

    如果您打算在單一端點/金鑰下存取多個 Azure AI 服務,請建立 Azure AI 服務資源。 若為僅限文件智慧服務存取,請建立文件智慧服務資源。 請注意,如果您想要使用 Microsoft Entra 驗證,您將需要單一服務資源。

  • 部署資源之後,選取 [移至資源]。 您需要使用已建立資源的金鑰和端點,將應用程式連線至文件智慧服務 API。 您稍後會在快速入門中將金鑰和端點貼上至程式碼中:

    Azure 入口網站 中金鑰和端點位置的螢幕快照。

選取程式代碼範例,以複製並貼到您的應用程式:

重要

完成時,請記得從程式碼中移除金鑰,且不要公開張貼金鑰。 在生產環境中,請使用安全的方式來儲存和存取您的認證,例如 Azure Key Vault。 如需詳細資訊,請參閱 Azure AI 服務安全性

試試看:版面配置模型

  • 在此範例中,您需要 URI 的檔案檔案。 您可以使用本快速入門的 範例檔
  1. {endpoint} 取代為您使用文件智慧服務訂用帳戶取得的端點。
  2. 將取代 {key} 為您從上一個步驟複製的金鑰。
  3. \"{your-document-url}取代為範例檔 URL:
https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf

要求

curl -v -i POST "https://{endpoint}/formrecognizer/v2.1/layout/analyze" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: {key}" --data-ascii "{​​​​​​​'urlSource': '{your-document-url}'}​​​​​​​​"

Operation-Location

您收到 202 (Success) 回應,其中包含 Operation-Location 標頭。 此標頭的值包含結果識別碼,可用來查詢異步操作的狀態並取得結果:

https:// cognitiveservice/formrecognizer/v2.1/layout/analyzeResults/{resultId}

在下列範例中,作為 URL 的一部分,後面的 analyzeResults/ 字串是結果標識符。

https://cognitiveservice/formrecognizer/v2/layout/analyzeResults/54f0b076-4e38-43e5-81bd-b85b8835fdfb

取得版面配置結果

呼叫分析版面配置 API 之後,您可以呼叫取得分析版面配置結果 API,以取得作業的狀態和擷取的數據。 執行命令之前,請先進行下列變更:

  1. {endpoint} 取代為您使用文件智慧服務訂用帳戶取得的端點。
  2. 將取代 {key} 為您從上一個步驟複製的金鑰。
  3. 將取代 {resultId} 為上一個步驟的結果標識碼。

要求

curl -v -X GET "https://{endpoint}/formrecognizer/v2.1/layout/analyzeResults/{resultId}" -H "Ocp-Apim-Subscription-Key: {key}"

檢查結果

您收到 200 (success) 回應及 JSON 內容。

請參閱下列發票影像及其對應的 JSON 輸出。

  • 節點 "readResults" 包含每一行文字,其各自的周框方塊放置在頁面上。
  • 節點 selectionMarks 會顯示每個選取標記(複選框、單選標記),以及其狀態 selected 為 或 unselected
  • "pageResults" 段包含擷取的數據表。 針對每個數據表,會擷取文字、數據列和數據行索引、數據列和數據行跨越、周框方塊等等。

具有數據表的 Contoso 專案語句檔。

回應本文

您可以在 GitHub 上檢視完整的範例輸出。

試試看:預先建置的模型

  • 在此範例中,我們會使用預先建置的模型來分析發票檔。 您可以針對本快速入門使用我們的 範例發票檔

選擇預先建置的模型

您不限於發票,有數個預先建置的模型可供選擇,每個模型都有自己的支援欄位集。 要用於分析作業的模型取決於要分析的檔類型。 以下是 Document Intelligence 服務目前支援的預建模型:

  • 發票:從發票擷取文字、選取標記、數據表、欄位和密鑰資訊。
  • 收據:從收據擷取文字和重要資訊。
  • 標識符檔:從駕駛執照和國際護照中擷取文字和密鑰資訊。
  • 名片:從名片擷取文字和重要資訊。

執行命令之前,請先進行下列變更:

  1. {endpoint} 取代為您使用文件智慧服務訂用帳戶取得的端點。

  2. 將取代 {key} 為您從上一個步驟複製的金鑰。

  3. 將 取代 \"{your-document-url} 為範例發票 URL:

    https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf
    

要求

curl -v -i POST https://{endpoint}/formrecognizer/v2.1/prebuilt/invoice/analyze" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key:  {key}" --data-ascii "{​​​​​​​'urlSource': '{your invoice URL}'}​​​​​​​​"

Operation-Location

您收到 202 (Success) 回應,其中包含 Operation-Location 標頭。 此標頭的值包含結果識別碼,可用來查詢異步操作的狀態並取得結果:

https:// cognitiveservice/formrecognizer/v2.1/prebuilt/receipt/analyzeResults/{resultId}

在下列範例中,作為 URL 的一部分,後面的 analyzeResults/ 字串是結果識別碼:

https://cognitiveservice/formrecognizer/v2.1/prebuilt/invoice/analyzeResults/54f0b076-4e38-43e5-81bd-b85b8835fdfb

取得發票結果

呼叫分析發票 API 之後,您可以呼叫取得分析發票結果 API,以取得作業的狀態和擷取的數據。 執行命令之前,請先進行下列變更:

  1. {endpoint} 取代為您使用文件智慧服務訂用帳戶取得的金鑰。 您可以在文件智慧服務的資源 [概觀] 索引標籤上找到此項目。
  2. 將取代 {resultId} 為上一個步驟的結果標識碼。
  3. 以您的金鑰取代 {key}

要求

curl -v -X GET "https://{endpoint}/formrecognizer/v2.1/prebuilt/invoice/analyzeResults/{resultId}" -H "Ocp-Apim-Subscription-Key: {key}"

檢查回應

您收到 200 (Success) 回應及 JSON 輸出。

  • 欄位 "readResults" 包含從發票擷取的每一行文字。
  • 包含 "pageResults" 從發票擷取的數據表和選取標記。
  • 欄位 "documentResults" 包含發票最相關部分的索引鍵/值資訊。

請參閱範例發票檔。

回應本文

請參閱 GitHub 上的完整範例輸出。

就是這樣,做得很好!

下一步

  • 如需增強的體驗和進階模型品質,請嘗試 Document Intelligence Studio

    • Studio 支援使用 v2.1 標籤數據定型的任何模型。

    • 變更記錄提供從 v3.1 移轉至 v4.0 的詳細資訊。