如何在C#中使用IronPDF進行OpenAI處理PDF

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

IronPDF的AI擴展啟用OpenAI驅動的PDF增強功能於C#應用程式中。 使用Microsoft Semantic Kernel增加摘要、查詢和備份功能,所需程式碼極少。

Chatgpt related to 如何在C#中使用IronPDF進行OpenAI處理PDF

OpenAI是一個AI研究實驗室,開發先進的人工智慧技術。 它提供通過API存取的強大語言模型,使開發者能夠將AI能力整合到他們的應用程式中。

IronPdf.Extensions.AI NuGet包將OpenAI帶入PDF處理:摘要、查詢和備份。 基於Microsoft Semantic Kernel,此SDK在.NET應用程式中簡化AI服務整合。 自動從PDF文件中提取見解、回答問題並生成摘要。

關鍵使用案例包括處理大量文件、從報告中提取資訊、建立快速審核摘要以及構建智能文件管理系統。 整合支持一次性摘要和持續查詢,適用於各種應用。 欲了解更多PDF功能,請瀏覽IronPDF的全面文件或瞭解從HTML建立PDF

快速開始:使用IronPDF和OpenAI總結PDF

開始將OpenAI整合到您的PDF處理工作流程中,使用IronPDF在C#中。 此範例演示如何使用少量程式碼快速生成PDF摘要。

  1. 使用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訂閱和Azure OpenAI服務存取權限。 該服務為生產應用程式提供企業級安全和合規性。 詳盡的說明請參閱IronPDF安裝概述

我如何用OpenAI總結PDF?

要使用OpenAI功能,配置您的Semantic Kernel及Azure端點和API密鑰。 匯入PDF文件,並使用Summarize方法生成摘要。

摘要功能適用於各種型別的PDF:

  • 掃描文件(結合OCR使用)
  • 含有多列的複雜佈局
  • 包含圖片和表格的文件

IronPDF提取文字內容並通過AI模型處理。 不同格式參見DOCX轉PDFMarkdown轉PDF

請注意
注意:您可能會遇到SKEXP0050錯誤,因為Semantic Kernel方法是實驗性的。 將此新增到您的.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認知搜索、Qdrant等

用於生產時,實現錯誤處理,並新增自定義日誌記錄來追蹤AI操作。 探索異步和多執行緒來同時處理多個文件。

摘要輸出看起來如何?

Visual Studio除錯控制台顯示流行網站技術棧的PDF摘要,包括語言和資料庫

摘要提供了簡明的文件概覽,提取了主要主題、重要事實和相關細節。 AI模型識別並優先考慮重要內容,使您快速理解冗長文件。

Icon Quote related to 摘要輸出看起來如何?

我最喜歡的程式庫是IronPDF。它允許快速高效地操作PDF文件。它還有許多有價值的功能,例如導出到PDF/A格式和數位簽署PDF文件。

Milan Jovanovic related to 摘要輸出看起來如何?

Milan Jovanovic

Microsoft MVP

查看案例研究
Icon Quote related to 摘要輸出看起來如何?

IronOCR意味著我們每年可以從手動處理中節省$40,000,同時提高生產力,釋放資源以進行高影響的任務。我會強烈推薦它。

Brent Matzelle related to 摘要輸出看起來如何?

Brent Matzelle

首席技術官,OPYN

查看案例研究
Icon Quote related to 摘要輸出看起來如何?

IronSuite在我們的運營中扮演著至關重要的角色。這些工具增加了包括建立平面圖和改善庫存管理在內的業務效率。

David Jones related to 摘要輸出看起來如何?

David Jones

首席軟體工程師,Agorus Build

查看案例研究

我如何持續查詢PDF?

單次查詢不適合所有情境。 IronPdf.Extensions.AI套件提供Query方法以進行持續查詢。 構建會話介面、研究工具或文件分析應用,其中使用者可對同一文件提出多次問題。

持續查詢保持會話上下文,允許後續問題和澄清。 理想用於:

  • 客戶支持系統參照文件
  • 法律文件分析需要條款解釋
  • 教育應用程式用於研究複雜材料
  • 研究工具提取特定資訊

為增強處理,考慮分別提取文字和圖片或實施PDF壓縮,以優化大型文件在AI處理前的狀態。

:path=/static-assets/pdf/content-code-examples/how-to/openai-query.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");

// Continuous query
while (true)
{
    Console.Write("User Input: ");
    var response = await pdf.Query(Console.ReadLine());
    Console.WriteLine($"\n{response}");
}
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")

' Continuous query
Do
	Console.Write("User Input: ")
	Dim response = Await pdf.Query(Console.ReadLine())
	Console.WriteLine($vbLf & "{response}")
Loop
$vbLabelText   $csharpLabel

持續查詢系統使用嵌入技術理解問題語義,提供準確、有上下文的回應。 每次查詢都針對文件內容進行處理,由AI保持會話歷史,以提供日益相關的答案。

為了在處理大型文件或同時出現多個使用者時獲得最佳性能,實施快取策略,並探索IronPDF的性能優化技術。 考慮速率限制以及適當的授權金鑰管理,以進行生產部署。

處理敏感文件時,實施適當的安全措施。 在AI處理前後,IronPDF提供各種安全和加密選項以保護PDF。

常見問題

PDF 處理的 AI 擴展的目的是什麼?

IronPdf.Extensions.AI NuGet 套件使得在 C# 應用中啟用 OpenAI 驅動的 PDF 增強。它允許您使用 Microsoft Semantic Kernel 為您的 PDF 新增摘要、查詢和記憶功能,使用最少的程式碼,自動從文件中提取見解並回答問題。

AI 驅動的 PDF 處理的主要使用案例是什麼?

IronPDF 的 AI 擴展非常適合處理大量文件、從報告中提取資訊、建立快速審核摘要和建立智能的文件管理系統。整合支持單次摘要和連續查詢以用於各種應用。

如何快速使用 OpenAI 摘要 PDF?

using 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(用於語義 kernel 功能)。

using OpenAI 與 PDF 之前的先決條件是什麼?

在使用 IronPDF 實現 AI 功能之前,您需要通過具有 Azure OpenAI 服務存取權限的 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 服務連接的複雜性,並提供針對 PDF 的 AI 操作的簡單 API。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 20,296,129 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想快速獲得證明嗎? PM > Install-Package IronPdf
執行範例 看您的HTML變成PDF。