How to Export and Save PDFs in IronPDF C#
IronPDF exports a rendered PdfDocument to PDF in C# through SaveAs for disk, the Stream and BinaryData properties for memory, and SaveAsPdfA, SaveAsPdfUA, and SaveAsRevision for archival, accessible, and versioned output. Each method takes a document you already rendered and writes it to the destination your application needs, whether that is a file path, an in-memory buffer, or an HTTP response.
This guide walks through every export target, from a one-line file save to serving a PDF directly to a browser, and the methods that produce conformance-tagged output.
Quickstart: Export HTML to PDF in C#
Render HTML and write the result to disk in a single statement. The RenderHtmlAsPdf call returns a PdfDocument, and SaveAs persists it.
-
Install IronPDF with NuGet Package Manager
-
Copy and run this code snippet.
new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>HelloPDF</h1>").SaveAs("myExportedFile.pdf"); -
Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
Minimal Workflow (5 steps)
- Download the C# PDF library from NuGet
- Render or load a
PdfDocument - Save to disk with
SaveAs, or to memory withStreamandBinaryData - Serve the bytes to the web as a file response rather than HTML
- Export conformant output with
SaveAsPdfA,SaveAsPdfUA, orSaveAsRevision
What Are the Options for Saving PDFs?
IronPDF saves a PdfDocument to four kinds of destination: a file on disk, an in-memory MemoryStream, a raw byte[], and a conformance-tagged file for archiving, accessibility, or incremental revision. The sections below cover each target with a tested example, starting with the simplest file save and ending with the specialized export methods.
How to Save a PDF to Disk
Use the SaveAs method to write a PdfDocument to a file path. This is the direct route for desktop applications or any server process that keeps PDFs on the file system.
:path=/static-assets/pdf/content-code-examples/how-to/export-save-pdf-csharp-2.cs
// Complete example for saving PDF to disk
using IronPdf;
// Initialize the Chrome PDF renderer
var renderer = new ChromePdfRenderer();
// Create HTML content with styling
string htmlContent = @"
<html>
<head>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
h1 { color: #333; }
.content { line-height: 1.6; }
</style>
</head>
<body>
<h1>Invoice #12345</h1>
<div class='content'>
<p>Date: " + DateTime.Now.ToString("yyyy-MM-dd") + @"</p>
<p>Thank you for your business!</p>
</div>
</body>
</html>";
// Render HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Save to disk with standard method
pdf.SaveAs("invoice_12345.pdf");
// Save with password protection for sensitive documents
pdf.Password = "secure123";
pdf.SaveAs("protected_invoice_12345.pdf");
Imports IronPdf
' Initialize the Chrome PDF renderer
Dim renderer As New ChromePdfRenderer()
' Create HTML content with styling
Dim htmlContent As String = "
<html>
<head>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
h1 { color: #333; }
.content { line-height: 1.6; }
</style>
</head>
<body>
<h1>Invoice #12345</h1>
<div class='content'>
<p>Date: " & DateTime.Now.ToString("yyyy-MM-dd") & "</p>
<p>Thank you for your business!</p>
</div>
</body>
</html>"
' Render HTML to PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Save to disk with standard method
pdf.SaveAs("invoice_12345.pdf")
' Save with password protection for sensitive documents
pdf.Password = "secure123"
pdf.SaveAs("protected_invoice_12345.pdf")
The same example also sets the Password property before a second save, which encrypts the file so it cannot open without that password. For finer control over what a recipient can do with the file, see the guide on PDF permissions and passwords.
Output
How to Save a PDF to a MemoryStream
The Stream property returns the document as a System.IO.MemoryStream. Reach for it when you need to hand the PDF to another method, upload it, or email it without writing a temporary file first. Read more on working with PDF memory streams.
:path=/static-assets/pdf/content-code-examples/how-to/export-save-pdf-csharp-3.cs
// Example: Save PDF to MemoryStream
using IronPdf;
using System.IO;
var renderer = new ChromePdfRenderer();
// Render HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Monthly Report</h1><p>Sales figures...</p>");
// Get the PDF as a MemoryStream
MemoryStream stream = pdf.Stream;
// Example: Upload to cloud storage or database
// UploadToCloudStorage(stream);
// Example: Email as attachment without saving to disk
// EmailService.SendWithAttachment(stream, "report.pdf");
// Remember to dispose of the stream when done
stream.Dispose();
Imports IronPdf
Imports System.IO
' Example: Save PDF to MemoryStream
Dim renderer As New ChromePdfRenderer()
' Render HTML content
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Monthly Report</h1><p>Sales figures...</p>")
' Get the PDF as a MemoryStream
Dim stream As MemoryStream = pdf.Stream
' Example: Upload to cloud storage or database
' UploadToCloudStorage(stream)
' Example: Email as attachment without saving to disk
' EmailService.SendWithAttachment(stream, "report.pdf")
' Remember to dispose of the stream when done
stream.Dispose()
Output
How to Save to Binary Data
The BinaryData property returns the document as a byte[]. A byte array suits database columns, cache entries, and APIs that accept raw bytes rather than a stream.
:path=/static-assets/pdf/content-code-examples/how-to/export-save-pdf-csharp-4.cs
// Example: Convert PDF to binary data
using IronPdf;
var renderer = new ChromePdfRenderer();
// Configure rendering options for better quality
renderer.RenderingOptions = new ChromePdfRenderOptions()
{
MarginTop = 20,
MarginBottom = 20,
MarginLeft = 10,
MarginRight = 10,
PaperSize = IronPdf.Rendering.PdfPaperSize.A4
};
// Render content to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Contract Document</h1>");
// Get binary data
byte[] binaryData = pdf.BinaryData;
// Example: Store in database
// database.StorePdfDocument(documentId, binaryData);
// Example: Send via API
// apiClient.UploadDocument(binaryData);
Imports IronPdf
Dim renderer As New ChromePdfRenderer()
' Configure rendering options for better quality
renderer.RenderingOptions = New ChromePdfRenderOptions() With {
.MarginTop = 20,
.MarginBottom = 20,
.MarginLeft = 10,
.MarginRight = 10,
.PaperSize = IronPdf.Rendering.PdfPaperSize.A4
}
' Render content to PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Contract Document</h1>")
' Get binary data
Dim binaryData As Byte() = pdf.BinaryData
' Example: Store in database
' database.StorePdfDocument(documentId, binaryData)
' Example: Send via API
' apiClient.UploadDocument(binaryData)
When you need the reverse direction, loading bytes back into an editable document, the guide on converting PDFs to a MemoryStream covers it.
Output
How Do I Serve a PDF from a Web Server to the Browser?
To return a PDF over HTTP you send the bytes as a file response, not as HTML. Both Stream and BinaryData plug straight into the file-result types that ASP.NET provides, so the controller renders a document and returns it without ever touching disk.
How Do I Export a PDF in MVC?
In ASP.NET Core MVC, wrap the Stream in a FileStreamResult to prompt a download, or pass BinaryData to File to display the PDF inline. The two actions below show both. This pairs naturally with rendering CSHTML views to PDF.
// MVC controller methods for PDF export
public IActionResult DownloadInvoice(int invoiceId)
{
// Generate your HTML content
string htmlContent = GenerateInvoiceHtml(invoiceId);
// Render the PDF with IronPDF
var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Take the PDF stream and rewind it
MemoryStream stream = pdf.Stream;
stream.Position = 0;
// Returning a FileStreamResult prompts a download in the browser
return new FileStreamResult(stream, "application/pdf")
{
FileDownloadName = $"invoice_{invoiceId}.pdf"
};
}
public IActionResult ViewInvoice(int invoiceId)
{
var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(GenerateInvoiceHtml(invoiceId));
// Returning BinaryData with no filename displays the PDF inline
return File(pdf.BinaryData, "application/pdf");
}
// MVC controller methods for PDF export
public IActionResult DownloadInvoice(int invoiceId)
{
// Generate your HTML content
string htmlContent = GenerateInvoiceHtml(invoiceId);
// Render the PDF with IronPDF
var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Take the PDF stream and rewind it
MemoryStream stream = pdf.Stream;
stream.Position = 0;
// Returning a FileStreamResult prompts a download in the browser
return new FileStreamResult(stream, "application/pdf")
{
FileDownloadName = $"invoice_{invoiceId}.pdf"
};
}
public IActionResult ViewInvoice(int invoiceId)
{
var renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf(GenerateInvoiceHtml(invoiceId));
// Returning BinaryData with no filename displays the PDF inline
return File(pdf.BinaryData, "application/pdf");
}
Imports System.IO
Imports Microsoft.AspNetCore.Mvc
' MVC controller methods for PDF export
Public Class InvoiceController
Inherits Controller
Public Function DownloadInvoice(invoiceId As Integer) As IActionResult
' Generate your HTML content
Dim htmlContent As String = GenerateInvoiceHtml(invoiceId)
' Render the PDF with IronPDF
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Take the PDF stream and rewind it
Dim stream As MemoryStream = pdf.Stream
stream.Position = 0
' Returning a FileStreamResult prompts a download in the browser
Return New FileStreamResult(stream, "application/pdf") With {
.FileDownloadName = $"invoice_{invoiceId}.pdf"
}
End Function
Public Function ViewInvoice(invoiceId As Integer) As IActionResult
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(GenerateInvoiceHtml(invoiceId))
' Returning BinaryData with no filename displays the PDF inline
Return File(pdf.BinaryData, "application/pdf")
End Function
Private Function GenerateInvoiceHtml(invoiceId As Integer) As String
' Placeholder for the method that generates HTML content
Return String.Empty
End Function
End Class
How Do I Export a PDF in ASP.NET WebForms?
Traditional ASP.NET WebForms applications write the bytes through the Response object instead. Configure the rendering options once, pull BinaryData, and stream it to the client.
// ASP.NET WebForms PDF export
protected void ExportButton_Click(object sender, EventArgs e)
{
var renderer = new ChromePdfRenderer();
// Configure rendering options
renderer.RenderingOptions = new ChromePdfRenderOptions()
{
PaperSize = IronPdf.Rendering.PdfPaperSize.Letter,
PrintHtmlBackgrounds = true,
CreatePdfFormsFromHtml = true
};
// Render from custom HTML
PdfDocument MyPdfDocument = renderer.RenderHtmlAsPdf(GetReportHtml());
// Retrieve the PDF bytes
byte[] Binary = MyPdfDocument.BinaryData;
// Write the bytes to the response as a download
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment; filename=report_" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);
Response.Flush();
Response.End();
}
// ASP.NET WebForms PDF export
protected void ExportButton_Click(object sender, EventArgs e)
{
var renderer = new ChromePdfRenderer();
// Configure rendering options
renderer.RenderingOptions = new ChromePdfRenderOptions()
{
PaperSize = IronPdf.Rendering.PdfPaperSize.Letter,
PrintHtmlBackgrounds = true,
CreatePdfFormsFromHtml = true
};
// Render from custom HTML
PdfDocument MyPdfDocument = renderer.RenderHtmlAsPdf(GetReportHtml());
// Retrieve the PDF bytes
byte[] Binary = MyPdfDocument.BinaryData;
// Write the bytes to the response as a download
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment; filename=report_" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
Context.Response.OutputStream.Write(Binary, 0, Binary.Length);
Response.Flush();
Response.End();
}
' ASP.NET WebForms PDF export
Protected Sub ExportButton_Click(sender As Object, e As EventArgs)
Dim renderer As New ChromePdfRenderer()
' Configure rendering options
renderer.RenderingOptions = New ChromePdfRenderOptions() With {
.PaperSize = IronPdf.Rendering.PdfPaperSize.Letter,
.PrintHtmlBackgrounds = True,
.CreatePdfFormsFromHtml = True
}
' Render from custom HTML
Dim MyPdfDocument As PdfDocument = renderer.RenderHtmlAsPdf(GetReportHtml())
' Retrieve the PDF bytes
Dim Binary As Byte() = MyPdfDocument.BinaryData
' Write the bytes to the response as a download
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=report_" & DateTime.Now.ToString("yyyyMMdd") & ".pdf")
Context.Response.OutputStream.Write(Binary, 0, Binary.Length)
Response.Flush()
Response.End()
End Sub
The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.
How Do I Export PDF/A, PDF/UA, and Revisions?
Beyond the general save targets, IronPDF writes three conformance-specific formats. SaveAsPdfA produces an archival file, SaveAsPdfUA produces a tagged accessible file, and SaveAsRevision appends an incremental revision to an existing document.
How to Save a PDF/A Archive
SaveAsPdfA writes a self-contained file that meets the ISO PDF/A standard for long-term storage, embedding the fonts and color data a reader needs years from now. The PdfAVersions argument selects the conformance level, such as PdfA3b.
:path=/static-assets/pdf/content-code-examples/how-to/export-save-pdf-csharp-pdfa.cs
using IronPdf;
var renderer = new ChromePdfRenderer();
// Render the document you want to archive
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Archived Document</h1>");
// Save as a PDF/A-3b file for long-term archiving.
// PdfAVersions controls the conformance level (PdfA1b, PdfA2b, PdfA3b, PdfA4, and others).
pdf.SaveAsPdfA("archive-pdfa.pdf", IronPdf.PdfAVersions.PdfA3b);
Imports IronPdf
Dim renderer As New ChromePdfRenderer()
' Render the document you want to archive
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Archived Document</h1>")
' Save as a PDF/A-3b file for long-term archiving.
' PdfAVersions controls the conformance level (PdfA1b, PdfA2b, PdfA3b, PdfA4, and others).
pdf.SaveAsPdfA("archive-pdfa.pdf", IronPdf.PdfAVersions.PdfA3b)
Output
How to Save an Accessible PDF/UA File
SaveAsPdfUA writes a tagged PDF that meets the PDF/UA accessibility standard, which screen readers rely on to navigate the document. The third argument sets the document language so assistive technology reads it with the right voice.
:path=/static-assets/pdf/content-code-examples/how-to/export-save-pdf-csharp-pdfua.cs
using IronPdf;
var renderer = new ChromePdfRenderer();
// Render content that should be tagged for assistive technology
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Accessible Document</h1><p>Tagged for screen readers.</p>");
// Save as a PDF/UA-1 file. The last argument sets the document's primary
// language, which screen readers use to choose the correct voice.
pdf.SaveAsPdfUA("accessible-pdfua.pdf", IronPdf.PdfUAVersions.PdfUA1, IronPdf.NaturalLanguages.English_UnitedKingdom);
Imports IronPdf
Dim renderer As New ChromePdfRenderer()
' Render content that should be tagged for assistive technology
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Accessible Document</h1><p>Tagged for screen readers.</p>")
' Save as a PDF/UA-1 file. The last argument sets the document's primary
' language, which screen readers use to choose the correct voice.
pdf.SaveAsPdfUA("accessible-pdfua.pdf", IronPdf.PdfUAVersions.PdfUA1, IronPdf.NaturalLanguages.English_UnitedKingdom)
Output
How to Save an Incremental Revision
SaveAsRevision appends changes to a file instead of rewriting it, so earlier revisions, including any digital signatures, stay intact. The document must be opened with ChangeTrackingModes.EnableChangeTracking for the incremental save to work.
:path=/static-assets/pdf/content-code-examples/how-to/export-save-pdf-csharp-revision.cs
using IronPdf;
using IronPdf.Rendering;
var renderer = new ChromePdfRenderer();
// Create and save the original revision of the document
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Versioned Document</h1>");
pdf.SaveAs("revision-base.pdf");
// Re-open with change tracking enabled so the next save appends a revision
// instead of rewriting the file. This preserves earlier signed revisions.
PdfDocument loaded = PdfDocument.FromFile("revision-base.pdf", null, null, ChangeTrackingModes.EnableChangeTracking);
// Write an incremental revision on top of the existing bytes
loaded.SaveAsRevision("revision-v2.pdf");
Imports IronPdf
Imports IronPdf.Rendering
Dim renderer As New ChromePdfRenderer()
' Create and save the original revision of the document
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Versioned Document</h1>")
pdf.SaveAs("revision-base.pdf")
' Re-open with change tracking enabled so the next save appends a revision
' instead of rewriting the file. This preserves earlier signed revisions.
Dim loaded As PdfDocument = PdfDocument.FromFile("revision-base.pdf", Nothing, Nothing, ChangeTrackingModes.EnableChangeTracking)
' Write an incremental revision on top of the existing bytes
loaded.SaveAsRevision("revision-v2.pdf")
Output
How Do I Export a PDF Asynchronously?
Rendering blocks the calling thread until the Chromium engine finishes. In a web request or a desktop UI, call RenderHtmlAsPdfAsync instead and await it, then save the returned document with the same SaveAs method. This keeps the thread free while the render runs.
:path=/static-assets/pdf/content-code-examples/how-to/export-save-pdf-csharp-async.cs
using IronPdf;
using System.Threading.Tasks;
var renderer = new ChromePdfRenderer();
// Render off the calling thread so a web request or UI stays responsive
PdfDocument pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Async Generated PDF</h1>");
// SaveAs writes the finished document to disk once the render completes
pdf.SaveAs("async-render.pdf");
Imports IronPdf
Imports System.Threading.Tasks
Dim renderer As New ChromePdfRenderer()
' Render off the calling thread so a web request or UI stays responsive
Dim pdf As PdfDocument = Await renderer.RenderHtmlAsPdfAsync("<h1>Async Generated PDF</h1>")
' SaveAs writes the finished document to disk once the render completes
pdf.SaveAs("async-render.pdf")
Output
Conclusion
IronPDF exports a rendered PdfDocument to disk, memory, an HTTP response, or a conformance-tagged file, each through a single method on the document you already built. Pick the target that matches where the bytes need to go, and apply SaveAsPdfA, SaveAsPdfUA, or SaveAsRevision when the output has to meet an archival, accessibility, or versioning standard.
From here, keep the bytes off disk entirely with the PDF memory stream workflow, or lock down a saved file using PDF permissions and passwords.
Frequently Asked Questions
How do I export HTML content to PDF in C#?
You can export HTML to PDF in C# using IronPDF's ChromePdfRenderer class. Simply create a renderer instance, use the RenderHtmlAsPdf() method to convert your HTML content, and then save it using the SaveAs() method. IronPDF makes it easy to convert HTML strings, files, or URLs directly to PDF documents.
What are the different methods to save a PDF using C#?
IronPDF provides multiple methods to save PDFs: SaveAs() for saving to disk, Stream for serving PDFs in web applications without creating temporary files, and BinaryData for getting the PDF as a byte array. Each method in IronPDF serves different use cases, from simple file storage to dynamic web delivery.
Can I save a PDF to memory instead of disk?
Yes, IronPDF allows you to save PDFs to memory using System.IO.MemoryStream. This is useful for web applications where you want to serve PDFs directly to users without creating temporary files on the server. You can use the Stream property or convert the PDF to binary data.
How do I add password protection when saving a PDF?
IronPDF enables password protection by setting the Password property on the PdfDocument object before saving. Simply assign a password string to pdf.Password and then use SaveAs() to create a protected PDF file that requires the password to open.
Can I serve a PDF directly to web browsers without saving to disk?
Yes, IronPDF allows you to serve PDFs directly to web browsers as binary data. You can use the BinaryData property to get the PDF as a byte array and serve it through your web application's response stream, eliminating the need for temporary file storage.
What's the simplest way to convert and save HTML as PDF in one line?
IronPDF provides a one-line solution: new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("Your HTML").SaveAs("output.pdf"). This creates a renderer, converts HTML to PDF, and saves it to disk in a single statement.

