如何在 C# 中导出 PDF | IronPDF

C# Export to PDF Code Example Tutorial

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

IronPDF 是一个C# PDF 库,允许您使用 C# 将 HTML 保存为 PDF。 它还允许 C# / VB 开发人员以编程方式编辑 PDF 文档。

标题:2(快速入门:使用 IronPDF 将 HTML 导出为 C# 中的 PDF)

使用 IronPDF 轻松地将 HTML 内容导出为 C# 中的 PDF。 本快速指南向您展示如何将 HTML 转换为 PDF 文档,并通过几行代码保存它。 IronPDF 简化了 PDF 的生成,让开发人员可以轻松地将 PDF 导出功能集成到他们的应用程序中,无需麻烦。 深入了解并看看开始是多么简单!

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>HelloPDF</h1>").SaveAs("myExportedFile.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步)

  1. 从 NuGet 下载并安装 C# PDF 导出库
  2. 浏览 PdfDocument 文档以发现数字签名导出 PDF 的方法
  3. 使用 System.IO.MemoryStream 将 PDF 保存到内存
  4. 将 PDF 作为二进制数据而不是 HTML 提供给网络
  5. 将 PDF 导出为文件


保存 PDF 的选项

如何将 PDF 保存到磁盘

使用PdfDocument.SaveAs方法将 PDF 保存到磁盘。

您会发现此方法支持添加密码保护。 查看以下文章以了解更多关于数字签名导出 PDF 的信息:'数字签名 PDF 文档.'

如何将 PDF 文件保存到 C# 中的 MemoryStream (System.IO.MemoryStream)

IronPdf.PdfDocument.Stream属性使用System.IO.MemoryStream将 PDF 保存到内存。

如何保存为二进制数据

IronPdf.PdfDocument.BinaryData属性将 PDF 文档导出为内存中的二进制数据。

这会将 PDF 作为ByteArray输出,在 C# 中表示为byte []

如何从 Web 服务器提供给浏览器

要将 PDF 提供给网络,我们需要将其作为二进制数据而不是 HTML 发送。

MVC PDF 导出

// Sends 'stream' to the client as a file download with the specified name.
return new FileStreamResult(stream, "application/pdf")
{
    FileDownloadName = "file.pdf"
};
// Sends 'stream' to the client as a file download with the specified name.
return new FileStreamResult(stream, "application/pdf")
{
    FileDownloadName = "file.pdf"
};
' Sends 'stream' to the client as a file download with the specified name.
Return New FileStreamResult(stream, "application/pdf") With {.FileDownloadName = "file.pdf"}
$vbLabelText   $csharpLabel

ASP.NET PDF 导出

// Retrieves the PDF binary data
byte[] Binary = MyPdfDocument.BinaryData;

// Clears the existing response content
Response.Clear();

// Sets the response content type to 'application/octet-stream', suitable for PDF files
Response.ContentType = "application/octet-stream";

// Writes the binary data to the response output stream
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);

// Flushes the response to send the data to the client
Response.Flush();
// Retrieves the PDF binary data
byte[] Binary = MyPdfDocument.BinaryData;

// Clears the existing response content
Response.Clear();

// Sets the response content type to 'application/octet-stream', suitable for PDF files
Response.ContentType = "application/octet-stream";

// Writes the binary data to the response output stream
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);

// Flushes the response to send the data to the client
Response.Flush();
' Retrieves the PDF binary data
Dim Binary() As Byte = MyPdfDocument.BinaryData

' Clears the existing response content
Response.Clear()

' Sets the response content type to 'application/octet-stream', suitable for PDF files
Response.ContentType = "application/octet-stream"

' Writes the binary data to the response output stream
Context.Response.OutputStream.Write(Binary, 0, Binary.Length)

' Flushes the response to send the data to the client
Response.Flush()
$vbLabelText   $csharpLabel

常见问题解答

如何在 C# 中导出 PDF?

要在 C# 中导出 PDF,您可以使用 IronPDF 库。首先,从 NuGet 下载并安装 C# PDF 导出库。然后,探索 `PdfDocument` 方法以将 PDF 保存到磁盘或内存中,并将其发送到网络浏览器。

使用 IronPDF 保存 PDF 的选项有哪些?

IronPDF 允许您使用几种方法保存 PDF:使用 PdfDocument.SaveAs 保存到磁盘,使用 System.IO.MemoryStream 保存到内存中,或使用 PdfDocument.BinaryData 以二进制数据的形式导出。

如何在 C# 中将 PDF 保存到磁盘?

使用 IronPDF 中的 PdfDocument.SaveAs 方法将 PDF 保存到磁盘。此方法还支持为您的文档添加密码保护等功能。

如何在 C# 中将 PDF 保存到 MemoryStream 中?

在 IronPDF 中,您可以使用 IronPdf.PdfDocument.Stream 属性将 PDF 保存到内存中,这利用了 System.IO.MemoryStream 来有效地处理 PDF 数据。

如何在 C# 中将 PDF 导出为二进制数据?

IronPDF 中的 PdfDocument.BinaryData 属性允许您将 PDF 文档导出为内存中的二进制数据,以 byte[] 数组表示。

如何使用 IronPDF 从网络服务器向浏览器传送 PDF?

要使用 IronPDF 从网络服务器向浏览器传送 PDF,您应将其作为二进制数据发送。在 MVC 中,您可以使用 FileStreamResult,而在 ASP.NET 中,您需要将二进制数据直接写入响应流。

System.IO.MemoryStream 在使用 IronPDF 处理 PDF 中有什么作用?

System.IO.MemoryStream 在 IronPDF 中用于将 PDF 文件保存到内存中,从而无需立即进行磁盘存储,便能有效管理 PDF 数据。

如何在 MVC 中使用 IronPDF 发送 PDF 作为文件下载?

在 MVC 应用程序中,您可以使用 FileStreamResult 类发送 PDF 作为文件下载。这会将 PDF 数据流发送到客户端以下载,文件名可指定。

.NET 10 兼容性:IronPDF 是否可以在 .NET 10 项目中完全使用?

是的。IronPDF 完全支持 .NET 10,无需特殊配置或变通方法,即可在跨平台(Windows、Linux、macOS)的 .NET 10 项目中开箱即用。

Curtis Chau
技术作家

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

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

审核者
Jeff Fritz
Jeffrey T. Fritz
首席项目经理 - .NET 社区团队
Jeff 也是 .NET 和 Visual Studio 团队的首席项目经理。他是 .NET Conf 虚拟会议系列的执行制片人,并主持“Fritz and Friends”直播节目,每周两次与观众一起谈论技术并编写代码。Jeff 撰写研讨会、演示文稿并计划包括 Microsoft Build、Microsoft Ignite、.NET Conf 和 Microsoft MVP 峰会在内的最大型微软开发者活动的内容。
准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布