Claude Platform Docs
Messages處理檔案

PDF 支援

使用 Claude 處理 PDF:從您的文件中擷取文字、分析圖表並理解視覺內容。

您可以向 Claude 詢問您所提供的 PDF 中的任何文字、圖片、圖表和表格。一些範例使用情境:

  • 分析財務報告並理解圖表/表格
  • 從法律文件中擷取關鍵資訊
  • 協助文件翻譯
  • 將文件資訊轉換為結構化格式

開始之前

檢查 PDF 需求

Claude 可處理任何標準 PDF。請確保您的請求大小符合以下需求:

需求限制
最大請求大小32 MB(依平台而異
每個請求的最大頁數600(當請求的上下文視窗小於 1M tokens 時為 100)
格式標準 PDF(無密碼/加密)

這兩項限制皆適用於整個請求負載,包括與 PDF 一同傳送的任何其他內容。對於大型 PDF,請考慮使用 Files API 上傳並透過 file_id 引用,以保持請求負載較小。

由於 PDF 支援仰賴 Claude 的視覺能力,因此它受到與其他視覺任務相同的限制與注意事項約束。

支援的平台與模型

所有現行模型皆支援 PDF 處理。若要透過 Amazon Bedrock 的 Converse API 使用 PDF 支援,請參閱 Amazon Bedrock PDF 支援

Amazon Bedrock PDF 支援

當透過 Converse API(屬於 Amazon Bedrock 上的 Claude(Opus 4.6 及更早版本)的一部分)使用 PDF 支援時,有兩種不同的文件處理模式:

文件處理模式

  1. Converse Document Chat(原始模式 - 僅文字擷取)

    • 提供 PDF 的基本文字擷取
    • 無法分析 PDF 中的影像、圖表或視覺版面配置
    • 3 頁的 PDF 約使用 1,000 個 tokens
    • 未啟用引用時自動使用
  2. Claude PDF Chat(新模式 - 完整視覺理解)

    • 提供 PDF 的完整視覺分析
    • 能夠理解並分析圖表、圖形、影像和視覺版面配置
    • 將每一頁同時以文字和影像處理,以獲得全面的理解
    • 3 頁的 PDF 約使用 7,000 個 tokens
    • 需要在 Converse API 中啟用引用

主要限制

  • Converse API: 視覺化 PDF 分析需要啟用引用。目前沒有不使用引用即可進行視覺分析的選項(與 InvokeModel API 不同)。
  • InvokeModel API: 提供對 PDF 處理的完整控制,不強制使用引用。

常見問題

如果在使用 Converse API 時 Claude 看不到您 PDF 中的影像或圖表,您很可能需要啟用引用旗標。若未啟用,Converse 將退回為僅進行基本文字擷取。

使用 Claude 處理 PDF

傳送您的第一個 PDF 請求

從使用 Messages API 的簡單範例開始。您可以透過三種方式向 Claude 提供 PDF:

  1. 以 URL 引用線上託管的 PDF
  2. document 內容區塊中以 base64 編碼的 PDF
  3. 透過 Files APIfile_id

選項 1:基於 URL 的 PDF 文件

最簡單的方法是直接從 URL 引用 PDF:

client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-opus-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "url",
                        "url": "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf",
                    },
                },
                {"type": "text", "text": "What are the key findings in this document?"},
            ],
        }
    ],
)

print(message.content)

回應會在 content 中以文字區塊的形式傳回 Claude 的分析,並在 usage 中提供 token 消耗量:

Output
{
  "id": "msg_01Hfp8YuFjQ55VgWbpdHDehB",
  "type": "message",
  "role": "assistant",
  "model": "claude-opus-5",
  "content": [
    {
      "type": "text",
      "text": "This document is an addendum to the Claude 3 model card, reporting updated evaluation results. The key findings include..."
    }
  ],
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 45000,
    "output_tokens": 300
  }
}

選項 2:Base64 編碼的 PDF 文件

如果您需要從本機系統傳送 PDF,或在沒有可用 URL 的情況下:

import base64
import httpx2

# 首先,載入並編碼 PDF
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
    httpx2.get(pdf_url, follow_redirects=True).content
).decode("utf-8")

# 替代方案:從本機檔案載入
# with open("document.pdf", "rb") as f:
#     pdf_data = base64.standard_b64encode(f.read()).decode("utf-8")

# 使用 base64 編碼傳送給 Claude
client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-opus-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "base64",
                        "media_type": "application/pdf",
                        "data": pdf_data,
                    },
                },
                {"type": "text", "text": "What are the key findings in this document?"},
            ],
        }
    ],
)

print(message.content)

選項 3:Files API

對於您會重複使用的 PDF,或當您想避免編碼開銷時,請使用 Files API

client = anthropic.Anthropic()

# 上傳 PDF 檔案
with open("/path/to/document.pdf", "rb") as f:
    file_upload = client.files.upload(file=("document.pdf", f, "application/pdf"))

