Online (synchronous) requests let send a single document for processing.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
C#
For more information, see the Document AI C# API reference documentation .
To authenticate to Document AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .
using
Google.Cloud.DocumentAI.V1
;
using
Google.Protobuf
;
using
System
;
using
System.IO
;
public
class
QuickstartSample
{
public
Document
Quickstart
(
string
projectId
=
"your-project-id"
,
string
locationId
=
"your-processor-location"
,
string
processorId
=
"your-processor-id"
,
string
localPath
=
"my-local-path/my-file-name"
,
string
mimeType
=
"application/pdf"
)
{
// Create client
var
client
=
new
DocumentProcessorServiceClientBuilder
{
Endpoint
=
$"{locationId}-documentai.googleapis.com"
}.
Build
();
// Read in local file
using
var
fileStream
=
File
.
OpenRead
(
localPath
);
var
rawDocument
=
new
RawDocument
{
Content
=
ByteString
.
FromStream
(
fileStream
),
MimeType
=
mimeType
};
// Initialize request argument(s)
var
request
=
new
ProcessRequest
{
Name
=
ProcessorName
.
FromProjectLocationProcessor
(
projectId
,
locationId
,
processorId
).
ToString
(),
RawDocument
=
rawDocument
};
// Make the request
var
response
=
client
.
ProcessDocument
(
request
);
var
document
=
response
.
Document
;
Console
.
WriteLine
(
document
.
Text
);
return
document
;
}
}
Java
For more information, see the Document AI Java API reference documentation .
To authenticate to Document AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .
import
com.google.cloud.documentai.v1. Document
;
import
com.google.cloud.documentai.v1. DocumentProcessorServiceClient
;
import
com.google.cloud.documentai.v1. DocumentProcessorServiceSettings
;
import
com.google.cloud.documentai.v1. ProcessRequest
;
import
com.google.cloud.documentai.v1. ProcessResponse
;
import
com.google.cloud.documentai.v1. RawDocument
;
import
com.google.protobuf. ByteString
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.util.List
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.TimeoutException
;
public
class
ProcessDocument
{
public
static
void
processDocument
()
throws
IOException
,
InterruptedException
,
ExecutionException
,
TimeoutException
{
// TODO(developer): Replace these variables before running the sample.
String
projectId
=
"your-project-id"
;
String
location
=
"your-project-location"
;
// Format is "us" or "eu".
String
processerId
=
"your-processor-id"
;
String
filePath
=
"path/to/input/file.pdf"
;
processDocument
(
projectId
,
location
,
processerId
,
filePath
);
}
public
static
void
processDocument
(
String
projectId
,
String
location
,
String
processorId
,
String
filePath
)
throws
IOException
,
InterruptedException
,
ExecutionException
,
TimeoutException
{
// Initialize client that will be used to send requests. This client only needs
// to be created
// once, and can be reused for multiple requests. After completing all of your
// requests, call
// the "close" method on the client to safely clean up any remaining background
// resources.
String
endpoint
=
String
.
format
(
"%s-documentai.googleapis.com:443"
,
location
);
DocumentProcessorServiceSettings
settings
=
DocumentProcessorServiceSettings
.
newBuilder
().
setEndpoint
(
endpoint
).
build
();
try
(
DocumentProcessorServiceClient
client
=
DocumentProcessorServiceClient
.
create
(
settings
))
{
// The full resource name of the processor, e.g.:
// projects/project-id/locations/location/processor/processor-id
// You must create new processors in the Cloud Console first
String
name
=
String
.
format
(
"projects/%s/locations/%s/processors/%s"
,
projectId
,
location
,
processorId
);
// Read the file.
byte
[]
imageFileData
=
Files
.
readAllBytes
(
Paths
.
get
(
filePath
));
// Convert the image data to a Buffer and base64 encode it.
ByteString
content
=
ByteString
.
copyFrom
(
imageFileData
);
RawDocument
document
=
RawDocument
.
newBuilder
().
setContent
(
content
).
setMimeType
(
"application/pdf"
).
build
();
// Configure the process request.
ProcessRequest
request
=
ProcessRequest
.
newBuilder
().
setName
(
name
).
setRawDocument
(
document
).
build
();
// Recognizes text entities in the PDF document
ProcessResponse
result
=
client
.
processDocument
(
request
);
Document
documentResponse
=
result
.
getDocument
();
// Get all of the document text as one big string
String
text
=
documentResponse
.
getText
();
// Read the text recognition output from the processor
System
.
out
.
println
(
"The document contains the following paragraphs:"
);
Document
.
Page
firstPage
=
documentResponse
.
getPages
(
0
);
List<Document
.
Page
.
Paragraph
>
paragraphs
=
firstPage
.
getParagraphsList
();
for
(
Document
.
Page
.
Paragraph
paragraph
:
paragraphs
)
{
String
paragraphText
=
getText
(
paragraph
.
getLayout
().
getTextAnchor
(),
text
);
System
.
out
.
printf
(
"Paragraph text:\n%s\n"
,
paragraphText
);
}
// Form parsing provides additional output about
// form-formatted PDFs. You must create a form
// processor in the Cloud Console to see full field details.
System
.
out
.
println
(
"The following form key/value pairs were detected:"
);
for
(
Document
.
Page
.
FormField
field
:
firstPage
.
getFormFieldsList
())
{
String
fieldName
=
getText
(
field
.
getFieldName
().
getTextAnchor
(),
text
);
String
fieldValue
=
getText
(
field
.
getFieldValue
().
getTextAnchor
(),
text
);
System
.
out
.
println
(
"Extracted form fields pair:"
);
System
.
out
.
printf
(
"\t(%s, %s))\n"
,
fieldName
,
fieldValue
);
}
}
}
// Extract shards from the text field
private
static
String
getText
(
Document
.
TextAnchor
textAnchor
,
String
text
)
{
if
(
textAnchor
.
getTextSegmentsList
().
size
()
>
0
)
{
int
startIdx
=
(
int
)
textAnchor
.
getTextSegments
(
0
).
getStartIndex
();
int
endIdx
=
(
int
)
textAnchor
.
getTextSegments
(
0
).
getEndIndex
();
return
text
.
substring
(
startIdx
,
endIdx
);
}
return
"[NO TEXT]"
;
}
}
Node.js
For more information, see the Document AI Node.js API reference documentation .
To authenticate to Document AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'YOUR_PROJECT_LOCATION'; // Format is 'us' or 'eu'
// const processorId = 'YOUR_PROCESSOR_ID'; // Create processor in Cloud Console
// const filePath = '/path/to/local/pdf';
const
{
DocumentProcessorServiceClient
}
=
require
(
' @google-cloud/documentai
'
).
v1
;
// Instantiates a client
const
client
=
new
DocumentProcessorServiceClient
();
async
function
processDocument
()
{
// The full resource name of the processor, e.g.:
// projects/project-id/locations/location/processor/processor-id
// You must create new processors in the Cloud Console first
const
name
=
`projects/
${
projectId
}
/locations/
${
location
}
/processors/
${
processorId
}
`
;
// Read the file into memory.
const
fs
=
require
(
'fs'
).
promises
;
const
imageFile
=
await
fs
.
readFile
(
filePath
);
// Convert the image data to a Buffer and base64 encode it.
const
encodedImage
=
Buffer
.
from
(
imageFile
).
toString
(
'base64'
);
const
request
=
{
name
,
rawDocument
:
{
content
:
encodedImage
,
mimeType
:
'application/pdf'
,
},
};
// Recognizes text entities in the PDF document
const
[
result
]
=
await
client
.
processDocument
(
request
);
const
{
document
}
=
result
;
// Get all of the document text as one big string
const
{
text
}
=
document
;
// Extract shards from the text field
const
getText
=
textAnchor
=
>
{
if
(
!
textAnchor
.
textSegments
||
textAnchor
.
textSegments
.
length
===
0
)
{
return
''
;
}
// First shard in document doesn't have startIndex property
const
startIndex
=
textAnchor
.
textSegments
[
0
].
startIndex
||
0
;
const
endIndex
=
textAnchor
.
textSegments
[
0
].
endIndex
;
return
text
.
substring
(
startIndex
,
endIndex
);
};
// Read the text recognition output from the processor
console
.
log
(
'The document contains the following paragraphs:'
);
const
[
page1
]
=
document
.
pages
;
const
{
paragraphs
}
=
page1
;
for
(
const
paragraph
of
paragraphs
)
{
const
paragraphText
=
getText
(
paragraph
.
layout
.
textAnchor
);
console
.
log
(
`Paragraph text:\n
${
paragraphText
}
`
);
}
// Form parsing provides additional output about
// form-formatted PDFs. You must create a form
// processor in the Cloud Console to see full field details.
console
.
log
(
'\nThe following form key/value pairs were detected:'
);
const
{
formFields
}
=
page1
;
for
(
const
field
of
formFields
)
{
const
fieldName
=
getText
(
field
.
fieldName
.
textAnchor
);
const
fieldValue
=
getText
(
field
.
fieldValue
.
textAnchor
);
console
.
log
(
'Extracted key value pair:'
);
console
.
log
(
`\t(
${
fieldName
}
,
${
fieldValue
}
)`
);
}
}
Python
For more information, see the Document AI Python API reference documentation .
To authenticate to Document AI, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .
from
typing
import
Optional
from
google.api_core.client_options
import
ClientOptions
from
google.cloud
import
documentai
# type: ignore
# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"
# location = "YOUR_PROCESSOR_LOCATION" # Format is "us" or "eu"
# processor_id = "YOUR_PROCESSOR_ID" # Create processor before running sample
# file_path = "/path/to/local/pdf"
# mime_type = "application/pdf" # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types
# field_mask = "text,entities,pages.pageNumber" # Optional. The fields to return in the Document object.
# processor_version_id = "YOUR_PROCESSOR_VERSION_ID" # Optional. Processor version to use
def
process_document_sample
(
project_id
:
str
,
location
:
str
,
processor_id
:
str
,
file_path
:
str
,
mime_type
:
str
,
field_mask
:
Optional
[
str
]
=
None
,
processor_version_id
:
Optional
[
str
]
=
None
,
)
-
> None
:
# You must set the `api_endpoint` if you use a location other than "us".
opts
=
ClientOptions
(
api_endpoint
=
f
"
{
location
}
-documentai.googleapis.com"
)
client
=
documentai
.
DocumentProcessorServiceClient
(
client_options
=
opts
)
if
processor_version_id
:
# The full resource name of the processor version, e.g.:
# `projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id}`
name
=
client
.
processor_version_path
(
project_id
,
location
,
processor_id
,
processor_version_id
)
else
:
# The full resource name of the processor, e.g.:
# `projects/{project_id}/locations/{location}/processors/{processor_id}`
name
=
client
.
processor_path
(
project_id
,
location
,
processor_id
)
# Read the file into memory
with
open
(
file_path
,
"rb"
)
as
image
:
image_content
=
image
.
read
()
# Load binary data
raw_document
=
documentai
.
RawDocument
(
content
=
image_content
,
mime_type
=
mime_type
)
# For more information: https://cloud.google.com/document-ai/docs/reference/rest/v1/ProcessOptions
# Optional: Additional configurations for processing.
process_options
=
documentai
.
ProcessOptions
(
# Process only specific pages
individual_page_selector
=
documentai
.
ProcessOptions
.
IndividualPageSelector
(
pages
=
[
1
]
)
)
# Configure the process request
request
=
documentai
.
ProcessRequest
(
name
=
name
,
raw_document
=
raw_document
,
field_mask
=
field_mask
,
process_options
=
process_options
,
)
result
=
client
.
process_document
(
request
=
request
)
# For a full list of `Document` object attributes, reference this page:
# https://cloud.google.com/document-ai/docs/reference/rest/v1/Document
document
=
result
.
document
# Read the text recognition output from the processor
print
(
"The document contains the following text:"
)
print
(
document
.
text
)
What's next
To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

