Enterprise Document OCR

您可以使用 Enterprise Document OCR 做為 Document AI 的一部分,偵測及擷取各種文件中的文字和版面配置資訊。您可透過可設定的功能,根據特定文件處理需求調整系統。

總覽

您可以使用 Enterprise Document OCR 執行各種工作,例如根據演算法或機器學習輸入資料,以及提升及驗證資料準確度。您也可以使用 Enterprise Document OCR 處理下列工作:

  • 數位化文字:從文件中擷取文字和版面配置資料,用於搜尋、以規則為基礎的文件處理管道,或建立自訂模型。
  • 使用大型語言模型應用程式:運用 LLM 的情境理解能力,以及 OCR 的文字和版面配置擷取功能,自動回答問題。從資料中取得洞察資訊,並簡化工作流程。
  • 封存:將紙本文件數位化為機器可讀文字,提高文件存取性。

選擇最適合您用途的 OCR

解決方案 產品 說明 用途
Document AI Enterprise Document OCR 專門用於文件用途的模型。進階功能包括圖像品質分數、語言提示和文件旋轉修正。 建議用於從文件擷取文字。用途包括 PDF、掃描成圖片的文件或 Microsoft DocX 檔案。
Document AI OCR 附加元件 滿足特定需求的付費功能。僅與 Enterprise Document OCR 2.0 以上版本相容。 需要偵測及辨識數學公式、接收字型資訊,或啟用核取方塊擷取功能。
Cloud Vision API 文字偵測 全球通用的 REST API,以 Google Cloud 標準 OCR 模型為基礎。預設配額為每分鐘 1,800 項要求。 需要低延遲和高容量的一般文字擷取用途。
Cloud Vision OCR Google Distributed Cloud (已淘汰) Google Cloud Marketplace 應用程式,可使用 GKE Enterprise 以容器形式部署至任何 GKE 叢集。 符合資料落地或法規遵循要求。

偵測與擷取

Enterprise Document OCR 可偵測 PDF 和圖片中的區塊、段落、行、字詞和符號,並校正文件,提高準確度。

支援的版面配置偵測和擷取屬性:

印刷文字 手寫 Paragraph 封鎖 Line 依單字 符號層級 頁碼
預設 預設 預設 預設 預設 預設 可自行設定 預設

可設定的 Enterprise Document OCR 功能包括:

  • 從數位 PDF 擷取內嵌或原生文字:這項功能會擷取文字和符號,並完整保留來源文件中的樣式,即使文字旋轉、字型大小或樣式極端,或部分隱藏,也能正確擷取。

  • 文件旋轉修正:使用 Enterprise Document OCR 預先處理文件圖片,修正可能影響擷取品質或處理作業的文件旋轉問題。

  • 圖像品質分數:取得品質指標,協助文件轉送。圖像品質分數會從八個維度提供頁面層級的品質指標,包括模糊度、字型是否小於一般大小,以及眩光。

  • 指定頁面範圍:指定輸入文件中要進行 OCR 的頁面範圍。這樣可以節省不必要頁面的支出和處理時間。

  • 語言偵測:偵測擷取文字所使用的語言。

  • 語言和手寫提示:根據資料集的已知特徵,提供 OCR 模型語言或手寫提示,提高準確率。

如要瞭解如何啟用 OCR 設定,請參閱「啟用 OCR 設定」。

OCR 附加元件

Enterprise Document OCR 提供選用的分析功能,可視需要為個別處理要求啟用。

穩定版 pretrained-ocr-v2.0-2023-06-02 和 pretrained-ocr-v2.1-2024-08-07,以及候選版 pretrained-ocr-v2.1.1-2025-01-31 支援下列外掛功能。

  • 數學 OCR:辨識並擷取 LaTeX 格式文件中的公式。
  • 核取方塊擷取:偵測核取方塊,並在 Enterprise Document OCR 回應中擷取其狀態 (已標示/未標示)。
  • 字型樣式偵測:辨識字詞層級的字型屬性,包括字型類型、字型樣式、手寫、粗細和顏色。

