MemoryStream to PDF C#

We can load and create MemoryStream to PDF files 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



Load a PDF from Memory

A new instance of IronPdf.PdfDocument can be initialized from any of the following .NET in-memory objects:

  • A MemoryStream
  • A FileStream
  • Binary data as a byte array (byte[])

Below is an example of reading a stream directly from a PDF file and creating a PdfDocument object from it using C#:

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

// Read PDF file as stream
var fileByte = File.ReadAllBytes("sample.pdf");

// Instantiate PDF object from stream
PdfDocument pdf = new PdfDocument(fileByte);
Imports IronPdf
Imports System.IO

' Read PDF file as stream
Private fileByte = File.ReadAllBytes("sample.pdf")

' Instantiate PDF object from stream
Private pdf As New PdfDocument(fileByte)
VB   C#

The provided example demonstrates how to read a PDF file directly from the file system and create a PdfDocument object. However, you can also initialize a PdfDocument from a byte array received via network communication or any other data exchange protocol. This allows you to transform the PDF data into an editable object, enabling you to make modifications as needed.