如何在 C# 中使用 OpenAI 處理 PDF 檔案(搭配 IronPDF)

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF 的 AI 擴充套件可在 C# 應用程式中實現由 OpenAI 驅動的 PDF 增強功能。 使用 Microsoft Semantic Kernel,以最少的程式碼即可新增摘要、查詢和記憶功能。

Chatgpt related to 如何在 C# 中使用 OpenAI 處理 PDF 檔案(搭配 IronPDF)

OpenAI 是一家致力於開發先進人工智慧技術的人工智慧研究實驗室。 它提供可透過 API 存取的強大語言模型,讓開發人員能將 AI 功能整合至其應用程式中。

IronPdf.Extensions.AI NuGet 套件將 OpenAI 技術導入 PDF 處理領域:摘要生成、查詢及記憶功能。 此 SDK 基於 Microsoft Semantic Kernel 打造,可簡化 AI 服務在 .NET 應用程式中的整合。 自動從 PDF 文件中提取洞見、回答問題並生成摘要。

主要應用情境包括處理大量文件、從報告中提取資訊、建立快速檢視摘要,以及建置智慧型文件管理系統。 此整合功能同時支援一次性摘要生成與持續查詢,適用於各種應用場景。 如需更多 PDF 功能,請參閱 IronPDF 的完整文件,或瞭解如何從 HTML 建立 PDF 檔案

快速入門:使用 IronPDF 與 OpenAI 摘要 PDF 文件

立即開始使用 C# 中的 IronPDF,將 OpenAI 整合至您的 PDF 處理工作流程中。 此範例展示如何僅透過幾行程式碼快速執行 PDF 摘要生成。

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 請複製並執行此程式碼片段。

    // Install-Package IronPdf.Extensions.AI
    await IronPdf.AI.PdfAIEngine.Summarize("input.pdf", "summary.txt", azureEndpoint, azureApiKey);
  3. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronPDF

    arrow pointer


所需套件:

在實作 AI 功能之前,請先設定 Azure OpenAI。 您需要一個具備 Azure OpenAI Service 存取權限的 Azure 訂閱。 本服務為生產環境應用程式提供企業級的安全性與合規性保障。 請參閱 IronPDF 安裝概覽以獲取詳細說明。

如何使用 OpenAI 摘要 PDF 文件?

若要使用 OpenAI 功能,請使用您的 Azure 端點和 API 金鑰來設定 Semantic Kernel。 匯入 PDF 文件,並使用 Summarize 方法生成摘要。

摘要功能支援多種 PDF 格式:

  • 掃描文件(搭配 OCR 使用時)
  • 包含多欄位的複雜版面配置
  • 包含圖片與表格的文件

IronPDF 會擷取文字內容,並透過 AI 模型進行處理。 如需其他格式轉換,請參閱 DOCX 轉 PDFMarkdown 轉 PDF

請注意
注意:由於 Semantic Kernel 的方法尚屬實驗性質,您可能會遇到 SKEXP0010SKEXP0050 等錯誤。 將此內容加入您的 .csproj 檔案中以隱藏這些內容:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <NoWarn>$(NoWarn);SKEXP0001,SKEXP0010,SKEXP0050</NoWarn>
  </PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <NoWarn>$(NoWarn);SKEXP0001,SKEXP0010,SKEXP0050</NoWarn>
  </PropertyGroup>
</Project>
XML

)}]

以下是使用 C# 透過 Semantic Kernel 摘要 PDF 的方法:

:path=/static-assets/pdf/content-code-examples/how-to/openai-summarize.cs
using IronPdf;
using IronPdf.AI;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Memory;
using System;
using System.Threading.Tasks;

// Setup OpenAI
var azureEndpoint = "<<enter your azure endpoint here>>";
var apiKey = "<<enter your azure API key here>>";
var builder = Kernel.CreateBuilder()
    .AddAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey)
    .AddAzureOpenAIChatCompletion("oaichat", azureEndpoint, apiKey);
var kernel = builder.Build();

// Setup Memory
var memory_builder = new MemoryBuilder()
    // optionally use new ChromaMemoryStore("http://127.0.0.1:8000") (see https://github.com/microsoft/semantic-kernel/blob/main/dotnet/notebooks/09-memory-with-chroma.ipynb)
    .WithMemoryStore(new VolatileMemoryStore())
    .WithAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey);
var memory = memory_builder.Build();

// Initialize IronAI
IronDocumentAI.Initialize(kernel, memory);

License.LicenseKey = "<<enter your IronPdf license key here";

// Import PDF document
PdfDocument pdf = PdfDocument.FromFile("wikipedia.pdf");