如要瞭解如何啟用列出的外掛程式,請參閱「啟用 OCR 外掛程式」。

支援的檔案格式

Enterprise Document OCR 支援 PDF、GIF、TIFF、JPEG、PNG、BMP 和 WebP 檔案格式。詳情請參閱「支援的檔案」。

Enterprise Document OCR 也支援 DocX 檔案,同步處理最多 15 頁,非同步處理最多 30 頁。如要提交配額提高要求 (QIR),請按照「申請調整配額」中的步驟操作。DocX 支援功能目前為不公開預先發布版。如要申請存取權,請與 Google 帳戶團隊聯絡。

進階版本管理

進階版本管理功能目前為預先發布版。基礎 AI/機器學習 OCR 模型升級後,OCR 行為可能會有所變更。如需嚴格的一致性,請使用凍結的模型版本,將行為固定在舊版 OCR 模型,最長可達 18 個月。這可確保圖片與 OCR 函式結果相同。請參閱處理器版本表格。

處理器版本

下列處理器版本與這項功能相容。詳情請參閱「管理處理器版本」。

版本 ID 發布管道 說明
pretrained-ocr-v1.2-2022-11-10 穩定 1.0 版的凍結模型版本:版本快照的模型檔案、設定和二進位檔,在容器映像檔中凍結最多 18 個月。
pretrained-ocr-v2.0-2023-06-02 穩定 專為文件用途設計、可用於正式環境的模型。可使用所有 OCR 附加元件。
pretrained-ocr-v2.1-2024-08-07 穩定 v2.1 的主要改善項目包括:提升印刷文字辨識能力、更精確地偵測核取方塊,以及更準確地判斷閱讀順序。
pretrained-ocr-v2.1.1-2025-01-31 候選版 v2.1.1 與 V2.1 類似,適用於所有區域,但以下區域除外:US、EU和asia-southeast1。

使用 Enterprise Document OCR 處理文件

本快速入門導覽課程介紹 Enterprise Document OCR。說明如何啟用或停用任何可用的 OCR 設定,為工作流程最佳化文件 OCR 結果。

  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, if it is not already enabled.

    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

建立 Enterprise Document OCR 處理器

首先,建立 Enterprise Document OCR 處理器。詳情請參閱「建立及管理處理器」。

OCR 設定

如要啟用所有 OCR 設定,請在 ProcessDocumentRequest 或 BatchProcessDocumentsRequest 的 ProcessOptions.ocrConfig 中設定對應欄位。

詳情請參閱「傳送處理要求」。

圖片品質分析

智慧型文件品質分析會使用機器學習技術,根據內容的可讀性評估文件品質。這項品質評估會以品質分數 [0, 1] 的形式傳回,其中 1 代表完美品質。如果偵測到的品質分數低於 0.5,系統也會傳回負面品質原因清單 (依可能性排序)。 可能性大於 0.5 時,系統會判定為偵測到。

如果文件被判定為有瑕疵,API 會傳回下列八種文件瑕疵類型:

  • quality/defect_blurry
  • quality/defect_noisy
  • quality/defect_dark
  • quality/defect_faint
  • quality/defect_text_too_small
  • quality/defect_document_cutoff
  • quality/defect_text_cutoff
  • quality/defect_glare

目前的文件品質分析有一些限制:

  • 如果數位文件沒有瑕疵,可能會傳回偽陽性偵測結果。這項功能最適合用於掃描或拍攝的文件。
  • 眩光瑕疵是局部的。這些元素可能不會影響文件的整體可讀性。

輸入

如要啟用,請在處理要求中將 ProcessOptions.ocrConfig.enableImageQualityScores 設為 true。這項額外功能會使程序呼叫進一步延遲,額外延遲的時間與 OCR 處理相當。

  {
    "rawDocument": {
      "mimeType": "MIME_TYPE",
      "content": "IMAGE_CONTENT"
    },
    "processOptions": {
      "ocrConfig": {
        "enableImageQualityScores": true
      }
    }
  }

