如何使用 IronPDF 在 C# 中将 OpenAI 用于 PDF.

This article was translated from English: Does it need improvement?
Translated
View the article in English
Chatgpt related to 如何使用 IronPDF 在 C# 中将 OpenAI 用于 PDF.

OpenAI 是一个人工智能研究实验室,由盈利的 OpenAI LP 和其非营利母公司 OpenAI Inc 组成。其成立的目的是以一种对整个人类有益的方式推进数字智能。 OpenAI 在人工智能(AI)的各个领域进行研究,旨在开发安全、有益且可访问的 AI 技术。

现在,通过 NuGet 包 IronPdf.Extensions.AI 启用 OpenAI 进行 PDF 增强:摘要、查询和记忆。 该包利用了微软的 Semantic Kernel

快速入门:使用 IronPDF 和 OpenAI 总结 PDF

使用 C# 中的 IronPDF 开始将 OpenAI 集成到您的 PDF 处理工作流中。 这个简单的例子展示了如何快速生成 PDF 文档的摘要。 只需几行代码,您即可轻松利用 AI 增强您的 PDF 功能。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 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


除了 IronPDF 包外,您还需要以下两个包:

总结 PDF 示例

要使用 OpenAI 功能,需要 Azure Endpoint 和 API 密钥。 根据以下代码示例配置 Semantic Kernel。 导入 PDF 文档并利用 Summarize 方法生成 PDF 文档的摘要。

[{i:( 注意:您可能会遇到 SKEXP0001、SKEXP0010 和 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

@@--bracket-close--@@@

以下是如何使用 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

输出摘要

总结 PDF 文档

连续查询示例

单次查询可能不适用于所有情况。 NuGet 包 IronPdf.Extensions.AI 还提供了一个 Query 方法,使用户能够执行连续查询。

: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

常见问题解答

如何使用 OpenAI 在 C# 中增强 PDF 文档?

在 C# 中增强 PDF 文档,您可以使用 `IronPdf.Extensions.AI` NuGet 软件包,该包启用诸如摘要和查询等功能,并借助 OpenAI 实现。这涉及使用 Azure 端点和 API 密钥以进行 OpenAI 集成。

集成 OpenAI 与 C# 中的 PDF 处理需要什么?

要将 OpenAI 集成到 C# 中的 PDF 处理中,您需要 `IronPdf` 和 `IronPdf.Extensions.AI` 软件包、Microsoft 语义内核、Azure 端点和 API 密钥。

如何使用 OpenAI 在 C# 中总结 PDF 文档?

您可以使用 `IronPdf.Extensions.AI` 软件包中的 `Summarize` 方法对 PDF 文档进行摘要。导入您的 PDF 文档,并通过提供 Azure 端点和 API 密钥来应用此方法。

我可以在 C# 中使用 AI 对 PDF 进行连续查询吗?

是的,您可以使用 `IronPdf.Extensions.AI` 软件包中的 `Query` 方法对 PDF 进行连续查询,从而允许动态提取 PDF 文档中的信息。

如何在 C# 项目中禁止实验性错误警告?

要禁止诸如 SKEXP0001、SKEXP0010 和 SKEXP0050 的实验性错误警告,请将以下代码添加到您的 .csproj 文件中:<NoWarn>$(NoWarn);SKEXP0001,SKEXP0010,SKEXP0050</NoWarn>

Microsoft 语义内核在 PDF 增强中扮演什么角色?

Microsoft 语义内核用于配置和运行 `Summarize` 和 `Query` 等方法在 PDF 文档上的应用,通过 `IronPdf.Extensions.AI` 软件包启用 OpenAI 功能。

使用 OpenAI 进行 PDF 摘要的好处是什么?

使用 OpenAI 进行 PDF 摘要可提供大型文档的简明摘要,更容易快速提取关键信息。这是通过 `IronPdf.Extensions.AI` 软件包中的 `Summarize` 方法实现的。

使用 OpenAI 扩展时,IronPDF 是否兼容 .NET 10?

是的——IronPDF 完全兼容 .NET 10,`IronPdf.Extensions.AI` 包无需特殊配置即可在 .NET 10 项目中运行。该库支持 .NET 10 中引入的最新运行时改进和语言特性。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 16,493,056 | Version: 2025.11 刚刚发布