# 在訊息中使用已上傳的檔案
message = client.messages.create(
    model="claude-opus-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {"type": "file", "file_id": file_upload.id},
                },
                {"type": "text", "text": "What are the key findings in this document?"},
            ],
        }
    ],
)

print(message.content)

PDF 支援的運作方式

當您將 PDF 傳送給 Claude 時,會發生以下步驟:

  1. 系統擷取文件的內容。

    • 系統將文件的每一頁轉換為影像。
    • 每一頁的文字會被擷取出來,並與該頁的影像一同提供。
  2. Claude 同時分析文字與影像,以更好地理解文件。

    • 文件以文字與影像組合的形式提供以供分析。
    • 這讓使用者能夠針對 PDF 的視覺元素(例如圖表、示意圖及其他非文字內容)尋求見解。
  3. Claude 做出回應,並在相關時引用 PDF 的內容。

    Claude 在回應時可以同時引用文字與視覺內容。您可以透過將 PDF 支援與以下功能整合來進一步提升效能:

估算您的成本

PDF 檔案的 token 數量取決於從文件中擷取的文字總量以及頁數:

  • 文字 token 成本:每頁通常使用 1,500–3,000 個 tokens,視內容密度而定。適用標準 API 定價,無額外 PDF 費用。
  • 影像 token 成本:由於每一頁都會轉換為影像,因此適用相同的基於影像的成本計算

您可以使用 token 計數來估算您特定 PDF 的成本。

最佳化 PDF 處理

提升效能

請遵循以下最佳實務以獲得最佳結果:

  • 在請求中將 PDF 放在文字之前
  • 使用標準字型
  • 確保文字清晰易讀
  • 將頁面旋轉至正確的直立方向
  • 在提示中使用邏輯頁碼(來自 PDF 檢視器)
  • 必要時將大型 PDF 分割成多個區塊
  • 針對重複分析啟用提示快取

擴展您的實作

對於大量處理,請考慮以下方法:

使用提示快取

使用「prompt caching」(提示快取)來快取 PDF,以提升重複查詢的效能,詳見提示快取

import base64
import httpx2

# 首先,載入並編碼 PDF
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
    httpx2.get(pdf_url, follow_redirects=True).content
).decode("utf-8")

# 使用已快取的文件建立訊息
client = anthropic.Anthropic()
message = client.messages.create(
    model="claude-opus-5",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "document",
                    "source": {
                        "type": "base64",
                        "media_type": "application/pdf",
                        "data": pdf_data,
                    },
                    "cache_control": {"type": "ephemeral"},
                },
                {
                    "type": "text",
                    "text": "Which model has the highest human preference win rates across each use-case?",
                },
            ],
        }
    ],
)

print(message.content)

處理文件批次

使用 Message Batches API 在單一請求中處理多個 PDF:

import base64
import httpx2

# 首先,載入並編碼 PDF
pdf_url = "https://assets.anthropic.com/m/1cd9d098ac3e6467/original/Claude-3-Model-Card-October-Addendum.pdf"
pdf_data = base64.standard_b64encode(
    httpx2.get(pdf_url, follow_redirects=True).content
).decode("utf-8")

# 建立一批使用該文件的請求
client = anthropic.Anthropic()
message_batch = client.messages.batches.create(
    requests=[
        {
            "custom_id": "my-first-request",
            "params": {
                "model": "claude-opus-5",
                "max_tokens": 1024,
                "messages": [
                    {
                        "role": "user",
                        "content": [
                            {
                                "type": "document",
                                "source": {
                                    "type": "base64",
                                    "media_type": "application/pdf",
                                    "data": pdf_data,
                                },
                            },
                            {
                                "type": "text",
                                "text": "Which model has the highest human preference win rates across each use-case?",
                            },
                        ],
                    }
                ],
            },
        },
        {
            "custom_id": "my-second-request",
            "params": {
                "model": "claude-opus-5",
                "max_tokens": 1024,
                "messages": [
                    {
                        "role": "user",
                        "content": [
                            {
                                "type": "document",
                                "source": {
                                    "type": "base64",
                                    "media_type": "application/pdf",
                                    "data": pdf_data,
                                },
                            },
                            {
                                "type": "text",
                                "text": "Extract 5 key insights from this document.",
                            },
                        ],
                    }
                ],
            },
        },
    ]
)

print(message_batch)

批次以非同步方式處理。若要檢查進度並在處理結束後擷取結果,請參閱批次處理

後續步驟

Claude 的視覺能力使其能夠理解並分析影像,為多模態互動開啟令人興奮的可能性。

在 Claude Cookbook 食譜中探索 PDF 處理的實用範例。

查看 PDF 支援的完整 API 文件。

Compatibility

Supported platforms
  • Claude API
  • Claude Platform on AWS
  • Amazon Bedrock
  • Google Cloud
  • Microsoft Foundry

Was this page helpful?