布局解析器快速入门
使用布局解析器从文档中提取元素,例如文本、表格和列表。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Document AI, Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Document AI, Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
创建处理器
在 Google Cloud 控制台的 Document AI 部分,选择处理器库。
在处理器库中,
搜索 布局解析器,然后选择创建。
在侧边窗口中,输入处理器名称,例如
quickstart-layout-processor。选择离您最近的区域。
点击创建。
系统会将您转到新表单解析器处理器的处理器详情页面。
可选:点击
管理版本 ,然后从版本表中选择一个处理器,以选择默认处理器。接下来,点击标记为默认 ,并通过输入处理器名称进行确认。
测试处理器
创建处理器后,您可以向该处理器发送注解请求。
点击
上传测试文档 按钮,然后选择您刚刚下载的文档。您现在看到的应该是布局解析器分析页面。您可以查看从文档中解析出的块,这些块按检测到的类型进行整理。
可选:选择
修改布局配置 以启用图片或表格注解数据。
处理文档
REST
此示例展示了如何将存储在 Cloud Storage 中的文档发送到布局解析器进行处理。此流程默认启用图片和表格注解。
REST
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- LOCATION:处理器的位置,例如:
us- 美国eu- 欧盟
- PROCESSOR_ID:自定义处理器的 ID。
- MIME_TYPE:布局解析器支持
application/pdf和text/html。 - GCS_FILE_PATH:包含文档的 Cloud Storage 存储桶的文件路径。
- CHUNK_SIZE:可选。拆分文档时使用的块大小(以 token 为单位)。
- INCLUDE_ANCESTOR_HEADINGS:可选。布尔值。在拆分文档时是否包含祖先标题。
HTTP 方法和网址:
POST https://LOCATION-documentai.googleapis.com/v1beta3/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/processorVersions/pretrained-layout-parser-v1.5-2025-08-25:process
请求 JSON 正文:
{
"gcsDocument": {
"gcsUri": "GCS_FILE_PATH",
"mimeType": "MIME_TYPE"
},
"processOptions": {
"layoutConfig": {
"enableTableAnnotation": "true",
"enableImageAnnotation": "true",
"chunkingConfig": {
"chunkSize": "CHUNK_SIZE",
"includeAncestorHeadings": "INCLUDE_ANCESTOR_HEADINGS",
}
}
}
}
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-documentai.googleapis.com/v1beta3/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/processorVersions/pretrained-layout-parser-v1.5-2025-08-25:process"
PowerShell
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-documentai.googleapis.com/v1beta3/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/processorVersions/pretrained-layout-parser-v1.5-2025-08-25:process" | Select-Object -Expand Content
您应该会收到一个成功的状态代码 (2xx) 和一个空响应。
查看输出
如果请求成功,则会返回 JSON 格式的文档对象。对于检索增强生成 (RAG),最重要的字段是 document.chunked_document.chunks。
以下是解析 A.A. 米尔恩的《小熊维尼》第三页后的输出。
{
"document": {
document_layout {
blocks {
block_id: "1"
text_block {
text: "WE ARE INTRODUCED 3"
type_: "header"
}
page_span