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
How can I convert a PDF to a MemoryStream in C#?
You can convert a PDF to a MemoryStream in C# by using IronPDF. Load your PDF as a PdfDocument
object, and use the PdfDocument.Stream
property to export it as a System.IO.MemoryStream
.
What are the methods to save a PDF to memory using IronPDF?
IronPDF provides two methods for saving a PDF to memory: using PdfDocument.Stream
to export as a MemoryStream
and PdfDocument.BinaryData
to export as a byte array.
How can I serve a PDF as binary data in an ASP.NET application?
In an ASP.NET application, you can serve a PDF as binary data by configuring the Response
object to send the MemoryStream
with the MIME type 'application/pdf'.
Is it possible to create a PDF from HTML in C#?
Yes, you can render a PDF from an HTML string or file using IronPDF's PdfDocument
object, which can then be converted into a MemoryStream
.
How do I handle PDF files in memory for web applications?
You can handle PDF files entirely in memory for web applications using IronPDF by exporting the PDF to a MemoryStream
and serving it directly to clients, avoiding file system interactions.
What are the benefits of using MemoryStream for PDF operations in .NET?
Using MemoryStream
for PDF operations in .NET allows for efficient memory management and increased security by keeping data in memory and avoiding disk writes, which is especially useful in web applications.
Can I convert a MemoryStream back to a PDF file in C#?
Yes, a MemoryStream
containing PDF data can be saved back to a PDF file using IronPDF by writing the stream's contents to a file.
How can I export a PDF file in an MVC web application?
In an MVC web application, you can export a PDF by using a MemoryStream
containing the PDF data and returning it as a FileStreamResult
, setting the MIME type to 'application/pdf'.