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"
};
' Sends 'stream' to the client as a file download with the specified name.
Return New FileStreamResult(stream, "application/pdf") With {.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();
' Retrieves the PDF binary data
Dim Binary() As Byte = 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
How can I export a PDF in C#?
To export a PDF in C#, you can use the IronPDF library. First, download and install the C# PDF Export Library from NuGet. Then, explore the `PdfDocument` methods to save PDFs to disk or memory and serve them to a web browser.
What are the options for saving PDFs using IronPDF?
IronPDF allows you to save PDFs using several methods: save to disk with PdfDocument.SaveAs
, save to memory using System.IO.MemoryStream
, or export as binary data with PdfDocument.BinaryData
.
How do I save a PDF to disk in C#?
Use the PdfDocument.SaveAs
method in IronPDF to save your PDF to disk. This method also supports features like adding password protection to your document.
How can I save a PDF to a MemoryStream in C#?
In IronPDF, you can save a PDF to memory using the IronPdf.PdfDocument.Stream
property, which employs a System.IO.MemoryStream
for efficient handling of PDF data.
How do I export a PDF as binary data in C#?
The PdfDocument.BinaryData
property in IronPDF 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 using IronPDF?
To serve a PDF from a web server to a browser using IronPDF, you should send it as binary data. In MVC, you can use FileStreamResult
, and in ASP.NET, you would write the binary data directly to the response stream.
What is the role of System.IO.MemoryStream in handling PDFs with IronPDF?
System.IO.MemoryStream
is used in IronPDF to save PDF files to memory, enabling efficient management of PDF data without the need for immediate disk storage.
How do I send a PDF as a file download in MVC using IronPDF?
In an MVC application, you can send a PDF as a file download using the FileStreamResult
class. This streams the PDF data to the client for download with the specified file name.