Skill

Analyze Documents with Azure AI Document Intelligence

Java skill for extracting layout, tables, and fields from documents with Azure AI Document Intelligence, plus custom models.

Works with azure

Maintainer of this project? Claim this page to edit the listing.


91
Spark score
out of 100
Updated 21 days ago
Version 13.12.0

Add to Favorites

Why it matters

Leverage Azure AI Document Intelligence (formerly Form Recognizer) SDK for Java to extract structured data from various document types, including invoices, receipts, and custom forms. Automate data extraction and analysis within your Java applications.

Outcomes

What it gets done

01

Extract text, tables, and selection marks using the prebuilt-layout model.

02

Parse key-value pairs and structured data from general documents.

03

Analyze and extract specific fields from receipts and invoices.

04

Build and utilize custom document analysis models for specialized needs.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/ag-azure-ai-formrecognizer-java | bash

Overview

Azure Document Intelligence (Form Recognizer) SDK for Java

Java skill for Azure AI Document Intelligence (Form Recognizer), covering prebuilt-model extraction (layout, receipt, invoice, ID, tax forms) with typed fields and confidence scores, plus custom model training, composition, and document classification. Use when extracting structured data from documents in Java, or training a custom extraction/classification model for a document type prebuilt models don't cover.

What it does

This skill builds document analysis applications with the Azure AI Document Intelligence (Form Recognizer) SDK for Java (com.azure:azure-ai-formrecognizer, version 4.2.0-beta.1), covering both prebuilt-model extraction and custom model training. Two clients are created via their respective builders: DocumentAnalysisClient for running analysis, and DocumentModelAdministrationClient for managing custom models - both authenticated with an AzureKeyCredential or DefaultAzureCredentialBuilder. Seven prebuilt models are available out of the box: prebuilt-layout (text, tables, selection marks), prebuilt-document (general key-value pairs), prebuilt-receipt, prebuilt-invoice, prebuilt-businessCard, prebuilt-idDocument (passports, licenses), and prebuilt-tax.us.w2. Analysis runs as a long-running operation via beginAnalyzeDocument/beginAnalyzeDocumentFromUrl, polled to a final AnalyzeResult with SyncPoller. Layout extraction walks each DocumentPage for lines, selection marks (checkboxes, with state and confidence), and DocumentTables (row/column counts and per-cell content); receipt/invoice extraction pulls specific typed fields (e.g. MerchantName as a string, TransactionDate as a date, Items as a list of nested field maps) each with a confidence score; and general document analysis returns key-value pairs directly. For custom needs, DocumentModelAdministrationClient.beginBuildDocumentModel trains a model from a labeled blob-storage training set (with a DocumentModelBuildMode), beginComposeDocumentModel combines multiple existing models into one routed model, and models can be listed, fetched, deleted, and checked against account resource limits (getResourceDetails). A separate document-classification workflow (beginBuildDocumentClassifier, mapping named document types to blob-storage source prefixes, then beginClassifyDocumentFromUrl) routes an incoming document to the correct type with a confidence score before further processing. Errors surface as HttpResponseException with status code and message.

When to use - and when NOT to

Use this skill when extracting structured data from documents in a Java application - invoices, receipts, business cards, ID documents, tax forms, or general forms with key-value pairs and tables - or when building a custom extraction model for a document type prebuilt models don't cover, or classifying incoming documents by type before routing them. It is not needed for documents that already arrive as structured data (JSON/XML) - this SDK is specifically for extracting structure from unstructured or semi-structured documents like scanned PDFs and images.

Inputs and outputs

Input is a document file, byte stream, or URL (PDF, image) to analyze, optionally against a custom or classifier model ID. Output is an AnalyzeResult containing pages, lines, tables, selection marks, key-value pairs, or typed extracted fields with per-field confidence scores, or a document type classification with confidence.

Integrations

Built on the Azure AI Document Intelligence (Form Recognizer) Java SDK, authenticated via AzureKeyCredential or Azure Identity's DefaultAzureCredentialBuilder, with custom model and classifier training data sourced from Azure Blob Storage.

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-ai-formrecognizer</artifactId>
    <version>4.2.0-beta.1</version>
</dependency>

Who it's for

Java developers building document processing pipelines - invoice/receipt extraction, form digitization, ID verification, or document classification and routing - who need structured field extraction without training a computer-vision model from scratch.

Source README

Azure Document Intelligence (Form Recognizer) SDK for Java

Build document analysis applications using the Azure AI Document Intelligence SDK for Java.

Installation

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-ai-formrecognizer</artifactId>
    <version>4.2.0-beta.1</version>
</dependency>

Client Creation

DocumentAnalysisClient

import com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClient;
import com.azure.ai.formrecognizer.documentanalysis.DocumentAnalysisClientBuilder;
import com.azure.core.credential.AzureKeyCredential;

