Skip to main content

Save and Export PDFs in C#

Save and Export PDFs in C#Photo by John Schnobrich

Originally Posted On: https://ironpdf.com/docs/questions/export-save-pdf-csharp/

 

IronPDF is a .NET library that allows you to use C# to save your HTML as a PDF. It also allows C# / VB developers to edit PDF documents programatically.

To Export our PDF documents we have 3 options – all based on the IronPDF.PdfDocument Object.

1. How to Save to Disk

Use IronPDF.PdfDocument.SaveAs() to save your PDF to disk.

You will find that this method supports adding password protection. You may also wish to explore the PdfDocument documentation to discover methods for digitally signing exported PDFS.

2. How to Save a PDF File to MemorySteam in C# (System.IO.MemoryStream)

The IronPDF.PdfDocument.Stream property saves the PDF to memory using a System.IO.MemoryStream

3. 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[].

4. 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
  1. /// send MyPdfDocument.Stream to this method
  2. return new FileStreamResult(stream, “application/pdf”)
  3. {
  4. FileDownloadName = “file.pdf”
  5. };

Copy code to clipboardVB  C#

ASP.NET PDF Export
  1. byte[] Binary = MyPdfDocument.BinaryData;
  2. Response.Clear();
  3. Response.ContentType = “application/octet-stream”;
  4. Context.Response.OutputStream.Write(Binary, 0, Binary.Length);
  5. Response.Flush();

Copy code to clipboard

Data & News supplied by www.cloudquote.io
Stock quotes supplied by Barchart
Quotes delayed at least 20 minutes.
By accessing this page, you agree to the following
Privacy Policy and Terms and Conditions.