輸出內容

瑕疵偵測結果會顯示在 Document.pages[].imageQualityScores[] 中。

  {
    "pages": [
      {
        "imageQualityScores": {
          "qualityScore": 0.7811847,
          "detectedDefects": [
            {
              "type": "quality/defect_document_cutoff",
              "confidence": 1.0
            },
            {
              "type": "quality/defect_glare",
              "confidence": 0.97849524
            },
            {
              "type": "quality/defect_text_cutoff",
              "confidence": 0.5
            }
          ]
        }
      }
    ]
  }

如需完整的輸出範例,請參閱「處理器輸出範例」。

語言提示

OCR 處理器支援您定義的語言提示,可提升 OCR 引擎效能。套用語言提示後,OCR 就能針對所選語言 (而非推測語言) 進行最佳化。

輸入

如要啟用,請將 ProcessOptions.ocrConfig.hints[].languageHints[] 設為 BCP-47 語言代碼清單。

  {
    "rawDocument": {
      "mimeType": "MIME_TYPE",
      "content": "IMAGE_CONTENT"
    },
    "processOptions": {
      "ocrConfig": {
        "hints": {
          "languageHints": ["en", "es"]
        }
      }
    }
  }

如需完整的輸出範例,請參閱「處理器輸出範例」。

符號偵測

在文件回應中,填入符號 (或個別字母) 層級的資料。

輸入

如要啟用,請在處理要求中將 ProcessOptions.ocrConfig.enableSymbol 設為 true。

  {
    "rawDocument": {
      "mimeType": "MIME_TYPE",
      "content": "IMAGE_CONTENT"
    },
    "processOptions": {
      "ocrConfig": {
        "enableSymbol": true
      }
    }
  }

輸出內容

如果啟用這項功能,系統會填入 Document.pages[].symbols[] 欄位。

如需完整的輸出範例,請參閱「處理器輸出範例」。

內建 PDF 剖析

從數位 PDF 檔案擷取內嵌文字。啟用後,如有數位文字,系統會自動使用內建的數位 PDF 模型。如果是非數位文字,系統會自動使用光學 OCR 模型。使用者會收到合併後的文字結果。

輸入

如要啟用,請在處理要求中將 ProcessOptions.ocrConfig.enableNativePdfParsing 設為 true。

  {
    "rawDocument": {
      "mimeType": "MIME_TYPE",
      "content": "IMAGE_CONTENT"
    },
    "processOptions": {
      "ocrConfig": {
        "enableNativePdfParsing": true
      }
    }
  }

偵測角色是否在方框內

根據預設,Enterprise Document OCR 會啟用偵測工具,提升方塊內字元的文字擷取品質。範例如下:

enterprise-document-ocr-1

如果方塊內的字元發生 OCR 品質問題,可以停用這項功能。

輸入

如要停用,請在處理要求中將 ProcessOptions.ocrConfig.disableCharacterBoxesDetection 設為 true。

  {
    "rawDocument": {
      "mimeType": "MIME_TYPE",
      "content": "IMAGE_CONTENT"
    },
    "processOptions": {
      "ocrConfig": {
        "disableCharacterBoxesDetection": true
      }
    }
  }

舊版版面配置

如果您需要啟發式版面配置偵測演算法,可以啟用舊版版面配置,做為目前以機器學習為基礎的版面配置偵測演算法的替代方案。我們不建議採用這種設定。客戶可以根據文件工作流程,選擇最合適的版面配置演算法。

輸入

如要啟用,請在處理要求中將 ProcessOptions.ocrConfig.advancedOcrOptions 設為 ["legacy_layout"]。

  {
    "rawDocument": {
      "mimeType": "MIME_TYPE",
      "content": "IMAGE_CONTENT"
    },
    "processOptions": {
      "ocrConfig": {
          "advancedOcrOptions": ["legacy_layout"]
      }
    }
  }

