Azure AI Document Intelligence Code Samples
repository·main·Indexed 42 days ago
https://github.com/azure-samples/document-intelligence-code-samplesComprehensive 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.
What's inside Azure AI Document Intelligence
- The Document Intelligence receipt model is a prebuilt model designed to extract information from receipts. As of the 2024-11-30 (GA) version, it identifies key receipt data points such as merchant information, transaction dates, and line items.
Use Prebuilt models for intelligent document processing
mainPrebuilt 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.
- Financial/Transaction:
Use the prebuilt-idDocument model
mainThe
prebuilt-idDocumentmodel 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)
Use the Document Intelligence prebuilt-receipt model
mainThe
prebuilt-receiptmodel 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 IDprebuilt-receiptwhen making API calls to trigger this specific extraction logic.Model ID: prebuilt-receiptUnderstand the structure of rental property details in Schedule E
mainThe 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(addresstype)FairRentalDays(number)PersonalUseDays(number)QJV(boolean)- Various box values (
Box3throughBox22, allnumbertype)
{ "IncomeOrLossFromRentalRealEstatePropertyDetails": [ { "PhysicalAddress": { "street": "...", "city": "..." }, "FairRentalDays": 1, "PersonalUseDays": 364, "QJV": true, "Box3": 123456 } ] }Understand the Box17 array structure for State Specific FUTA details
mainThe
Box17field 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.
Understand Document Analysis models
mainAzure 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.
Use the prebuilt-contract model
mainThe
prebuilt-contractmodel is a specialized Azure Document Intelligence model designed to extract key information from legal contracts. As of the 2024-11-30 (GA) release, it supports English (United States,en-US) and extracts structured fields including contract identifiers, parties, dates, and jurisdictions.Model ID: prebuilt-contractUse Custom models for specialized extraction
mainWhen 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.
Use the US Tax Form W-2 Prebuilt Model
mainAzure Document Intelligence provides a prebuilt model specifically for analyzing US W-2 tax forms. This model extracts key information such as employee and employer details, tax withholdings, and state/local tax information.
Model ID:
prebuilt-tax.us.w2Supported Languages and Locales:
- English (
en-US)
- English (
Use the US tax form 1099-B prebuilt model
mainThe Azure Document Intelligence prebuilt model for US tax form 1099-B allows for automated extraction of tax year, payer, recipient, transaction summaries, and individual transaction details.
Model ID:
prebuilt-tax.us.1099BSupported Language/Locale: English (
en-US)Model ID: prebuilt-tax.us.1099BPoll for analysis results using getLongRunningPoller
mainSince document analysis is an asynchronous operation, you must use
getLongRunningPollerto create a poller from the initial response. Usepoller.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);