DocumentAnalysisClient client = new DocumentAnalysisClientBuilder()
    .credential(new AzureKeyCredential("{key}"))
    .endpoint("{endpoint}")
    .buildClient();

DocumentModelAdministrationClient

import com.azure.ai.formrecognizer.documentanalysis.administration.DocumentModelAdministrationClient;
import com.azure.ai.formrecognizer.documentanalysis.administration.DocumentModelAdministrationClientBuilder;

DocumentModelAdministrationClient adminClient = new DocumentModelAdministrationClientBuilder()
    .credential(new AzureKeyCredential("{key}"))
    .endpoint("{endpoint}")
    .buildClient();

With DefaultAzureCredential

import com.azure.identity.DefaultAzureCredentialBuilder;

DocumentAnalysisClient client = new DocumentAnalysisClientBuilder()
    .endpoint("{endpoint}")
    .credential(new DefaultAzureCredentialBuilder().build())
    .buildClient();

Prebuilt Models

Model ID Purpose
prebuilt-layout Extract text, tables, selection marks
prebuilt-document General document with key-value pairs
prebuilt-receipt Receipt data extraction
prebuilt-invoice Invoice field extraction
prebuilt-businessCard Business card parsing
prebuilt-idDocument ID document (passport, license)
prebuilt-tax.us.w2 US W2 tax forms

Core Patterns

Extract Layout

import com.azure.ai.formrecognizer.documentanalysis.models.*;
import com.azure.core.util.BinaryData;
import com.azure.core.util.polling.SyncPoller;
import java.io.File;

File document = new File("document.pdf");
BinaryData documentData = BinaryData.fromFile(document.toPath());

SyncPoller<OperationResult, AnalyzeResult> poller = 
    client.beginAnalyzeDocument("prebuilt-layout", documentData);

AnalyzeResult result = poller.getFinalResult();

// Process pages
for (DocumentPage page : result.getPages()) {
    System.out.printf("Page %d: %.2f x %.2f %s%n",
        page.getPageNumber(),
        page.getWidth(),
        page.getHeight(),
        page.getUnit());
    
    // Lines
    for (DocumentLine line : page.getLines()) {
        System.out.println("Line: " + line.getContent());
    }
    
    // Selection marks (checkboxes)
    for (DocumentSelectionMark mark : page.getSelectionMarks()) {
        System.out.printf("Checkbox: %s (confidence: %.2f)%n",
            mark.getSelectionMarkState(),
            mark.getConfidence());
    }
}

// Tables
for (DocumentTable table : result.getTables()) {
    System.out.printf("Table: %d rows x %d columns%n",
        table.getRowCount(),
        table.getColumnCount());
    
    for (DocumentTableCell cell : table.getCells()) {
        System.out.printf("Cell[%d,%d]: %s%n",
            cell.getRowIndex(),
            cell.getColumnIndex(),
            cell.getContent());
    }
}

Analyze from URL

String documentUrl = "https://example.com/invoice.pdf";

SyncPoller<OperationResult, AnalyzeResult> poller = 
    client.beginAnalyzeDocumentFromUrl("prebuilt-invoice", documentUrl);

AnalyzeResult result = poller.getFinalResult();

Analyze Receipt

SyncPoller<OperationResult, AnalyzeResult> poller = 
    client.beginAnalyzeDocumentFromUrl("prebuilt-receipt", receiptUrl);

AnalyzeResult result = poller.getFinalResult();

for (AnalyzedDocument doc : result.getDocuments()) {
    Map<String, DocumentField> fields = doc.getFields();
    
    DocumentField merchantName = fields.get("MerchantName");
    if (merchantName != null && merchantName.getType() == DocumentFieldType.STRING) {
        System.out.printf("Merchant: %s (confidence: %.2f)%n",
            merchantName.getValueAsString(),
            merchantName.getConfidence());
    }
    
    DocumentField transactionDate = fields.get("TransactionDate");
    if (transactionDate != null && transactionDate.getType() == DocumentFieldType.DATE) {
        System.out.printf("Date: %s%n", transactionDate.getValueAsDate());
    }
    
    DocumentField items = fields.get("Items");
    if (items != null && items.getType() == DocumentFieldType.LIST) {
        for (DocumentField item : items.getValueAsList()) {
            Map<String, DocumentField> itemFields = item.getValueAsMap();
            System.out.printf("Item: %s, Price: %.2f%n",
                itemFields.get("Name").getValueAsString(),
                itemFields.get("Price").getValueAsDouble());
        }
    }
}

General Document Analysis

SyncPoller<OperationResult, AnalyzeResult> poller = 
    client.beginAnalyzeDocumentFromUrl("prebuilt-document", documentUrl);

AnalyzeResult result = poller.getFinalResult();