指定頁面範圍

根據預設,OCR 會從文件的所有頁面擷取文字和版面配置資訊。您可以選取特定頁碼或頁面範圍,只從這些頁面擷取文字。

在 ProcessOptions 中,有三種設定方式:

  • 如要只處理第二和第五頁:
  {
    "individualPageSelector": {"pages": [2, 5]}
  }
  • 如要只處理前三頁:
  {
    "fromStart": 3
  }
  • 如要只處理最後四頁:
  {
    "fromEnd": 4
  }

在回應中,每個 Document.pages[].pageNumber 都會對應要求中指定的相同頁面。

OCR 附加元件用途

您可以視需要,在個別處理要求中啟用這些 Enterprise Document OCR 選用分析功能。

數學 OCR

數學 OCR 可偵測、辨識及擷取公式,例如以 LaTeX 表示的數學方程式,以及定界框座標。

以下是 LaTeX 表示法的範例:

  • 偵測到圖片

    enterprise-document-ocr-2

  • 轉換為 LaTeX

    enterprise-document-ocr-3

輸入

如要啟用,請在處理要求中將 ProcessOptions.ocrConfig.premiumFeatures.enableMathOcr 設為 true。

  {
    "rawDocument": {
      "mimeType": "MIME_TYPE",
      "content": "IMAGE_CONTENT"
    },
    "processOptions": {
      "ocrConfig": {
          "premiumFeatures": {
            "enableMathOcr": true
          }
      }
    }
  }

輸出內容

數學 OCR 輸出內容會顯示在 Document.pages[].visualElements[] 中,並附上 "type": "math_formula"。

"visualElements": [
  {
    "layout": {
      "textAnchor": {
        "textSegments": [
          {
            "endIndex": "46"
          }
        ]
      },
      "confidence": 1,
      "boundingPoly": {
        "normalizedVertices": [
          {
            "x": 0.14662756,
            "y": 0.27891156
          },
          {
            "x": 0.9032258,
            "y": 0.27891156
          },
          {
            "x": 0.9032258,
            "y": 0.8027211
          },
          {
            "x": 0.14662756,
            "y": 0.8027211
          }
        ]
      },
      "orientation": "PAGE_UP"
    },
    "type": "math_formula"
  }
]

您可以透過這個連結查看完整的 Document JSON 輸出內容。

擷取選項標記

如果啟用,模型會嘗試擷取文件中的所有核取方塊和圓形按鈕,以及定界框座標。

輸入

如要啟用,請在處理要求中將 ProcessOptions.ocrConfig.premiumFeatures.enableSelectionMarkDetection 設為 true。

  {
    "rawDocument": {
      "mimeType": "MIME_TYPE",
      "content": "IMAGE_CONTENT"
    },
    "processOptions": {
      "ocrConfig": {
          "premiumFeatures": {
            "enableSelectionMarkDetection": true
          }
      }
    }
  }

輸出內容

核取方塊輸出內容會顯示在 Document.pages[].visualElements[] 中,並包含 "type": "unfilled_checkbox" 或 "type": "filled_checkbox"。

"visualElements": [
  {
    "layout": {
      "confidence": 0.89363575,
      "boundingPoly": {
        "vertices": [
          {
            "x": 11,
            "y": 24
          },
          {
            "x": 37,
            "y": 24
          },
          {
            "x": 37,
            "y": 56
          },
          {
            "x": 11,
            "y": 56
          }
        ],
        "normalizedVertices": [
          {
            "x": 0.017488075,
            "y": 0.38709676
          },
          {
            "x": 0.05882353,
            "y": 0.38709676
          },
          {
            "x": 0.05882353,
            "y": 0.9032258
          },
          {
            "x": 0.017488075,
            "y": 0.9032258
          }
        ]
      }
    },
    "type": "unfilled_checkbox"
  },
  {
    "layout": {
      "confidence": 0.9148201,
      "boundingPoly": ...
    },
    "type": "filled_checkbox"
  }
],

