MemoryStream to PDF C#

We can load, create, and export 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. Follow the tutorial below to find out how to export HTML to PDF in your C# project.

Load PDF from Memory
​    private void ReadViaMemStream()

​    {

​      //Conversion of the URL into PDF

​      var Renderer = new IronPdf.HtmlToPdf();

​      Uri url = new Uri("https://ironpdf.com/docs/questions/pdf-memory-stream/");

 

​      MemoryStream rend = Renderer.RenderUrlAsPdf(url).Stream; //Read stream

 

​      HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf"); //Download

 

​      HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("{0}; filename=Print.pdf; size={1}", "attachment", rend.Length.ToString()));

 

​      // Write PDF buffer to HTTP response

​      HttpContext.Current.Response.BinaryWrite(rend.ToArray());

 

​      // Stop page processing

​      HttpContext.Current.Response.End();

​    }
​ Private Sub ReadViaMemStream()

​ ​ var Renderer = New IronPdf.HtmlToPdf()

​ Uri url = New Uri("https://ironpdf.com/docs/questions/pdf-memory-stream/")



​ MemoryStream rend = Renderer.RenderUrlAsPdf(url).Stream 'Read stream



​ HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf") 'Download



​ HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("{0}; filename=Print.pdf; size={1}", "attachment", rend.Length.ToString()))



​ ​ HttpContext.Current.Response.BinaryWrite(rend.ToArray())



​ ​ HttpContext.Current.Response.End()

'INSTANT VB TODO TASK: The following line uses invalid syntax:
'​ }
Jump to Article
Export PDF with MVC
return new FileStreamResult(stream, “application/pdf”)

{

   FileDownloadName = “downloadedfile.pdf”

};
Return New FileStreamResult(stream, "application/pdf") With {.FileDownloadName = "downloadedfile.pdf"}
Jump to Article
Export PDF with ASPNET
byte[] Binary = MyPDFDocument.BinaryData;

Response.Clear();

Response.ContentType = “application/octet-stream”;

Context.Response.OutputStream.Write(Binary, 0, Binary.Length);

Response.Flush();
Dim Binary() As Byte = MyPDFDocument.BinaryData

Response.Clear()

Response.ContentType = "application/octet-stream"

Context.Response.OutputStream.Write(Binary, 0, Binary.Length)

Response.Flush()
Jump to Article
Try IronPDF free for development


Step 1

1. Install IronPDF

In this tutorial, we'll be using IronPDF's C# PDF Library to save MemoryStream files to PDF in C#. You can download it free or install to Visual Studio with NuGet.


 PM > Install-Package IronPdf


How to Tutorial

2. Load a PDF from Memory

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

  • A MemoryStream
  • A FileSteam
  • Binary Data as a Byte array (byte[])

Here is an example of reading a URL directly into a stream, then saving the PDF file to disk by using C#:

​    private void ReadViaMemStream()

​    {

​      //Conversion of the URL into PDF

​      var Renderer = new IronPdf.HtmlToPdf();

​      Uri url = new Uri("https://ironpdf.com/docs/questions/pdf-memory-stream/");

​      MemoryStream rend = Renderer.RenderUrlAsPdf(url).Stream; //Read stream

​      HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf"); //Download

​      HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("{0}; filename=Print.pdf; size={1}", "attachment", rend.Length.ToString()));

​      // Write PDF buffer to HTTP response

​      HttpContext.Current.Response.BinaryWrite(rend.ToArray());

​      // Stop page processing

​      HttpContext.Current.Response.End();

​    }
​ Private Sub ReadViaMemStream()

​ ​ var Renderer = New IronPdf.HtmlToPdf()

​ Uri url = New Uri("https://ironpdf.com/docs/questions/pdf-memory-stream/")

​ MemoryStream rend = Renderer.RenderUrlAsPdf(url).Stream 'Read stream

​ HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf") 'Download

​ HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("{0}; filename=Print.pdf; size={1}", "attachment", rend.Length.ToString()))

​ ​ HttpContext.Current.Response.BinaryWrite(rend.ToArray())

​ ​ HttpContext.Current.Response.End()

'INSTANT VB TODO TASK: The following line uses invalid syntax:
'​ }
VB   C#


3. Save a PDF to Memory

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


4. Serve a PDF to Web from Memory

To serve or export a PDF to the web, you need to send the PDF file as binary data instead of HTML. You can find out more here about saving & exporting PDF documents in C#.

Here is a quick example for MVC and ASP.NET:

4.1. Export a PDF with MVC


return new FileStreamResult(stream, “application/pdf”)

{

   FileDownloadName = “downloadedfile.pdf”

};
Return New FileStreamResult(stream, "application/pdf") With {.FileDownloadName = "downloadedfile.pdf"}
VB   C#

4.1. Export a PDF with ASP.NET

byte[] Binary = MyPDFDocument.BinaryData;

Response.Clear();

Response.ContentType = “application/octet-stream”;

Context.Response.OutputStream.Write(Binary, 0, Binary.Length);

Response.Flush();
Dim Binary() As Byte = MyPDFDocument.BinaryData

Response.Clear()

Response.ContentType = "application/octet-stream"

Context.Response.OutputStream.Write(Binary, 0, Binary.Length)

Response.Flush()
VB   C#