@@ -6,7 +6,7 @@ author: laujan
6
6
manager : nitinme
7
7
ms.service : azure-ai-document-intelligence
8
8
ms.topic : include
9
- ms.date : 03/25 /2024
9
+ ms.date : 05/02 /2024
10
10
ms.author : lajanuar
11
11
---
12
12
<!-- markdownlint-disable MD025 -->
@@ -154,39 +154,37 @@ Extract text, selection marks, text styles, table structures, and bounding regio
154
154
const endpoint = "<your-endpoint>";
155
155
156
156
// sample document
157
- const formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
157
+ const formUrl = "https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-layout.pdf"
158
158
159
- async function main() {
159
+ async function main() {
160
160
const client = DocumentIntelligenceClient(endpoint, new AzureKeyCredential(key));
161
161
162
- const poller = await client.beginAnalyzeDocument("prebuilt-layout", formUrl);
163
162
164
- const {
165
- pages,
166
- tables
167
- } = await poller.pollUntilDone();
163
+ const initialResponse = await client
164
+ .path("/documentModels/{modelId}:analyze", "prebuilt-layout")
165
+ .post: ({
166
+ contentType: "application/json",
167
+ body: {
168
+ urlSource: formUrl
169
+ },
170
+ });
168
171
169
- if (pages.length <= 0) {
170
- console.log("No pages were extracted from the document.");
171
- } else {
172
- console.log("Pages:");
173
- for (const page of pages) {
174
- console.log("- Page", page.pageNumber, `(unit: ${page.unit})`);
175
- console.log(` ${page.width}x${page.height}, angle: ${page.angle}`);
176
- console.log(` ${page.lines.length} lines, ${page.words.length} words`);
177
- }
178
- }
172
+ const poller = await getLongRunningPoller(client, initialResponse);
173
+ const analyzeResult = (await poller.pollUntilDone()).body.analyzeResult;
179
174
180
- if (tables.length <= 0) {
181
- console.log("No tables were extracted from the document.");
182
- } else {
183
- console.log("Tables:");
184
- for (const table of tables) {
185
- console.log(
186
- `- Extracted table: ${table.columnCount} columns, ${table.rowCount} rows (${table.cells.length} cells)`
187
- );
188
- }
175
+ const documents = analyzeResult?.documents;
176
+
177
+ const document = documents && documents[0];
178
+ if (!document) {
179
+ throw new Error("Expected at least one document in the result.");
189
180
}
181
+
182
+ console.log(
183
+ "Extracted document:",
184
+ document.docType,
185
+ `(confidence: ${document.confidence || "<undefined>"})`,
186
+ );
187
+ console.log("Fields:", document.fields);
190
188
}
191
189
192
190
main().catch((error) => {
@@ -323,32 +321,43 @@ const { AzureKeyCredential } = require("@azure/core-auth");
323
321
invoiceUrl = " https://raw.githubusercontent.com/Azure-Samples/cognitive-services-REST-api-samples/master/curl/form-recognizer/sample-invoice.pdf"
324
322
325
323
async function main () {
324
+
326
325
const client = DocumentIntelligenceClient (endpoint, new AzureKeyCredential (key));
327
326
328
- const poller = await client .beginAnalyzeDocument (" prebuilt-invoice" , invoiceUrl);
329
- if (pages .length <= 0 ) {
330
- console .log (" No pages were extracted from the document." );
331
- } else {
332
- console .log (" Pages:" );
333
- for (const page of pages) {
334
- console .log (" - Page" , page .pageNumber , ` (unit: ${ page .unit } )` );
335
- console .log (` ${ page .width } x${ page .height } , angle: ${ page .angle } ` );
336
- console .log (` ${ page .lines .length } lines, ${ page .words .length } words` );
337
- }
338
- }
327
+ const initialResponse = await client
328
+ .path (" /documentModels/{modelId}:analyze" , " prebuilt-invoice" )
329
+ .post ({
330
+ contentType: " application/json" ,
331
+ body: {
332
+ // The Document Intelligence service will access the URL to the invoice image and extract data from it
333
+ urlSource: invoiceUrl,
334
+ },
335
+ });
339
336
340
- if (tables .length <= 0 ) {
341
- console .log (" No tables were extracted from the document." );
337
+
338
+ const poller = await getLongRunningPoller (client, initialResponse);
339
+
340
+ poller .onProgress ((state ) => console .log (" Operation:" , state .result , state .status ));
341
+ const analyzeResult = (await poller .pollUntilDone ()).body .analyzeResult ;
342
+
343
+ const documents = analyzeResult? .documents ;
344
+
345
+ const result = documents && documents[0 ];
346
+ if (result) {
347
+ console .log (result .fields );
342
348
} else {
343
- console .log (" Tables:" );
344
- for (const table of tables) {
345
- console .log (
346
- ` - Extracted table: ${ table .columnCount } columns, ${ table .rowCount } rows (${ table .cells .length } cells)`
347
- );
348
- }
349
+ throw new Error (" Expected at least one invoice in the result." );
349
350
}
351
+
352
+ console .log (
353
+ " Extracted invoice:" ,
354
+ document .docType ,
355
+ ` (confidence: ${ document .confidence || " <undefined>" } )` ,
356
+ );
357
+ console .log (" Fields:" , document .fields );
350
358
}
351
359
360
+
352
361
main ().catch ((error ) => {
353
362
console .error (" An error occurred:" , error);
354
363
process .exit (1 );
0 commit comments