您可以透過這個連結查看完整的 Document JSON 輸出內容。

字型樣式偵測

啟用字型樣式偵測功能後,Enterprise Document OCR 會擷取字型屬性,以便進行更完善的後續處理。

在詞元 (字詞) 層級,系統會偵測下列屬性:

  • 手寫偵測
  • 字型樣式
  • 字型大小
  • 字型
  • 字型顏色
  • 字型粗細
  • 字母間距
  • 粗體
  • 斜體
  • 加上底線
  • 文字顏色 (RGBa)
  • 背景顏色 (RGBa)

輸入

如要啟用,請在處理要求中將 ProcessOptions.ocrConfig.premiumFeatures.computeStyleInfo 設為 true。

  {
    "rawDocument": {
      "mimeType": "MIME_TYPE",
      "content": "IMAGE_CONTENT"
    },
    "processOptions": {
      "ocrConfig": {
          "premiumFeatures": {
            "computeStyleInfo": true
          }
      }
    }
  }

輸出內容

字型樣式輸出內容會顯示在 Document.pages[].tokens[].styleInfo 中,類型為 StyleInfo。

"tokens": [
  {
    "styleInfo": {
      "fontSize": 3,
      "pixelFontSize": 13,
      "fontType": "SANS_SERIF",
      "bold": true,
      "fontWeight": 564,
      "textColor": {
        "red": 0.16862746,
        "green": 0.16862746,
        "blue": 0.16862746
      },
      "backgroundColor": {
        "red": 0.98039216,
        "green": 0.9882353,
        "blue": 0.99215686
      }
    }
  },
  ...
]

您可以透過這個連結查看完整的 Document JSON 輸出內容。

將文件物件轉換為 Vision AI API 格式

Document AI Toolbox 包含一項工具,可將 Document AI API Document 格式轉換為 Vision AI AnnotateFileResponse 格式,讓使用者比較文件 OCR 處理器和 Vision AI API 的回應。請參閱程式碼範例。

Vision AI API 回應與 Document AI API 回應和轉換器之間的已知差異:

  • Vision AI API 回應只會為圖片要求填入 vertices,並只會為 PDF 要求填入 normalized_vertices。Document AI 回應和轉換器會填入 vertices 和 normalized_vertices。
  • Vision AI API 回應會在字詞的最後一個符號中填入 detected_break。Document AI API 回應和轉換器會在字詞和字詞的最後一個符號中填入 detected_break。
  • Vision AI API 回應一律會填入符號欄位。根據預設,Document AI 回應不會填入符號欄位。為確保 Document AI 回應和轉換器填入符號欄位,請將 enable_symbol 功能設為詳細。

程式碼範例

下列程式碼範例示範如何傳送處理要求,啟用 OCR 設定和外掛程式,然後讀取欄位並輸出至終端機:

REST