// Summarize the document
Console.WriteLine("Please wait while I summarize the document...");
string summary = await pdf.Summarize(); // optionally pass AI instance or use AI instance directly
Console.WriteLine($"Document summary: {summary}\n\n");
Imports Microsoft.VisualBasic
Imports IronPdf
Imports IronPdf.AI
Imports Microsoft.SemanticKernel
Imports Microsoft.SemanticKernel.Connectors.OpenAI
Imports Microsoft.SemanticKernel.Memory
Imports System
Imports System.Threading.Tasks

' Setup OpenAI
Private azureEndpoint = "<<enter your azure endpoint here>>"
Private apiKey = "<<enter your azure API key here>>"
Private builder = Kernel.CreateBuilder().AddAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey).AddAzureOpenAIChatCompletion("oaichat", azureEndpoint, apiKey)
Private kernel = builder.Build()

' Setup Memory
Private memory_builder = (New MemoryBuilder()).WithMemoryStore(New VolatileMemoryStore()).WithAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey)
Private memory = memory_builder.Build()

' Initialize IronAI
IronDocumentAI.Initialize(kernel, memory)

License.LicenseKey = "<<enter your IronPdf license key here"

' Import PDF document
Dim pdf As PdfDocument = PdfDocument.FromFile("wikipedia.pdf")

' Summarize the document
Console.WriteLine("Please wait while I summarize the document...")
Dim summary As String = Await pdf.Summarize() ' optionally pass AI instance or use AI instance directly
Console.WriteLine($"Document summary: {summary}" & vbLf & vbLf)
$vbLabelText   $csharpLabel

該程式碼同時初始化 Semantic Kernel 與記憶體儲存區。 記憶體儲存區能在連續查詢過程中維持上下文。 請從以下選項中選擇:

  • VolatileMemoryStore:用於開發與測試的記憶體內儲存
  • ChromaMemoryStore:適用於生產環境的持久化向量資料庫
  • 其他商店:Azure Cognitive 搜尋、Qdrant 等

在正式部署時,請實作錯誤處理機制並加入自訂記錄功能,以追蹤 AI 運作狀況。 探索非同步與多執行緒技術,以同時處理多份文件。

摘要輸出的樣式為何?

Visual Studio 除錯主控台顯示熱門網站技術堆疊的 PDF 摘要,內容包含程式語言與資料庫

摘要提供了一份簡明的文件概覽,提煉出主要主題、重要事實及相關細節。 AI 模型能識別並優先處理重要內容,使讀者能快速理解冗長的文件。

如何持續查詢 PDF 檔案?

單一查詢無法適用於所有情境。 IronPdf.Extensions.AI 套件提供了一種 Query 方法,用於執行連續查詢。 建立對話式介面、研究工具或文件分析應用程式,讓使用者能針對同一份文件提出多個問題。

持續提問可維持對話脈絡,以便進行後續提問與澄清。 適用於:

  • 參考文件中的客戶支援系統
  • 需解讀條款的法律文件分析
  • 用於研讀複雜教材的教育應用程式
  • 用於擷取特定資訊的研究工具

為提升處理效能,建議在 AI 處理前,先將文字與圖片分開提取,或對大型文件實施 PDF 壓縮以進行優化。

:path=/static-assets/pdf/content-code-examples/how-to/openai-summarize.cs
using IronPdf;
using IronPdf.AI;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Memory;
using System;
using System.Threading.Tasks;

// Setup OpenAI
var azureEndpoint = "<<enter your azure endpoint here>>";
var apiKey = "<<enter your azure API key here>>";
var builder = Kernel.CreateBuilder()
    .AddAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey)
    .AddAzureOpenAIChatCompletion("oaichat", azureEndpoint, apiKey);
var kernel = builder.Build();

// Setup Memory
var memory_builder = new MemoryBuilder()
    // optionally use new ChromaMemoryStore("http://127.0.0.1:8000") (see https://github.com/microsoft/semantic-kernel/blob/main/dotnet/notebooks/09-memory-with-chroma.ipynb)
    .WithMemoryStore(new VolatileMemoryStore())
    .WithAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey);
var memory = memory_builder.Build();

// Initialize IronAI
IronDocumentAI.Initialize(kernel, memory);

License.LicenseKey = "<<enter your IronPdf license key here";

// Import PDF document
PdfDocument pdf = PdfDocument.FromFile("wikipedia.pdf");

