Get started with Form Recognizer client library SDKs or REST API
Get started with Azure Form Recognizer using the programming language of your choice. Azure Form Recognizer is a cloud-based Azure Applied AI Service that uses machine learning to extract key-value pairs, text, and tables from your documents. You can easily call Form Recognizer models by integrating our client library SDks into your workflows and applications. We recommend that you use the free service when you're learning the technology. Remember that the number of free pages is limited to 500 per month.
To learn more about Form Recognizer features and development options, visit our Overview page.
Important
This quickstart targets Azure Form Recognizer REST API version 2.1 using cURL to execute REST API calls.
- For simplicity, the code in this article uses synchronous methods and unsecured credentials storage.
Reference documentation | Library source code | Package (NuGet) | Samples
In this quickstart, you'll use the following APIs to extract structured data from forms and documents:
Prerequisites
Azure subscription - Create one for free.
The current version of Visual Studio IDE.
A Cognitive Services or Form Recognizer resource. Once you have your Azure subscription, create a single-service or multi-service Form Recognizer resource in the Azure portal to get your key and endpoint. You can use the free pricing tier (
F0) to try the service, and upgrade later to a paid tier for production.Tip
Create a Cognitive Services resource if you plan to access multiple cognitive services under a single endpoint/key. For Form Recognizer access only, create a Form Recognizer resource. Please note that you'll need a single-service resource if you intend to use Azure Active Directory authentication.
After your resource deploys, click Go to resource. You need the key and endpoint from the resource you create to connect your application to the Form Recognizer API. You'll paste your key and endpoint into the code below later in the quickstart:
Set up
Start Visual Studio 2019.
On the start page, choose Create a new project.
On the Create a new project page, enter console in the search box. Choose the Console Application template, then choose Next.
In the Configure your new project dialog window, enter
formRecognizer_quickstartin the Project name box. Then choose Next.
In the Additional information dialog window, select .NET 5.0 (Current), and then select Create.
Install the client library with NuGet
Right-click on your formRecognizer_quickstart project and select Manage NuGet Packages... .
Select the Browse tab and type Azure.AI.FormRecognizer.
Select version 3.1.1 from the dropdown menu and select Install.
Build your application
To interact with the Form Recognizer service, you'll need to create an instance of the FormRecognizerClient class. To do so, you'll create an AzureKeyCredential with your key and a FormRecognizerClient instance with the AzureKeyCredential and your Form Recognizer endpoint.
Open the Program.cs file.
Include the following using directives:
using System;
using Azure;
using Azure.AI.FormRecognizer;
using Azure.AI.FormRecognizer.Models;
using System.Threading.Tasks;
- Set your
endpointandkeyenvironment variables and create yourAzureKeyCredentialandFormRecognizerClientinstance:
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);
Delete the line,
Console.Writeline("Hello World!");, and add one of the Try It code samples to the Main method in the Program.cs file:
Select a code sample to copy and paste into your application's Main method:
Important
Remember to remove the key from your code when you're done, and never post it publicly. For production, use secure methods to store and access your credentials. See the Cognitive Services [security](.(/azure/cognitive-services/cognitive-services-security.md) article for more information.
Try it: Layout model
Extract text, selection marks, text styles, and table structures, along with their bounding region coordinates from documents.
- For this example, you'll need a form document file at a URI. You can use our sample form document for this quickstart.
- We've added the file URI value to the
formUrivariable. - To extract the layout from a given file at a URI, use the
StartRecognizeContentFromUriAsyncmethod.
Add the following code to your layout application Main method:
FormRecognizerClient recognizerClient = AuthenticateClient();
Task recognizeContent = RecognizeContent(recognizerClient);
Task.WaitAll(recognizeContent);
Add the following code below the Main method:
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}'.");
}
}
}
}
}
}
Try it: Prebuilt model
This sample demonstrates how to analyze data from certain types of common documents with pre-trained models, using an invoice as an example.
- For this example, we wll analyze an invoice document using a prebuilt model. You can use our sample invoice document for this quickstart.
- We've added the file URI value to the
invoiceUrivariable at the top of the Main method. - To analyze a given file at a URI, use the
StartRecognizeInvoicesFromUriAsyncmethod. - For simplicity, all the fields that the service returns are not shown here. To see the list of all supported fields and corresponding types, see our Invoice concept page.
Choose a prebuilt model
You are not limited to invoices—there are several prebuilt models to choose from, each of which has its own set of supported fields. The model to use for the analyze operation depends on the type of document to be analyzed. Here are the prebuilt models currently supported by the Form Recognizer service:
- Invoice: extracts text, selection marks, tables, fields, and key information from invoices.
- Receipt: extracts text and key information from receipts.
- ID document: extracts text and key information from driver licenses and international passports.
- Business-card: extracts text and key information from business cards.
Add the following code to your prebuilt invoice application Main method
FormRecognizerClient recognizerClient = AuthenticateClient();
Task analyzeinvoice = AnalyzeInvoice(recognizerClient, invoiceUrl);
Task.WaitAll(analyzeinvoice);
Add the following code below the Main method:
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}");
}
}
}
}
}
Run your application
Choose the green Start button next to formRecognizer_quickstart to build and run your program, or press F5.
Congratulations! In this quickstart, you used the Form Recognizer C# SDK to analyze various forms and documents in different ways. Next, explore the reference documentation to learn about Form Recognizer API in more depth.
Next steps
Important
This quickstart targets Azure Form Recognizer version 2.1.
Reference documentation | Library source code | Package (Maven) | Samples
In this quickstart, you'll use the following APIs to extract structured data from forms and documents:
Prerequisites
- Azure subscription - Create one for free.
- A Java Development Kit (JDK), version 8 or later
- A Cognitive Services or Form Recognizer resource. Once you have your Azure subscription, create a single-service or multi-service Form Recognizer resource in the Azure portal to get your key and endpoint. You can use the free pricing tier (
F0) to try the service, and upgrade later to a paid tier for production.
Tip
Create a Cognitive Services resource if you plan to access multiple cognitive services under a single endpoint/key. For Form Recognizer access only, create a Form Recognizer resource. Please note that you'lll need a single-service resource if you intend to use Azure Active Directory authentication.
After your resource deploys, click Go to resource. You need the key and endpoint from the resource you create to connect your application to the Form Recognizer API. You'll paste your key and endpoint into the code below later in the quickstart:
Set up
Create a new Gradle project
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app called form-recognizer-app, and navigate to it.
mkdir form-recognizer-app && form-recognizer-app
Run the
gradle initcommand from your working directory. This command will create essential build files for Gradle, including build.gradle.kts, which is used at runtime to create and configure your application.gradle init --type basicWhen prompted to choose a DSL, select Kotlin.
Accept the default project name (form-recognizer-app)
Install the client library
This quickstart uses the Gradle dependency manager. You can find the client library and information for other dependency managers on the Maven Central Repository.
In your project's build.gradle.kts file, include the client library as an implementation statement, along with the required plugins and settings.
plugins {
java
application
}
application {
mainClass.set("FormRecognizer")
}
repositories {
mavenCentral()
}
dependencies {
implementation(group = "com.azure", name = "azure-ai-formrecognizer", version = "3.1.1")
}
Create a Java file
From your working directory, run the following command:
mkdir -p src/main/java
You will create the following directory structure:
Navigate to the Java directory and create a file called FormRecognizer.java. Open it in your preferred editor or IDE and add the following package declaration and import statements:
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;
Select a code sample to copy and paste into your application's main method:
Important
Remember to remove the key from your code when you're done, and never post it publicly. For production, use secure methods to store and access your credentials. See the Cognitive Services security article for more information.
Try it: Layout model
Extract text, selection marks, text styles, and table structures, along with their bounding region coordinates from documents.
- For this example, you'll need a form document file at a URI. You can use our sample form document for this quickstart.
- To analyze a given file at a URI, you'll use the
beginRecognizeContentFromUrlmethod. - We've added the file URI value to the
formUrlvariable in the main method.
Update your application's FormRecognizer class, with the following code (be certain to update the key and endpoint variables with values from your Form Recognizer instance in the Azure portal):
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();
});
});
}
Try it: Prebuilt model
This sample demonstrates how to analyze data from certain types of common documents with pre-trained models, using an invoice as an example.
- For this example, we wll analyze an invoice document using a prebuilt model. You can use our sample invoice document for this quickstart.
- To analyze a given file at a URI, you'll use the
beginRecognizeInvoicesFromUrl. - We've added the file URI value to the
invoiceUrlvariable in the main method. - For simplicity, all the fields that the service returns are not shown here. To see the list of all supported fields and corresponding types, see our Invoice concept page.
Choose a prebuilt model
You are not limited to invoices—there are several prebuilt models to choose from, each of which has its own set of supported fields. The model to use for the analyze operation depends on the type of document to be analyzed. Here are the prebuilt models currently supported by the Form Recognizer service:
- Invoice: extracts text, selection marks, tables, fields, and key information from invoices.
- Receipt: extracts text and key information from receipts.
- ID document: extracts text and key information from driver licenses and international passports.
- Business-card: extracts text and key information from business cards.
Update your application's FormRecognizer class, with the following code (be certain to update the key and endpoint variables with values from your Form Recognizer instance in the Azure portal):
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());
}
}
}
}
Build and run your application
Navigate back to your main project directory—form-recognizer-app.
- Build your application with the
buildcommand:
gradle build
- Run your application with the
runcommand:
gradle run
Congratulations! In this quickstart, you used the Form Recognizer Java SDK to analyze various forms and documents in different ways. Next, explore the reference documentation to learn about Form Recognizer API in more depth.
Next steps
Important
This quickstart targets Azure Form Recognizer REST API v2.1.
Reference documentation | Library source code | Package (npm) | Samples
In this quickstart, you'll use the following APIs to extract structured data from forms and documents:
Prerequisites
Azure subscription - Create one for free.
The latest version of Visual Studio Code or your preferred IDE.
The latest LTS version of Node.js
A Cognitive Services or Form Recognizer resource. Once you have your Azure subscription, create a single-service or multi-service Form Recognizer resource in the Azure portal to get your key and endpoint. You can use the free pricing tier (
F0) to try the service, and upgrade later to a paid tier for production.Tip
Create a Cognitive Services resource if you plan to access multiple cognitive services under a single endpoint/key. For Form Recognizer access only, create a Form Recognizer resource. Please note that you'lll need a single-service resource if you intend to use Azure Active Directory authentication.
After your resource deploys, select Go to resource. You need the key and endpoint from the resource you create to connect your application to the Form Recognizer API. You'll paste your key and endpoint into the code below later in the quickstart:
Set up
Create a new Node.js application. In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.
mkdir form-recognizer-app && cd form-recognizer-appRun the
npm initcommand to create a node application with apackage.jsonfile.npm initInstall the
ai-form-recognizerclient library npm package:npm install @azure/ai-form-recognizerYour app's
package.jsonfile will be updated with the dependencies.Create a file named
index.js, open it, and import the following libraries:const { FormRecognizerClient, AzureKeyCredential } = require("@azure/ai-form-recognizer");Create variables for your resource's Azure endpoint and key:
const key = "PASTE_YOUR_FORM_RECOGNIZER_KEY_HERE"; const endpoint = "PASTE_YOUR_FORM_RECOGNIZER_ENDPOINT_HERE";At this point, your JavaScript application should contain the following lines of code:
const { FormRecognizerClient, AzureKeyCredential } = require("@azure/ai-form-recognizer"); const endpoint = "PASTE_YOUR_FORM_RECOGNIZER_ENDPOINT_HERE"; const key = "PASTE_YOUR_FORM_RECOGNIZER_KEY_HERE";
Select a code sample to copy and paste into your application:
Important
Remember to remove the key from your code when you're done, and never post it publicly. For production, use secure methods to store and access your credentials. See our Cognitive Services [security]((/azure/cognitive-services/cognitive-services-security.md) article for more information.
Try it: Layout model
- For this example, you'll need a form document file at a URI. You can use our sample form document for this quickstart.
- We've added the file URI value to the
formUrlvariable near the top of the file. - To analyze a given file at a URI, you'll use the
beginRecognizeContentmethod.
Add the following code to your layout application on the line below the key variable
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);
});
Try it: Prebuilt model
This sample demonstrates how to analyze data from certain types of common documents with pre-trained models, using an invoice as an example. See our prebuilt concept page for a complete list of invoice fields
- For this example, we wll analyze an invoice document using a prebuilt model. You can use our sample invoice document for this quickstart.
- We've added the file URI value to the
invoiceUrlvariable at the top of the file. - To analyze a given file at a URI, you'll use the
beginRecognizeInvoicesmethod. - For simplicity, all the fields that the service returns are not shown here. To see the list of all supported fields and corresponding types, see our Invoice concept page.
Choose a prebuilt model
You are not limited to invoices—there are several prebuilt models to choose from, each of which has its own set of supported fields. The model to use for the analyze operation depends on the type of document to be analyzed. Here are the prebuilt models currently supported by the Form Recognizer service:
- Invoice: extracts text, selection marks, tables, fields, and key information from invoices.
- Receipt: extracts text and key information from receipts.
- ID document: extracts text and key information from driver licenses and international passports.
- Business-card: extracts text and key information from business cards.
Add the following code to your prebuilt invoice application below the key variable
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);
});
Congratulations! In this quickstart, you used the Form Recognizer JavaScript SDK to analyze various forms in different ways. Next, explore the reference documentation to learn moe about Form Recognizer v3.0 API.
Next steps
Important
This quickstart targets Azure Form Recognizer REST API v2.1.
Reference documentation | Library source code | Package (PyPi) | Samples
In this quickstart, you'll use the following APIs to extract structured data from forms and documents:
Prerequisites
Azure subscription - Create one for free
-
- Your Python installation should include pip. You can check if you have pip installed by running
pip --versionon the command line. Get pip by installing the latest version of Python.
- Your Python installation should include pip. You can check if you have pip installed by running
A Cognitive Services or Form Recognizer resource. Once you have your Azure subscription, create a single-service or multi-service Form Recognizer resource in the Azure portal to get your key and endpoint. You can use the free pricing tier (
F0) to try the service, and upgrade later to a paid tier for production.Tip
Create a Cognitive Services resource if you plan to access multiple cognitive services under a single endpoint/key. For Form Recognizer access only, create a Form Recognizer resource. Please note that you'lll need a single-service resource if you intend to use Azure Active Directory authentication.
After your resource deploys, select Go to resource. You need the key and endpoint from the resource you create to connect your application to the Form Recognizer API. You'll paste your key and endpoint into the code below later in the quickstart:
Set up
Open a terminal window in your local environment and install the Azure Form Recognizer client library for Python with pip:
pip install azure-ai-formrecognizer
Create a new Python application
Create a new Python application called form_recognizer_quickstart.py in your preferred editor or IDE. Then import the following libraries:
import os
from azure.ai.formrecognizer import FormRecognizerClient
from azure.core.credentials import AzureKeyCredential
Create variables for your Azure resource endpoint and key
endpoint = "YOUR_FORM_RECOGNIZER_ENDPOINT"
key = "YOUR_FORM_RECOGNIZER_KEY"
At this point, your Python application should contain the following lines of code:
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"
Select a code sample to copy and paste into your application:
Important
Remember to remove the key from your code when you're done, and never post it publicly. For production, use secure methods to store and access your credentials. See the Cognitive Services security article for more information.
Try it: Layout model
- For this example, you'll need a form document file at a URI. You can use our sample form document for this quickstart.
- We've added the file URI value to the
formUrlvariable near the top of the file. - To analyze a given file at a URI, you'll use the
begin_recognize_content_from_urlmethod.
Add the following code to your layout application on the line below the key variable
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 form 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()
Try it: Prebuilt model
This sample demonstrates how to analyze data from certain types of common documents with pre-trained models, using an invoice as an example. See our prebuilt concept page for a complete list of invoice fields
- For this example, we wll analyze an invoice document using a prebuilt model. You can use our sample invoice document for this quickstart.
- We've added the file URI value to the ``formUrl` variable at the top of the file.
- To analyze a given file at a URI, you'll use the ``begin_recognize_invoices_from_url` method.
- For simplicity, all the fields that the service returns are not shown here. To see the list of all supported fields and corresponding types, see our Invoice concept page.
Choose a prebuilt model
You are not limited to invoices—there are several prebuilt models to choose from, each of which has its own set of supported fields. The model to use for the analyze operation depends on the type of document to be analyzed. Here are the prebuilt models currently supported by the Form Recognizer service:
- Invoice: extracts text, selection marks, tables, fields, and key information from invoices.
- Receipt: extracts text and key information from receipts.
- ID document: extracts text and key information from driver licenses and international passports.
- Business-card: extracts text and key information from business cards.
Add the following code to your prebuilt invoice application below the key variable
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()
Run your application
Navigate to the folder where you have your form_recognizer_quickstart.py file.
Type the following command in your terminal:
python form_recognizer_quickstart.py
Congratulations! In this quickstart, you used the Form Recognizer Python SDK to analyze various forms in different ways. Next, explore the reference documentation to learn moe about Form Recognizer v3.0 API.
Next steps
Important
This quickstart targets Azure Form Recognizer REST API version 2.1 using cURL to execute REST API calls.
| Form Recognizer REST API | Azure REST API reference |
In this quickstart, you'll use the following APIs to extract structured data from forms and documents:
Prerequisites
Azure subscription - Create one for free
cURL installed.
PowerShell version 6.0+, or a similar command-line application.
A Cognitive Services or Form Recognizer resource. Once you have your Azure subscription, create a single-service or multi-service Form Recognizer resource in the Azure portal to get your key and endpoint. You can use the free pricing tier (
F0) to try the service, and upgrade later to a paid tier for production.Tip
Create a Cognitive Services resource if you plan to access multiple cognitive services under a single endpoint/key. For Form Recognizer access only, create a Form Recognizer resource. Please note that you'lll need a single-service resource if you intend to use Azure Active Directory authentication.
After your resource deploys, select Go to resource. You need the key and endpoint from the resource you create to connect your application to the Form Recognizer API. You'll paste your key and endpoint into the code below later in the quickstart:
Select a code sample to copy and paste into your application:
Important
Remember to remove the key from your code when you're done, and never post it publicly. For production, use secure methods to store and access your credentials. See the Cognitive Services security article for more information.
Try it: Layout model
- For this example, you'll need a form document file at a URI. You can use our sample form document for this quickstart.
- Replace
{endpoint}with the endpoint that you obtained with your Form Recognizer subscription. - Replace
{key}with the key you copied from the previous step. - Replace
\"{your-document-url}with a sample form URL:
https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf
Request
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
You'll receive a 202 (Success) response that includes an Operation-Location header. The value of this header contains a result ID that you can use to query the status of the asynchronous operation and get the results:
https://cognitiveservice/formrecognizer/v2.1/layout/analyzeResults/{resultId}.
In the following example, as part of the URL, the string after analyzeResults/ is the result ID.
https://cognitiveservice/formrecognizer/v2/layout/analyzeResults/54f0b076-4e38-43e5-81bd-b85b8835fdfb
Get layout results
After you've called the Analyze Layout API, you call the Get Analyze Layout Result API to get the status of the operation and the extracted data. Before you run the command, make these changes:
- Replace
{endpoint}with the endpoint that you obtained with your Form Recognizer subscription. - Replace
{key}with the key you copied from the previous step. - Replace
{resultId}with the result ID from the previous step.
Request
curl -v -X GET "https://{endpoint}/formrecognizer/v2.1/layout/analyzeResults/{resultId}" -H "Ocp-Apim-Subscription-Key: {key}"
Examine the results
You'll receive a 200 (success) response with JSON content.
See the following invoice image and its corresponding JSON output.
- The
"readResults"node contains every line of text with its respective bounding box placement on the page. - The
selectionMarksnode shows every selection mark (checkbox, radio mark) and whether its status is "selected" or "unselected". - The
"pageResults"section includes the tables extracted. For each table, the text, row, and column index, row and column spanning, bounding box, and more are extracted.
Response body
You can view the full sample output on GitHub.
Try it: Prebuilt model
- For this example, we wll analyze an invoice document using a prebuilt model. You can use our sample invoice document for this quickstart.
Choose a prebuilt model
You are not limited to invoices—there are several prebuilt models to choose from, each of which has its own set of supported fields. The model to use for the analyze operation depends on the type of document to be analyzed. Here are the prebuilt models currently supported by the Form Recognizer service:
- Invoice: extracts text, selection marks, tables, fields, and key information from invoices.
- Receipt: extracts text and key information from receipts.
- ID document: extracts text and key information from driver licenses and international passports.
- Business-card: extracts text and key information from business cards.
Before you run the command, make these changes:
Replace
{endpoint}with the endpoint that you obtained with your Form Recognizer subscription.Replace
{key}with the key you copied from the previous step.Replace
\"{your-document-url}with a sample invoice URL:https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf
Request
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
You'll receive a 202 (Success) response that includes an Operation-Location header. The value of this header contains a result ID that you can use to query the status of the asynchronous operation and get the results:
https://cognitiveservice/formrecognizer/v2.1/prebuilt/receipt/analyzeResults/{resultId}
In the following example, as part of the URL, the string after analyzeResults/ is the result ID:
https://cognitiveservice/formrecognizer/v2.1/prebuilt/invoice/analyzeResults/54f0b076-4e38-43e5-81bd-b85b8835fdfb
Get invoice results
After you've called the Analyze Invoice API, you call the Get Analyze Invoice Result API to get the status of the operation and the extracted data. Before you run the command, make these changes:
- Replace
{endpoint}with the endpoint that you obtained with your Form Recognizer key. You can find it on your Form Recognizer resource Overview tab. - Replace
{resultId}with the result ID from the previous step. - Replace
{key}with your key.
Request
curl -v -X GET "https://{endpoint}/formrecognizer/v2.1/prebuilt/invoice/analyzeResults/{resultId}" -H "Ocp-Apim-Subscription-Key: {key}"
Examine the response
You'll receive a 200 (Success) response with JSON output.
- The
"readResults"field contains every line of text that was extracted from the invoice. - The
"pageResults"includes the tables and selections marks extracted from the invoice. - The
"documentResults"field contains key/value information for the most relevant parts of the invoice.
See the Sample invoice document.
Response body
See the full sample output on GitHub.
Next steps
Congratulations! In this quickstart, you used the Form Recognizer REST API to analyze various forms in different ways. Next, explore the reference documentation to learn moe about Form Recognizer v3.0 API.
Povratne informacije
Pošalјite i prikažite povratne informacije za