使用任何要求資料之前,請先修改下列項目的值:

  • LOCATION:處理器的位置,例如:
    • us - 美國
    • eu - 歐盟
  • PROJECT_ID: Google Cloud 專案 ID。
  • PROCESSOR_ID:自訂處理器的 ID。
  • PROCESSOR_VERSION:處理器版本 ID。詳情請參閱「選取處理器版本」。例如:
    • pretrained-TYPE-vX.X-YYYY-MM-DD
    • stable
    • rc
  • skipHumanReview:用於停用專人審查的布林值 (僅支援人工介入迴路處理器)。
    • true - 略過專人審查
    • false:啟用專人審查 (預設)
  • MIME_TYPE†:其中一個有效的 MIME 類型選項。
  • IMAGE_CONTENT†:有效的內嵌文件內容之一,以位元組串流表示。如果是 JSON 表示法,則為二進位圖片資料的 Base64 編碼 (ASCII 字串)。這個字串應與下列字串類似:
    • /9j/4QAYRXhpZgAA...9tAVx/zDQDlGxn//2Q==
    詳情請參閱「Base64 編碼」主題。
  • FIELD_MASK:指定要納入 Document 輸出的欄位。這是以半形逗號分隔的完整欄位名稱清單,格式為 FieldMask。
    • 範例:text,entities,pages.pageNumber
  • OCR 設定
    • ENABLE_NATIVE_PDF_PARSING:(布林值) 從 PDF 擷取內嵌文字 (如有)。
    • ENABLE_IMAGE_QUALITY_SCORES:(布林值) 啟用智慧文件品質分數。
    • ENABLE_SYMBOL:(布林值) 包含符號 (字母) OCR 資訊。
    • DISABLE_CHARACTER_BOXES_DETECTION:(布林值) 關閉 OCR 引擎的字元方塊偵測工具。
    • LANGUAGE_HINTS:用於 OCR 的 BCP-47 語言代碼清單。
    • ADVANCED_OCR_OPTIONS:進階 OCR 選項清單,可進一步微調 OCR 行為。目前有效的值如下:
      • legacy_layout:啟發式版面配置偵測演算法,可做為目前機器學習版面配置偵測演算法的替代方案。
  • Premium OCR 附加元件
    • ENABLE_SELECTION_MARK_DETECTION:(布林值) 啟用 OCR 引擎的選取標記偵測工具。
    • COMPUTE_STYLE_INFO (布林值):啟用字型識別模型,並傳回字型樣式資訊。
    • ENABLE_MATH_OCR:(布林值) 啟用可擷取 LaTeX 數學公式的模型。
  • INDIVIDUAL_PAGES:要處理的個別頁面清單。
    • 或者,您也可以提供 fromStart 或 fromEnd 欄位,從文件開頭或結尾處理特定數量的頁面。

† 這項內容也可以使用 inlineDocument 物件中採用 Base64 編碼的內容指定。

HTTP 方法和網址:

POST https://LOCATION-documentai.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/processorVersions/PROCESSOR_VERSION:process

JSON 要求內文:

{
  "skipHumanReview": skipHumanReview,
  "rawDocument": {
    "mimeType": "MIME_TYPE",
    "content": "IMAGE_CONTENT"
  },
  "fieldMask": "FIELD_MASK",
  "processOptions": {
    "ocrConfig": {
      "enableNativePdfParsing": ENABLE_NATIVE_PDF_PARSING,
      "enableImageQualityScores": ENABLE_IMAGE_QUALITY_SCORES,
      "enableSymbol": ENABLE_SYMBOL,
      "disableCharacterBoxesDetection": DISABLE_CHARACTER_BOXES_DETECTION,
      "hints": {
        "languageHints": [
          "LANGUAGE_HINTS"
        ]
      },
      "advancedOcrOptions": ["ADVANCED_OCR_OPTIONS"],
      "premiumFeatures": {
        "enableSelectionMarkDetection": ENABLE_SELECTION_MARK_DETECTION,
        "computeStyleInfo": COMPUTE_STYLE_INFO,
        "enableMathOcr": ENABLE_MATH_OCR,
      }
    },
    "individualPageSelector" {
      "pages": [INDIVIDUAL_PAGES]
    }
  }
}

如要傳送要求,請選擇以下其中一個選項:

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/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/processorVersions/PROCESSOR_VERSION: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/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/processorVersions/PROCESSOR_VERSION:process" | Select-Object -Expand Content

如果要求成功,伺服器會傳回 200 OK HTTP 狀態碼和 JSON 格式的回應。回應主體包含 Document 的執行個體。

Python

詳情請參閱 Document AI Python API 參考文件。

如要向 Document AI 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


from typing import Optional, Sequence

from google.api_core.client_options import ClientOptions
from google.cloud import documentai

# 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
# processor_version = "rc" # Refer to https://cloud.google.com/document-ai/docs/manage-processor-versions for more information
# 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


