使用客户端库处理文档

本页介绍了如何以您偏好的编程语言开始使用 Document AI API。

准备工作

  1. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. Enable the Document AI API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the API

  4. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Grant the Document AI > Document AI Administrator role to the service account.

      To grant the role, find the Select a role list, then select Document AI > Document AI Administrator.

    7. Click Continue.
    8. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  5. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  6. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

安装客户端库

C#

如需详细了解如何设置 C# 开发环境,请参阅 C# 开发环境设置指南

Install-Package Google.Cloud.DocumentAI.V1 -Pre

Go

go get cloud.google.com/go/documentai

Java

如需详细了解如何设置 Java 开发环境,请参阅 Java 开发环境设置指南

如果您使用的是 Maven,请将以下代码添加到您的 pom.xml 文件中。如需详细了解 BOM,请参阅 Google Cloud Platform 库 BOM

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.86.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-document-ai</artifactId>
  </dependency>
</dependencies>

如果您使用的是 Gradle,请将以下代码添加到您的依赖项中:

implementation platform('com.google.cloud:libraries-bom:26.86.0')

implementation 'com.google.cloud:google-cloud-document-ai'

如果您使用的是 sbt,请将以下代码添加到您的依赖项中:

libraryDependencies += "com.google.cloud" % "google-cloud-document-ai" % "2.101.0"

如果您使用的是 Visual Studio Code 或 IntelliJ,可以通过以下 IDE 插件将客户端库添加到您的项目中:

上述插件还提供其他功能,例如服务账号密钥管理。如需了解详情,请参阅各个插件相应的文档。

Node.js

如需详细了解如何设置 Node.js 开发环境,请参阅 Node.js 开发环境设置指南

npm install @google-cloud/documentai

PHP

composer require google/cloud-document-ai

Python

如需详细了解如何设置 Python 开发环境,请参阅 Python 开发环境设置指南

pip install --upgrade google-cloud-documentai

Ruby

如需详细了解如何设置 Ruby 开发环境,请参阅 Ruby 开发环境设置指南

gem install google-cloud-document_ai

文档处理

使用 Document AI API 从本地 PDF 文档请求信息。如需运行以下示例,您必须先在界面中创建处理器。

控制台

  1. 在 Google Cloud 控制台的 Document AI 部分,前往处理器页面。

    前往“处理器”页面

  2. 选择 创建处理器

  3. 点击要创建的处理器类型。

  4. 在侧边创建处理器窗口中,指定处理器名称。

  5. 从列表中选择您的区域

  6. 点击创建以创建处理器。

创建处理器并获得处理器 ID 后,运行以下代码以请求处理单个文档:

C#

如需了解详情,请参阅 Document AI C# API 参考文档

如需向 Document AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证


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