// Summarize the document
Console.WriteLine("Please wait while I summarize the document...");
string summary = await pdf.Summarize(); // optionally pass AI instance or use AI instance directly
Console.WriteLine($"Document summary: {summary}\n\n");
Imports Microsoft.VisualBasic
Imports IronPdf
Imports IronPdf.AI
Imports Microsoft.SemanticKernel
Imports Microsoft.SemanticKernel.Connectors.OpenAI
Imports Microsoft.SemanticKernel.Memory
Imports System
Imports System.Threading.Tasks

' Setup OpenAI
Private azureEndpoint = "<<enter your azure endpoint here>>"
Private apiKey = "<<enter your azure API key here>>"
Private builder = Kernel.CreateBuilder().AddAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey).AddAzureOpenAIChatCompletion("oaichat", azureEndpoint, apiKey)
Private kernel = builder.Build()

' Setup Memory
Private memory_builder = (New MemoryBuilder()).WithMemoryStore(New VolatileMemoryStore()).WithAzureOpenAITextEmbeddingGeneration("oaiembed", azureEndpoint, apiKey)
Private memory = memory_builder.Build()

' Initialize IronAI
IronDocumentAI.Initialize(kernel, memory)

License.LicenseKey = "<<enter your IronPdf license key here"

' Import PDF document
Dim pdf As PdfDocument = PdfDocument.FromFile("wikipedia.pdf")

' Summarize the document
Console.WriteLine("Please wait while I summarize the document...")
Dim summary As String = Await pdf.Summarize() ' optionally pass AI instance or use AI instance directly
Console.WriteLine($"Document summary: {summary}" & vbLf & vbLf)
$vbLabelText   $csharpLabel

該連續查詢系統利用嵌入向量來理解問題的語義,從而提供精準且符合上下文的回應。 每個查詢皆針對文件內容進行處理,AI 會保留對話歷史紀錄,以提供越來越切合需求的答案。

若需處理大型文件或應對多用戶同時存取以獲得最佳效能,請實施快取策略並參考 IronPDF 的效能優化技巧。 針對生產環境部署,請考量速率限制及適當的授權金鑰管理

處理敏感文件時,請實施適當的安全措施。 IronPDF 提供多種安全與加密選項,可在 AI 處理前後保護 PDF 檔案。

常見問題

PDF 處理用 AI 擴充功能的目的為何?

IronPdf.Extensions.AI NuGet 套件可在 C# 應用程式中實現由 OpenAI 驅動的 PDF 增強功能。透過 Microsoft Semantic Kernel,您只需極少的程式碼即可為 PDF 文件添加摘要、查詢和記憶功能,協助自動從文件中提取洞見並回答問題。

AI 驅動的 PDF 處理有哪些主要應用場景?

IronPDF 的 AI 擴充功能非常適合處理大量文件、從報告中擷取資訊、建立快速檢視摘要,以及建置智慧型文件管理系統。此整合功能同時支援一次性摘要生成與持續查詢,可應用於各種情境。

如何使用 OpenAI 快速摘要 PDF 文件?

透過 IronPDF 的 AI 擴充功能,您只需一行程式碼即可摘要任何 PDF 文件:await IronPdf.AI.PdfAIEngine.Summarize("input.pdf", "summary.txt", azureEndpoint, azureApiKey)。這項簡單的實作方式,讓您能輕鬆地從 PDF 文件中產生摘要。

進行 AI PDF 處理需要安裝哪些套件?

若要透過 IronPDF 實作 AI 功能,您需要三個套件:IronPdf(主要 PDF 函式庫)、IronPdf.Extensions.AI(AI 擴充套件)以及 Microsoft.SemanticKernel.Plugins.Memory(用於語義核心功能)。

using OpenAI 處理 PDF 檔案有哪些先決條件?

在 IronPDF 中實作 AI 功能之前,您需要使用具備 Azure OpenAI Service 存取權限的 Azure 訂閱來設定 Azure OpenAI。該服務為生產環境應用程式提供企業級的安全性和合規性,需要一個 Azure 端點和 API 金鑰。

AI PDF 處理的最小工作流程為何?

using IronPDF 的最簡工作流程包含 5 個步驟:1) 下載 C# 函式庫,2) 準備 Azure 端點與 API 金鑰,3) 匯入目標 PDF 文件,4) 使用 Summarize 方法產生摘要,以及 5) 使用 Query 方法進行持續查詢。

AI 擴充功能如何與 Microsoft Semantic Kernel 整合?

IronPDF 的 AI 擴充套件建構於 Microsoft Semantic Kernel 之上,可簡化 .NET 應用程式中 AI 服務的整合。此 SDK 處理了連線至 OpenAI 服務的複雜性,並提供直觀的 API 來執行 PDF 專用的 AI 操作。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 19,014,616 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。