def process_document_ocr_sample(
    project_id: str,
    location: str,
    processor_id: str,
    processor_version: str,
    file_path: str,
    mime_type: str,
) -> None:
    # Optional: Additional configurations for Document OCR Processor.
    # For more information: https://cloud.google.com/document-ai/docs/enterprise-document-ocr
    process_options = documentai.ProcessOptions(
        ocr_config=documentai.OcrConfig(
            enable_native_pdf_parsing=True,
            enable_image_quality_scores=True,
            enable_symbol=True,
            # OCR Add Ons https://cloud.google.com/document-ai/docs/ocr-add-ons
            premium_features=documentai.OcrConfig.PremiumFeatures(
                compute_style_info=True,
                enable_math_ocr=False,  # Enable to use Math OCR Model
                enable_selection_mark_detection=True,
            ),
        )
    )
    # Online processing request to Document AI
    document = process_document(
        project_id,
        location,
        processor_id,
        processor_version,
        file_path,
        mime_type,
        process_options=process_options,
    )

    text = document.text
    print(f"Full document text: {text}\n")
    print(f"There are {len(document.pages)} page(s) in this document.\n")

    for page in document.pages:
        print(f"Page {page.page_number}:")
        print_page_dimensions(page.dimension)
        print_detected_languages(page.detected_languages)

        print_blocks(page.blocks, text)
        print_paragraphs(page.paragraphs, text)
        print_lines(page.lines, text)
        print_tokens(page.tokens, text)

        if page.symbols:
            print_symbols(page.symbols, text)

        if page.image_quality_scores:
            print_image_quality_scores(page.image_quality_scores)

        if page.visual_elements:
            print_visual_elements(page.visual_elements, text)


def print_page_dimensions(dimension: documentai.Document.Page.Dimension) -> None:
    print(f"    Width: {str(dimension.width)}")
    print(f"    Height: {str(dimension.height)}")


def print_detected_languages(
    detected_languages: Sequence[documentai.Document.Page.DetectedLanguage],
) -> None:
    print("    Detected languages:")
    for lang in detected_languages:
        print(f"        {lang.language_code} ({lang.confidence:.1%} confidence)")


def print_blocks(blocks: Sequence[documentai.Document.Page.Block], text: str) -> None:
    print(f"    {len(blocks)} blocks detected:")
    first_block_text = layout_to_text(blocks[0].layout, text)
    print(f"        First text block: {repr(first_block_text)}")
    last_block_text = layout_to_text(blocks[-1].layout, text)
    print(f"        Last text block: {repr(last_block_text)}")


def print_paragraphs(
    paragraphs: Sequence[documentai.Document.Page.Paragraph], text: str
) -> None:
    print(f"    {len(paragraphs)} paragraphs detected:")
    first_paragraph_text = layout_to_text(paragraphs[0].layout, text)
    print(f"        First paragraph text: {repr(first_paragraph_text)}")
    last_paragraph_text = layout_to_text(paragraphs[-1].layout, text)
    print(f"        Last paragraph text: {repr(last_paragraph_text)}")


def print_lines(lines: Sequence[documentai.Document.Page.Line], text: str) -> None:
    print(f"    {len(lines)} lines detected:")
    first_line_text = layout_to_text(lines[0].layout, text)
    print(f"        First line text: {repr(first_line_text)}")
    last_line_text = layout_to_text(lines[-1].layout, text)
    print(f"        Last line text: {repr(last_line_text)}")


def print_tokens(tokens: Sequence[documentai.Document.Page.Token], text: str) -> None:
    print(f"    {len(tokens)} tokens detected:")
    first_token_text = layout_to_text(tokens[0].layout, text)
    first_token_break_type = tokens[0].detected_break.type_.name
    print(f"        First token text: {repr(first_token_text)}")
    print(f"        First token break type: {repr(first_token_break_type)}")
    if tokens[0].style_info:
        print_style_info(tokens[0].style_info)

    last_token_text = layout_to_text(tokens[-1].layout, text)
    last_token_break_type = tokens[-1].detected_break.type_.name
    print(f"        Last token text: {repr(last_token_text)}")
    print(f"        Last token break type: {repr(last_token_break_type)}")
    if tokens[-1].style_info:
        print_style_info(tokens[-1].style_info)


