PDF to MemoryStream C#
We can export PDF to MemoryStream in C# .NET without even touching the file system. This is possible through the MemoryStream object present inside the System.IO
.NET namespace.
Get started with IronPDF
Start using IronPDF in your project today with a free trial.
How to Convert MemoryStream to PDF in C#
- Download the IronPDF C# library to convert MemoryStream to PDF
- Load an existing PDF as PdfDocument object
- Render a new PDF from a URL or an HTML string/file
- Convert the PDF to a Stream using the
Stream
method and BinaryData property - Serve the MemoryStream to the Web, including MVC and ASP.NET
Save a PDF to Memory
An IronPdf.PdfDocument
can be saved directly to memory in one of two ways:
IronPdf.PdfDocument.Stream
exports the PDF as aSystem.IO.MemoryStream
IronPdf.PdfDocument.BinaryData
exports the PDF as a byte array (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
Serve a PDF to Web from Memory
To serve or export a PDF on the web, you need to send the PDF file as binary data instead of HTML. You can find more information in this guide on exporting and saving PDF documents in C#.
Here is a quick example for MVC and ASP.NET:
Export a PDF with MVC
The stream in the code snippet below is the binary data retrieved from IronPDF. The MIME type of the response is 'application/pdf', specifying the filename as '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
Export a PDF with ASP.NET
Similar to the example above, the stream is the binary data retrieved from IronPDF. The Response is then configured and flushed to ensure that it is sent to the client.
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
Frequently Asked Questions
What is MemoryStream in C#?
MemoryStream is an object in the System.IO namespace of .NET that allows for reading and writing data to a memory buffer instead of a physical file.
How can I convert a PDF to MemoryStream?
You can convert a PDF to MemoryStream using the IronPdf.PdfDocument.Stream property, which exports the PDF as a System.IO.MemoryStream.
How can I serve a PDF to the web from memory in ASP.NET?
In ASP.NET, you can serve a PDF to the web by configuring the Response object to send the MemoryStream as binary data with the correct MIME type, 'application/pdf'.
What MIME type should be used when serving a PDF file?
The MIME type for serving a PDF file is 'application/pdf'.
Can I get a PDF as a byte array?
Yes, IronPDF allows you to retrieve a PDF as a byte array using the IronPdf.PdfDocument.BinaryData property.
Where can I download the library for PDF operations?
You can download IronPDF from the NuGet package manager at https://nuget.org/packages/IronPdf/.
What are the two main methods to save a PDF to memory?
The two main methods to save a PDF to memory using IronPDF are using the PdfDocument.Stream property to export as a MemoryStream and the PdfDocument.BinaryData property to export as a byte array.
Is it possible to manipulate a PDF file in memory without saving it to disk?
Yes, using MemoryStream from the System.IO namespace, you can manipulate PDF files in memory without saving them to disk.
What is the benefit of using MemoryStream for PDF files in web applications?
Using MemoryStream allows you to handle PDF files entirely in memory, which can be more efficient and secure, especially in web applications where you want to avoid interacting with the file system.