IronPDF 操作指南 PDFs to Memory PDF to MemoryStream C# Curtis Chau 已更新:七月 22, 2025 Download IronPDF NuGet 下载 DLL 下载 Windows 安装程序 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English 我们可以在 C# .NET 中将 PDF 导出到 MemoryStream,而无需触及文件系统。 这可以通过 System.IO .NET 命名空间中的 MemoryStream 对象实现。 快速入门:将 PDF 转换为 MemoryStream 使用 IronPDF 的直观 API,轻松将 PDF 文件转换为 MemoryStream。 本指南帮助开发人员快速开始加载 PDF 并将其导出为 MemoryStream,非常适合无缝集成到 .NET 应用程序中。 遵循这个简单的示例来增强您在 C# 中处理 PDF 的能力。 Get started making PDFs with NuGet now: Install IronPDF with NuGet Package Manager PM > Install-Package IronPdf Copy and run this code snippet. using var stream = new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello Stream!</h1>").Stream; Deploy to test on your live environment Start using IronPDF in your project today with a free trial Free 30 day Trial class="hsg-featured-snippet"> 最小化工作流程(5 步) 下载 IronPDF C# 库以将 MemoryStream 转换为 PDF 将现有 PDF 加载为 PdfDocument 对象 从 URL 或 HTML 字符串/文件渲染新 PDF 使用 Stream 方法和 BinaryData 属性将 PDF 转换为流 将 MemoryStream 提供给 Web,包括 MVC 和 ASP.NET 将 PDF 保存到内存 IronPdf.PdfDocument 可以通过以下两种方式之一直接保存到内存中: IronPdf.PdfDocument.Stream 将 PDF 导出为 System.IO.MemoryStream IronPdf.PdfDocument.BinaryData 将 PDF 导出为字节数组 (byte[]) :path=/static-assets/pdf/content-code-examples/how-to/pdf-to-memory-stream-to-stream.cs using IronPdf; using System.IO; var renderer = new ChromePdfRenderer(); // Convert the URL into PDF PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/"); // Export PDF as Stream MemoryStream pdfAsStream = pdf.Stream; // Export PDF as Byte Array byte[] pdfAsByte = pdf.BinaryData; Imports IronPdf Imports System.IO Private renderer = New ChromePdfRenderer() ' Convert the URL into PDF Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/") ' Export PDF as Stream Private pdfAsStream As MemoryStream = pdf.Stream ' Export PDF as Byte Array Private pdfAsByte() As Byte = pdf.BinaryData $vbLabelText $csharpLabel 从内存中向 Web 提供 PDF 要在网上提供或导出 PDF,您需要将 PDF 文件作为二进制数据而不是 HTML 发送。 您可以在这篇 指南中找到有关在 C# 中导出和保存 PDF 文档的更多信息。 以下是 MVC 和 ASP.NET 的快速示例: 使用 MVC 导出 PDF 下面代码片段中的流是从 IronPDF 获取的二进制数据。 响应的 MIME 类型为 'application/pdf',指定文件名为 'download.pdf'。 using System.Web.Mvc; using System.IO; public ActionResult ExportPdf() { // Assume pdfAsStream is a MemoryStream containing PDF data MemoryStream pdfAsStream = new MemoryStream(); return new FileStreamResult(pdfAsStream, "application/pdf") { FileDownloadName = "download.pdf" }; } using System.Web.Mvc; using System.IO; public ActionResult ExportPdf() { // Assume pdfAsStream is a MemoryStream containing PDF data MemoryStream pdfAsStream = new MemoryStream(); return new FileStreamResult(pdfAsStream, "application/pdf") { FileDownloadName = "download.pdf" }; } Imports System.Web.Mvc Imports System.IO Public Function ExportPdf() As ActionResult ' Assume pdfAsStream is a MemoryStream containing PDF data Dim pdfAsStream As New MemoryStream() Return New FileStreamResult(pdfAsStream, "application/pdf") With {.FileDownloadName = "download.pdf"} End Function $vbLabelText $csharpLabel 使用 ASP.NET 导出 PDF 与上面的示例类似,流是从 IronPDF 获取的二进制数据。 然后配置并刷新响应以确保其发送到客户端。 using System.IO; using System.Web; public class PdfHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { // Assume pdfAsStream is a MemoryStream containing PDF data MemoryStream pdfAsStream = new MemoryStream(); context.Response.Clear(); context.Response.ContentType = "application/octet-stream"; context.Response.OutputStream.Write(pdfAsStream.ToArray(), 0, (int)pdfAsStream.Length); context.Response.Flush(); } public bool IsReusable => false; } using System.IO; using System.Web; public class PdfHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { // Assume pdfAsStream is a MemoryStream containing PDF data MemoryStream pdfAsStream = new MemoryStream(); context.Response.Clear(); context.Response.ContentType = "application/octet-stream"; context.Response.OutputStream.Write(pdfAsStream.ToArray(), 0, (int)pdfAsStream.Length); context.Response.Flush(); } public bool IsReusable => false; } Imports System.IO Imports System.Web Public Class PdfHandler Implements IHttpHandler Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest ' Assume pdfAsStream is a MemoryStream containing PDF data Dim pdfAsStream As New MemoryStream() context.Response.Clear() context.Response.ContentType = "application/octet-stream" context.Response.OutputStream.Write(pdfAsStream.ToArray(), 0, CInt(pdfAsStream.Length)) context.Response.Flush() End Sub Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable Get Return False End Get End Property End Class $vbLabelText $csharpLabel 常见问题解答 我如何在C#中将PDF转换为MemoryStream? 您可以使用IronPDF在C#中将PDF转换为MemoryStream。将您的PDF加载为PdfDocument对象,并使用PdfDocument.Stream属性将其导出为System.IO.MemoryStream。 将PDF保存到内存的方法有哪些? IronPDF提供两种将PDF保存到内存的方法:使用PdfDocument.Stream导出为MemoryStream和PdfDocument.BinaryData导出为字节数组。 如何在ASP.NET应用程序中以二进制数据形式提供PDF? 在ASP.NET应用程序中,您可以通过配置Response对象发送带有MIME类型'application/pdf'的MemoryStream来提供PDF。 在C#中是否可以从HTML创建PDF? 是的,您可以使用IronPDF的PdfDocument对象从HTML字符串或文件渲染PDF,然后将其转换为MemoryStream。 如何在网络应用程序中处理内存中的PDF文件? 您可以使用IronPDF在网络应用程序中完全在内存中处理PDF文件,将PDF导出为MemoryStream并直接提供给客户端,避免文件系统交互。 在.NET中使用MemoryStream进行PDF操作有哪些好处? 在.NET中使用MemoryStream进行PDF操作可以实现高效的内存管理,并通过将数据保存在内存中而避免磁盘写入,从而增加安全性,这在网络应用程序中尤其有用。 我可以将MemoryStream转换回PDF文件吗? 是的,包含PDF数据的MemoryStream可以使用IronPDF将流的内容写入文件来保存回PDF文件。 如何在MVC网络应用程序中导出PDF文件? 在MVC网络应用程序中,您可以使用包含PDF数据的MemoryStream进行导出,并将其作为FileStreamResult返回,设置MIME类型为'application/pdf'。 IronPDF 是否完全兼容 .NET 10 的内存流操作? 是的——IronPDF 正式兼容 .NET 10。该库支持 .NET 10 以及 .NET 9、8、7、6、5 和 .NET Standard 2.0+ 版本。您可以在 .NET 10 项目中使用PdfDocument.Stream或PdfDocument.BinaryData ,无需任何额外配置。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:16,154,058 查看许可证