def print_symbols(
    symbols: Sequence[documentai.Document.Page.Symbol], text: str
) -> None:
    print(f"    {len(symbols)} symbols detected:")
    first_symbol_text = layout_to_text(symbols[0].layout, text)
    print(f"        First symbol text: {repr(first_symbol_text)}")
    last_symbol_text = layout_to_text(symbols[-1].layout, text)
    print(f"        Last symbol text: {repr(last_symbol_text)}")


def print_image_quality_scores(
    image_quality_scores: documentai.Document.Page.ImageQualityScores,
) -> None:
    print(f"    Quality score: {image_quality_scores.quality_score:.1%}")
    print("    Detected defects:")

    for detected_defect in image_quality_scores.detected_defects:
        print(f"        {detected_defect.type_}: {detected_defect.confidence:.1%}")


def print_style_info(style_info: documentai.Document.Page.Token.StyleInfo) -> None:
    """
    Only supported in version `pretrained-ocr-v2.0-2023-06-02`
    """
    print(f"           Font Size: {style_info.font_size}pt")
    print(f"           Font Type: {style_info.font_type}")
    print(f"           Bold: {style_info.bold}")
    print(f"           Italic: {style_info.italic}")
    print(f"           Underlined: {style_info.underlined}")
    print(f"           Handwritten: {style_info.handwritten}")
    print(
        f"           Text Color (RGBa): {style_info.text_color.red}, {style_info.text_color.green}, {style_info.text_color.blue}, {style_info.text_color.alpha}"
    )


def print_visual_elements(
    visual_elements: Sequence[documentai.Document.Page.VisualElement], text: str
) -> None:
    """
    Only supported in version `pretrained-ocr-v2.0-2023-06-02`
    """
    checkboxes = [x for x in visual_elements if "checkbox" in x.type]
    math_symbols = [x for x in visual_elements if x.type == "math_formula"]

    if checkboxes:
        print(f"    {len(checkboxes)} checkboxes detected:")
        print(f"        First checkbox: {repr(checkboxes[0].type)}")
        print(f"        Last checkbox: {repr(checkboxes[-1].type)}")

    if math_symbols:
        print(f"    {len(math_symbols)} math symbols detected:")
        first_math_symbol_text = layout_to_text(math_symbols[0].layout, text)
        print(f"        First math symbol: {repr(first_math_symbol_text)}")




def process_document(
    project_id: str,
    location: str,
    processor_id: str,
    processor_version: str,
    file_path: str,
    mime_type: str,
    process_options: Optional[documentai.ProcessOptions] = None,
) -> documentai.Document:
    # You must set the `api_endpoint` if you use a location other than "us".
    client = documentai.DocumentProcessorServiceClient(
        client_options=ClientOptions(
            api_endpoint=f"{location}-documentai.googleapis.com"
        )
    )

    # The full resource name of the processor version, e.g.:
    # `projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id}`
    # You must create a processor before running this sample.
    name = client.processor_version_path(
        project_id, location, processor_id, processor_version
    )

    # Read the file into memory
    with open(file_path, "rb") as image:
        image_content = image.read()

    # Configure the process request
    request = documentai.ProcessRequest(
        name=name,
        raw_document=documentai.RawDocument(content=image_content, mime_type=mime_type),
        # Only supported for Document OCR processor
        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
    return result.document




def layout_to_text(layout: documentai.Document.Page.Layout, text: str) -> str:
    """
    Document AI identifies text in different parts of the document by their
    offsets in the entirety of the document"s text. This function converts
    offsets to a string.
    """
    # If a text segment spans several lines, it will
    # be stored in different text segments.
    return "".join(
        text[int(segment.start_index) : int(segment.end_index)]
        for segment in layout.text_anchor.text_segments
    )

後續步驟