Azure AI Document Intelligence Code Samples

repository·main·Indexed 42 days ago

https://github.com/azure-samples/document-intelligence-code-samples

Comprehensive code samples for the Azure AI Document Intelligence client library (formerly Form Recognizer). Includes implementation examples for prebuilt, custom, and RAG-based workflows using Java (SDK versions 1.0.0-beta.1 and 1.0.0-beta.2) and JavaScript (@azure-rest/ai-document-intelligence). Covers document analysis models such as Layout, Read, Invoices, and Receipts, as well as add-on capabilities like barcode and formula extraction.

Tokens
117.7K
Snippets
191
Records
230
Agent score
51%

What's inside Azure AI Document Intelligence

  1. Use Prebuilt models for intelligent document processing

    main

    Prebuilt models allow you to process specific document types without training custom models. They are optimized for extracting specific fields from common business documents:

    • Financial/Transaction: Invoice, Receipt, Pay stubs, Checks, Credit/Debit card.
    • Identity/Legal: Identity, Contract, Marriage certificate, Health Insurance card.
    • Mortgage: US Mortgage 1003, US Mortgage 1004, US Mortgage 1005, US Mortgage 1008, US Mortgage closing disclosure.
    • Tax: US Tax W-2, US Tax 1040, US Tax 1095, US Tax 1098, US Tax 1099.
  2. Use the prebuilt-idDocument model

    main

    The prebuilt-idDocument model is used to extract information from various identity documents. The supported document types vary by region:

    • Worldwide: Passport Book, Passport Card
    • United States: Driver License, Identification Card, Residency Permit (Green card), Social Security Card, Military ID
    • Europe: Driver License, Identification Card, Residency Permit
    • India: Driver License, PAN Card, Aadhaar Card
    • Canada: Driver License, Identification Card, Residency Permit (Maple Card)
    • Australia: Driver License, Photo Card, Key-pass ID (including digital version)
  3. Use the Document Intelligence prebuilt-receipt model

    main

    The prebuilt-receipt model is used to extract structured information from various types of receipts. It supports different receipt categories including thermal receipts (retail, meals, parking), hotel receipts, credit card receipts, gas receipts, and parking receipts. Use the Model ID prebuilt-receipt when making API calls to trigger this specific extraction logic.

    Model ID: prebuilt-receipt
  4. Understand the structure of rental property details in Schedule E

    main

    The model extracts an array of objects under the key IncomeOrLossFromRentalRealEstatePropertyDetails. Each object in this array represents a specific property or royalty entry and contains the following nested fields:

    • PhysicalAddress (address type)
    • FairRentalDays (number)
    • PersonalUseDays (number)
    • QJV (boolean)
    • Various box values (Box3 through Box22, all number type)
    {
      "IncomeOrLossFromRentalRealEstatePropertyDetails": [
        {
          "PhysicalAddress": { "street": "...", "city": "..." },
          "FairRentalDays": 1,
          "PersonalUseDays": 364,
          "QJV": true,
          "Box3": 123456
        }
      ]
    }
  5. Understand the Box17 array structure for State Specific FUTA details

    main

    The Box17 field is an array of objects used to capture State Specific FUTA (Federal Unemployment Tax Act) details. Each object in the array contains the following properties:

    • NameOfState (string): The name of the state.
    • TaxableWage (number): The taxable wage for that state.
    • StateExperienceRatePeriodFromDate (date): The start date of the state experience rate period.
    • StateExperienceRatePeriodToDate (date): The end date of the state experience rate period.
    • StateExperienceRate (number): The state experience rate.
    • BoxE (number): Value from Box E.
    • BoxF (number): Value from Box F.
    • BoxG (number): Value from Box G.
    • ContributionPaid (number): The contribution paid.
  6. Understand Document Analysis models

    main

    Azure Document Intelligence provides document analysis models to extract text and structure from forms. Use these models when you need to convert unstructured documents into structured, business-ready content.

    • Layout: Use this to extract text, tables, and the overall document structure.
    • Read: Use this specifically for extracting printed and handwritten text.
  7. Use Custom models for specialized extraction

    main

    When prebuilt models do not meet your requirements, you can use custom models to handle specific document types or layouts:

    • Custom template: Best for extracting data from documents with static, unchanging layouts.
    • Custom neural: Best for extracting data from mixed-type documents where layouts may vary.
    • Custom composed: Allows you to extract data using a collection of different models.
    • Custom classifier: Used to identify and categorize document types (classes) before routing them to the appropriate extraction model.
  8. Poll for analysis results using getLongRunningPoller

    main

    Since document analysis is an asynchronous operation, you must use getLongRunningPoller to create a poller from the initial response. Use poller.pollUntilDone() to wait for the final result.

    import {
      getLongRunningPoller,
      AnalyzeResultOperationOutput,
      isUnexpected,
    } from "@azure-rest/ai-document-intelligence";
    
    if (isUnexpected(initialResponse)) {
      throw initialResponse.body.error;
    }
    const poller = await getLongRunningPoller(client, initialResponse);
    const result = (await poller.pollUntilDone()).body as AnalyzeResultOperationOutput;
    console.log(result);