Class DocumentAnalysisClient
This class provides a synchronous client to connect to the Form Recognizer Azure Cognitive Service.
This client provides synchronous methods to perform:
- Custom Document Analysis: Classification, extraction and analysis of data from forms and documents specific
to distinct business data and use cases. Use the custom trained model by passing its modelId into the
beginAnalyzeDocument(String, BinaryData)
method. - General Document Analysis: Extract text, tables, structure, and key-value pairs. Use general document model
provided by the Form Recognizer service by passing modelId="rebuilt-document" into the
beginAnalyzeDocument(String, BinaryData)
method. - Prebuilt Model Analysis: Analyze receipts, business cards, invoices, ID's, W2's and other documents with
supported prebuilt models. Use the prebuilt receipt model
provided by passing modelId="prebuilt-receipt" into the
beginAnalyzeDocument(String, BinaryData)
method. - Layout Analysis: Extract text, selection marks, and tables structures, along with their bounding box
coordinates, from forms and documents. Use the layout analysis model provided the service by passing
modelId="prebuilt-layout" into the
beginAnalyzeDocument(String, BinaryData)
method. - Polling and Callbacks: It includes mechanisms for polling the service to check the status of an analysis operation or registering callbacks to receive notifications when the analysis is complete.
This client also provides different methods based on inputs from a URL and inputs from a stream.
Note: This client only supports
DocumentAnalysisServiceVersion.V2022_08_31
and newer.
To use an older service version, FormRecognizerClient
and
FormTrainingClient
.
Service clients are the point of interaction for developers to use Azure Form Recognizer.
DocumentAnalysisClient
is the synchronous service client and
DocumentAnalysisAsyncClient
is the asynchronous service client.
The examples shown in this document use a credential object named DefaultAzureCredential for authentication, which is
appropriate for most scenarios, including local development and production environments. Additionally, we
recommend using
managed identity
for authentication in production environments.
You can find more information on different ways of authenticating and their corresponding credential types in the
Azure Identity documentation".
Sample: Construct a DocumentAnalysisAsyncClient with DefaultAzureCredential
The following code sample demonstrates the creation of a
DocumentAnalysisClient
, using
the `DefaultAzureCredentialBuilder` to configure it.
DocumentAnalysisClient documentAnalysisClient = new DocumentAnalysisClientBuilder() .endpoint("{endpoint}") .credential(new DefaultAzureCredentialBuilder().build()) .buildClient();
Further, see the code sample below to use
AzureKeyCredential
for client creation.
DocumentAnalysisClient documentAnalysisClient = new DocumentAnalysisClientBuilder() .credential(new AzureKeyCredential("{key}")) .endpoint("{endpoint}") .buildClient();
-
Method Summary
Modifier and TypeMethodDescriptioncom.azure.core.util.polling.SyncPoller<OperationResult,
AnalyzeResult> beginAnalyzeDocument
(String modelId, com.azure.core.util.BinaryData document) Analyzes data from documents using optical character recognition (OCR) using any of the prebuilt models or a custom-built analysis model.com.azure.core.util.polling.SyncPoller<OperationResult,
AnalyzeResult> beginAnalyzeDocument
(String modelId, com.azure.core.util.BinaryData document, AnalyzeDocumentOptions analyzeDocumentOptions, com.azure.core.util.Context context) Analyzes data from documents with optical character recognition (OCR) and semantic values from a given document using any of the prebuilt models or a custom-built analysis model.com.azure.core.util.polling.SyncPoller<OperationResult,
AnalyzeResult> beginAnalyzeDocumentFromUrl
(String modelId, String documentUrl) Analyzes data from documents with optical character recognition (OCR) and semantic values from a given document using any of the prebuilt models or a custom-built analysis model.com.azure.core.util.polling.SyncPoller<OperationResult,
AnalyzeResult> beginAnalyzeDocumentFromUrl
(String modelId, String documentUrl, AnalyzeDocumentOptions analyzeDocumentOptions, com.azure.core.util.Context context) Analyzes data from documents with optical character recognition (OCR) and semantic values from a given document using any of the prebuilt models or a custom-built analysis model.com.azure.core.util.polling.SyncPoller<OperationResult,
AnalyzeResult> beginClassifyDocument
(String classifierId, com.azure.core.util.BinaryData document) Classify a given document using a document classifier.com.azure.core.util.polling.SyncPoller<OperationResult,
AnalyzeResult> beginClassifyDocument
(String classifierId, com.azure.core.util.BinaryData document, com.azure.core.util.Context context) Classify a given document using a document classifier.com.azure.core.util.polling.SyncPoller<OperationResult,
AnalyzeResult> beginClassifyDocumentFromUrl
(String classifierId, String documentUrl) Classify a given document using a document classifier.com.azure.core.util.polling.SyncPoller<OperationResult,
AnalyzeResult> beginClassifyDocumentFromUrl
(String classifierId, String documentUrl, com.azure.core.util.Context context) Classify a given document using a document classifier.
-
Method Details
-
beginAnalyzeDocumentFromUrl
public com.azure.core.util.polling.SyncPoller<OperationResult,AnalyzeResult> beginAnalyzeDocumentFromUrl(String modelId, String documentUrl) Analyzes data from documents with optical character recognition (OCR) and semantic values from a given document using any of the prebuilt models or a custom-built analysis model.The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support
Code sample
Analyze a document using the URL of the document.
String documentUrl = "{document_url}"; String modelId = "{custom_trained_model_id}"; documentAnalysisClient.beginAnalyzeDocumentFromUrl(modelId, documentUrl).getFinalResult() .getDocuments().stream() .map(AnalyzedDocument::getFields) .forEach(documentFieldMap -> documentFieldMap.forEach((key, documentField) -> { System.out.printf("Field text: %s%n", key); System.out.printf("Field value data content: %s%n", documentField.getContent()); System.out.printf("Confidence score: %.2f%n", documentField.getConfidence()); }));
- Parameters:
modelId
- The unique model ID to be used. Use this to specify the custom model ID or prebuilt model ID. Prebuilt model IDs supported can be found heredocumentUrl
- The URL of the document to analyze.- Returns:
- A
SyncPoller
to poll the progress of the analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns anAnalyzeResult
. - Throws:
com.azure.core.exception.HttpResponseException
- If analyze operation fails and returns with anOperationStatus.FAILED
.IllegalArgumentException
- IfdocumentUrl
ormodelId
is null.
-
beginAnalyzeDocumentFromUrl
public com.azure.core.util.polling.SyncPoller<OperationResult,AnalyzeResult> beginAnalyzeDocumentFromUrl(String modelId, String documentUrl, AnalyzeDocumentOptions analyzeDocumentOptions, com.azure.core.util.Context context) Analyzes data from documents with optical character recognition (OCR) and semantic values from a given document using any of the prebuilt models or a custom-built analysis model.The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support
Code sample
Analyze a document using the URL of the document with configurable options.
String documentUrl = "{document_url}"; String modelId = "{custom_trained_model_id}"; documentAnalysisClient.beginAnalyzeDocumentFromUrl(modelId, documentUrl).getFinalResult() .getDocuments().stream() .map(AnalyzedDocument::getFields) .forEach(documentFieldMap -> documentFieldMap.forEach((key, documentField) -> { System.out.printf("Field text: %s%n", key); System.out.printf("Field value data content: %s%n", documentField.getContent()); System.out.printf("Confidence score: %.2f%n", documentField.getConfidence()); }));
- Parameters:
modelId
- The unique model ID to be used. Use this to specify the custom model ID or prebuilt model ID. Prebuilt model IDs supported can be found heredocumentUrl
- The source URL to the input document.analyzeDocumentOptions
- The additional configurableoptions
that may be passed when analyzing documents.context
- Additional context that is passed through the HTTP pipeline during the service call.- Returns:
- A
SyncPoller
to poll the progress of the analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns anAnalyzeResult
. - Throws:
com.azure.core.exception.HttpResponseException
- If analyze operation fails and returns with anOperationStatus.FAILED
.IllegalArgumentException
- IfdocumentUrl
ormodelId
is null.
-
beginAnalyzeDocument
public com.azure.core.util.polling.SyncPoller<OperationResult,AnalyzeResult> beginAnalyzeDocument(String modelId, com.azure.core.util.BinaryData document) Analyzes data from documents using optical character recognition (OCR) using any of the prebuilt models or a custom-built analysis model.The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Code sample
File document = new File("{local/file_path/fileName.jpg}"); String modelId = "{custom_trained_model_id}"; byte[] fileContent = Files.readAllBytes(document.toPath()); documentAnalysisClient.beginAnalyzeDocument(modelId, BinaryData.fromBytes(fileContent)) .getFinalResult() .getDocuments().stream() .map(AnalyzedDocument::getFields) .forEach(documentFieldMap -> documentFieldMap.forEach((key, documentField) -> { System.out.printf("Field text: %s%n", key); System.out.printf("Field value data content: %s%n", documentField.getContent()); System.out.printf("Confidence score: %.2f%n", documentField.getConfidence()); })); }
- Parameters:
modelId
- The unique model ID to be used. Use this to specify the custom model ID or prebuilt model ID. Prebuilt model IDs supported can be found heredocument
- The data of the document to analyze information from.- Returns:
- A
SyncPoller
that polls the of progress of analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns anAnalyzeResult
. - Throws:
com.azure.core.exception.HttpResponseException
- If analyze operation fails and returns with anOperationStatus.FAILED
.IllegalArgumentException
- Ifdocument
ormodelId
is null.
-
beginAnalyzeDocument
public com.azure.core.util.polling.SyncPoller<OperationResult,AnalyzeResult> beginAnalyzeDocument(String modelId, com.azure.core.util.BinaryData document, AnalyzeDocumentOptions analyzeDocumentOptions, com.azure.core.util.Context context) Analyzes data from documents with optical character recognition (OCR) and semantic values from a given document using any of the prebuilt models or a custom-built analysis model.The service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Code sample
Analyze a document with configurable options.
File document = new File("{local/file_path/fileName.jpg}"); String modelId = "{custom_trained_model_id}"; byte[] fileContent = Files.readAllBytes(document.toPath()); documentAnalysisClient.beginAnalyzeDocument(modelId, BinaryData.fromBytes(fileContent), new AnalyzeDocumentOptions().setPages(Arrays.asList("1", "3")), Context.NONE) .getFinalResult() .getDocuments().stream() .map(AnalyzedDocument::getFields) .forEach(documentFieldMap -> documentFieldMap.forEach((key, documentField) -> { System.out.printf("Field text: %s%n", key); System.out.printf("Field value data content: %s%n", documentField.getContent()); System.out.printf("Confidence score: %.2f%n", documentField.getConfidence()); }));
- Parameters:
modelId
- The unique model ID to be used. Use this to specify the custom model ID or prebuilt model ID. Prebuilt model IDs supported can be found heredocument
- The data of the document to analyze information from.analyzeDocumentOptions
- The additional configurableoptions
that may be passed when analyzing documents.context
- Additional context that is passed through the HTTP pipeline during the service call.- Returns:
- A
SyncPoller
that polls the of progress of analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns anAnalyzeResult
. - Throws:
com.azure.core.exception.HttpResponseException
- If analyze operation fails and returns with anOperationStatus.FAILED
.IllegalArgumentException
- Ifdocument
ormodelId
is null.IllegalArgumentException
- Ifdocument
length is null or unspecified. UseBinaryData.fromStream(InputStream, Long)
to create an instance of thedocument
from givenInputStream
with length.
-
beginClassifyDocumentFromUrl
public com.azure.core.util.polling.SyncPoller<OperationResult,AnalyzeResult> beginClassifyDocumentFromUrl(String classifierId, String documentUrl) Classify a given document using a document classifier. For more information on how to build a custom classifier model, seeThe service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support
Code sample
Analyze a document using the URL of the document with configurable options.
String documentUrl = "{file_source_url}"; String classifierId = "{custom_trained_classifier_id}"; documentAnalysisClient.beginClassifyDocumentFromUrl(classifierId, documentUrl) .getFinalResult() .getDocuments() .forEach(analyzedDocument -> System.out.printf("Doc Type: %s%n", analyzedDocument.getDocType()));
- Parameters:
classifierId
- The unique classifier ID to be used. Use this to specify the custom classifier ID. Prebuilt model IDs supported can be found heredocumentUrl
- The source URL to the input document.- Returns:
- A
SyncPoller
to poll the progress of the analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns anAnalyzeResult
. - Throws:
com.azure.core.exception.HttpResponseException
- If analyze operation fails and returns with anOperationStatus.FAILED
.IllegalArgumentException
- IfdocumentUrl
orclassifierId
is null.
-
beginClassifyDocument
public com.azure.core.util.polling.SyncPoller<OperationResult,AnalyzeResult> beginClassifyDocument(String classifierId, com.azure.core.util.BinaryData document) Classify a given document using a document classifier. For more information on how to build a custom classifier model, seeThe service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Code sample
File document = new File("{local/file_path/fileName.jpg}"); String classifierId = "{custom_trained_classifier_id}"; byte[] fileContent = Files.readAllBytes(document.toPath()); documentAnalysisClient.beginClassifyDocument(classifierId, BinaryData.fromBytes(fileContent)) .getFinalResult() .getDocuments() .forEach(analyzedDocument -> System.out.printf("Doc Type: %s%n", analyzedDocument.getDocType()));
- Parameters:
classifierId
- The unique classifier ID to be used. Use this to specify the custom classifier ID.document
- The data of the document to analyze information from.- Returns:
- A
SyncPoller
that polls the of progress of analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns anAnalyzeResult
. - Throws:
com.azure.core.exception.HttpResponseException
- If analyze operation fails and returns with anOperationStatus.FAILED
.IllegalArgumentException
- Ifdocument
orclassifierId
is null.
-
beginClassifyDocumentFromUrl
public com.azure.core.util.polling.SyncPoller<OperationResult,AnalyzeResult> beginClassifyDocumentFromUrl(String classifierId, String documentUrl, com.azure.core.util.Context context) Classify a given document using a document classifier. For more information on how to build a custom classifier model, seeThe service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support
Code sample
Analyze a document using the URL of the document with configurable options.
String documentUrl = "{file_source_url}"; String classifierId = "{custom_trained_classifier_id}"; documentAnalysisClient.beginClassifyDocumentFromUrl(classifierId, documentUrl, Context.NONE) .getFinalResult() .getDocuments() .forEach(analyzedDocument -> System.out.printf("Doc Type: %s%n", analyzedDocument.getDocType()));
- Parameters:
classifierId
- The unique classifier ID to be used. Use this to specify the custom classifier ID. Prebuilt model IDs supported can be found heredocumentUrl
- The source URL to the input document.context
- Additional context that is passed through the HTTP pipeline during the service call.- Returns:
- A
SyncPoller
to poll the progress of the analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns anAnalyzeResult
. - Throws:
com.azure.core.exception.HttpResponseException
- If analyze operation fails and returns with anOperationStatus.FAILED
.IllegalArgumentException
- IfdocumentUrl
orclassifierId
is null.
-
beginClassifyDocument
public com.azure.core.util.polling.SyncPoller<OperationResult,AnalyzeResult> beginClassifyDocument(String classifierId, com.azure.core.util.BinaryData document, com.azure.core.util.Context context) Classify a given document using a document classifier. For more information on how to build a custom classifier model, seeThe service does not support cancellation of the long running operation and returns with an error message indicating absence of cancellation support.
Code sample
File document = new File("{local/file_path/fileName.jpg}"); String classifierId = "{custom_trained_classifier_id}"; byte[] fileContent = Files.readAllBytes(document.toPath()); documentAnalysisClient.beginClassifyDocument(classifierId, BinaryData.fromBytes(fileContent), Context.NONE) .getFinalResult() .getDocuments() .forEach(analyzedDocument -> System.out.printf("Doc Type: %s%n", analyzedDocument.getDocType()));
- Parameters:
classifierId
- The unique classifier ID to be used. Use this to specify the custom classifier ID.document
- The data of the document to analyze information from.context
- Additional context that is passed through the HTTP pipeline during the service call.- Returns:
- A
SyncPoller
that polls the of progress of analyze document operation until it has completed, has failed, or has been cancelled. The completed operation returns anAnalyzeResult
. - Throws:
com.azure.core.exception.HttpResponseException
- If analyze operation fails and returns with anOperationStatus.FAILED
.IllegalArgumentException
- Ifdocument
orclassifierId
is null.
-