// Key-value pairs
for (DocumentKeyValuePair kvp : result.getKeyValuePairs()) {
    System.out.printf("Key: %s => Value: %s%n",
        kvp.getKey().getContent(),
        kvp.getValue() != null ? kvp.getValue().getContent() : "null");
}

Custom Models

Build Custom Model

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

String blobContainerUrl = "{SAS_URL_of_training_data}";
String prefix = "training-docs/";

SyncPoller<OperationResult, DocumentModelDetails> poller = adminClient.beginBuildDocumentModel(
    blobContainerUrl,
    DocumentModelBuildMode.TEMPLATE,
    prefix,
    new BuildDocumentModelOptions()
        .setModelId("my-custom-model")
        .setDescription("Custom invoice model"),
    Context.NONE);

DocumentModelDetails model = poller.getFinalResult();

System.out.println("Model ID: " + model.getModelId());
System.out.println("Created: " + model.getCreatedOn());

model.getDocumentTypes().forEach((docType, details) -> {
    System.out.println("Document type: " + docType);
    details.getFieldSchema().forEach((field, schema) -> {
        System.out.printf("  Field: %s (%s)%n", field, schema.getType());
    });
});

Analyze with Custom Model

SyncPoller<OperationResult, AnalyzeResult> poller = 
    client.beginAnalyzeDocumentFromUrl("my-custom-model", documentUrl);

AnalyzeResult result = poller.getFinalResult();

for (AnalyzedDocument doc : result.getDocuments()) {
    System.out.printf("Document type: %s (confidence: %.2f)%n",
        doc.getDocType(),
        doc.getConfidence());
    
    doc.getFields().forEach((name, field) -> {
        System.out.printf("Field '%s': %s (confidence: %.2f)%n",
            name,
            field.getContent(),
            field.getConfidence());
    });
}

Compose Models

List<String> modelIds = Arrays.asList("model-1", "model-2", "model-3");

SyncPoller<OperationResult, DocumentModelDetails> poller = 
    adminClient.beginComposeDocumentModel(
        modelIds,
        new ComposeDocumentModelOptions()
            .setModelId("composed-model")
            .setDescription("Composed from multiple models"));

DocumentModelDetails composedModel = poller.getFinalResult();

Manage Models

// List models
PagedIterable<DocumentModelSummary> models = adminClient.listDocumentModels();
for (DocumentModelSummary summary : models) {
    System.out.printf("Model: %s, Created: %s%n",
        summary.getModelId(),
        summary.getCreatedOn());
}

// Get model details
DocumentModelDetails model = adminClient.getDocumentModel("model-id");

// Delete model
adminClient.deleteDocumentModel("model-id");

// Check resource limits
ResourceDetails resources = adminClient.getResourceDetails();
System.out.printf("Models: %d / %d%n",
    resources.getCustomDocumentModelCount(),
    resources.getCustomDocumentModelLimit());

Document Classification

Build Classifier

Map<String, ClassifierDocumentTypeDetails> docTypes = new HashMap<>();
docTypes.put("invoice", new ClassifierDocumentTypeDetails()
    .setAzureBlobSource(new AzureBlobContentSource(containerUrl).setPrefix("invoices/")));
docTypes.put("receipt", new ClassifierDocumentTypeDetails()
    .setAzureBlobSource(new AzureBlobContentSource(containerUrl).setPrefix("receipts/")));

SyncPoller<OperationResult, DocumentClassifierDetails> poller = 
    adminClient.beginBuildDocumentClassifier(docTypes,
        new BuildDocumentClassifierOptions().setClassifierId("my-classifier"));

DocumentClassifierDetails classifier = poller.getFinalResult();

Classify Document

SyncPoller<OperationResult, AnalyzeResult> poller = 
    client.beginClassifyDocumentFromUrl("my-classifier", documentUrl, Context.NONE);

AnalyzeResult result = poller.getFinalResult();

for (AnalyzedDocument doc : result.getDocuments()) {
    System.out.printf("Classified as: %s (confidence: %.2f)%n",
        doc.getDocType(),
        doc.getConfidence());
}

Error Handling

import com.azure.core.exception.HttpResponseException;

try {
    client.beginAnalyzeDocumentFromUrl("prebuilt-receipt", "invalid-url");
} catch (HttpResponseException e) {
    System.out.println("Status: " + e.getResponse().getStatusCode());
    System.out.println("Error: " + e.getMessage());
}

Environment Variables

FORM_RECOGNIZER_ENDPOINT=https://<resource>.cognitiveservices.azure.com/
FORM_RECOGNIZER_KEY=<your-api-key>

Trigger Phrases

  • "document intelligence Java"
  • "form recognizer SDK"
  • "extract text from PDF"
  • "OCR document Java"
  • "analyze invoice receipt"
  • "custom document model"
  • "document classification"

When to Use

This skill is applicable to execute the workflow or actions described in the overview.

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.