C# Export to PDF Code Example Tutorial
IronPDF is a C# PDF Library that allows you to use C# to save your HTML as a PDF. It also allows C# / VB developers to edit PDF documents programmatically.
Get started with IronPDF
Start using IronPDF in your project today with a free trial.
How to Export PDF in C#
- Download and install the C# PDF Export Library from NuGet
- Explore the PdfDocument documentation to discover methods for digitally signing exported PDFs
- Save PDF to memory using a System.IO.MemoryStream
- Serve a PDF to the web as binary data rather than HTML
- Export the PDF as file
Options for Saving PDFs
How to Save PDF to Disk
Use the PdfDocument.SaveAs
method to save your PDF to disk.
You will find that this method supports adding password protection. Check out the following article to learn more about digitally signing exported PDFs: 'Digitally Sign a PDF Document.'
How to Save a PDF File to MemoryStream in C# (System.IO.MemoryStream
)
The IronPdf.PdfDocument.Stream
property saves the PDF to memory using a System.IO.MemoryStream
.
How to Save to Binary Data
The IronPdf.PdfDocument.BinaryData
property exports the PDF document as binary data in memory.
This outputs the PDF as a ByteArray
, which is expressed in C# as byte []
.
How to Serve from a Web Server to Browser
To serve a PDF to the web, we need to send it as binary data rather than HTML.
MVC PDF Export
// Sends 'stream' to the client as a file download with the specified name.
return new FileStreamResult(stream, "application/pdf")
{
FileDownloadName = "file.pdf"
};
// Sends 'stream' to the client as a file download with the specified name.
return new FileStreamResult(stream, "application/pdf")
{
FileDownloadName = "file.pdf"
};
ASP.NET PDF Export
// Retrieves the PDF binary data
byte[] Binary = MyPdfDocument.BinaryData;
// Clears the existing response content
Response.Clear();
// Sets the response content type to 'application/octet-stream', suitable for PDF files
Response.ContentType = "application/octet-stream";
// Writes the binary data to the response output stream
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);
// Flushes the response to send the data to the client
Response.Flush();
// Retrieves the PDF binary data
byte[] Binary = MyPdfDocument.BinaryData;
// Clears the existing response content
Response.Clear();
// Sets the response content type to 'application/octet-stream', suitable for PDF files
Response.ContentType = "application/octet-stream";
// Writes the binary data to the response output stream
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);
// Flushes the response to send the data to the client
Response.Flush();
Frequently Asked Questions
What is IronPDF?
IronPDF is a C# PDF Library that allows developers to save HTML as a PDF and edit PDF documents programmatically using C# or VB.
How do I get started with IronPDF?
To get started with IronPDF, download the library from NuGet and explore its features through the documentation.
How can I export a PDF in C# using IronPDF?
To export a PDF in C#, download the C# PDF Export Library from NuGet, explore the PdfDocument methods, and use them to save PDFs to memory or disk.
How do I save a PDF to disk using IronPDF?
Use the PdfDocument.SaveAs method to save your PDF to disk. This method also supports adding password protection.
How can I save a PDF to a MemoryStream in C#?
You can save a PDF to memory using the IronPdf.PdfDocument.Stream property, which utilizes a System.IO.MemoryStream.
How do I export a PDF as binary data?
The IronPdf.PdfDocument.BinaryData property allows you to export the PDF document as binary data in memory, represented as a byte array.
How can I serve a PDF from a web server to a browser?
To serve a PDF to the web, send it as binary data rather than HTML using methods like FileStreamResult in MVC or writing binary data directly in ASP.NET.
What is the use of System.IO.MemoryStream in IronPDF?
System.IO.MemoryStream is used in IronPDF to save PDF files to memory, allowing for efficient handling of PDF data without immediate disk storage.
What does the PdfDocument.SaveAs method do?
The PdfDocument.SaveAs method saves a PDF document to disk, and it supports additional features like password protection.
How is a PDF sent as a file download in MVC?
In MVC, a PDF can be sent as a file download using FileStreamResult, which streams the PDF data to the client as a downloadable file.