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.

First Step:
green arrow pointer



Save a PDF to Memory

An IronPdf.PdfDocument can be saved directly to memory in one of two ways:

:path=/static-assets/pdf/content-code-examples/how-to/pdf-to-memory-stream-to-stream.cs
// Required namespace for IronPdf functionalities
using IronPdf;
using System.IO;

// Create an instance of ChromePdfRenderer, which is responsible for rendering URLs as PDF documents.
var renderer = new ChromePdfRenderer();

// Convert the specified URL into a PDF document. The RenderUrlAsPdf method fetches the content from the URL
// and renders it into a PDF format.
PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");

// Export the generated PDF as a MemoryStream. This allows for manipulations 
// or further operations on the PDF using the stream, such as saving to a file or sending over a network.
MemoryStream pdfAsStream = pdf.Stream;

// Export the generated PDF as a byte array. This is useful for scenarios where 
// the PDF needs to be stored in a database or transmitted as raw bytes.
byte[] pdfAsByte = pdf.BinaryData;
' Required namespace for IronPdf functionalities
Imports IronPdf
Imports System.IO

' Create an instance of ChromePdfRenderer, which is responsible for rendering URLs as PDF documents.
Private renderer = New ChromePdfRenderer()

' Convert the specified URL into a PDF document. The RenderUrlAsPdf method fetches the content from the URL
' and renders it into a PDF format.
Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")

' Export the generated PDF as a MemoryStream. This allows for manipulations 
' or further operations on the PDF using the stream, such as saving to a file or sending over a network.
Private pdfAsStream As MemoryStream = pdf.Stream

' Export the generated PDF as a byte array. This is useful for scenarios where 
' the PDF needs to be stored in a database or transmitted as raw bytes.
Private pdfAsByte() As Byte = pdf.BinaryData
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

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
$vbLabelText   $csharpLabel

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.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.