Generating PDF Files in C# (2026 Guide)
Generating PDFs from HTML and URLs is a critical capability for .NET developers in 2026, whether you're converting web pages to documents, archiving online content, or transforming HTML reports into portable formats. With the right .NET PDF library, you can capture web pages and HTML content as professional PDF documents with just a few lines of code, preserving the exact appearance and structure of the original source.
IronPDF is the simplest, most versatile .NET PDF generator for HTML-to-PDF conversion - with an incredibly easy learning curve that gets you generating PDFs from web content in minutes, not hours. This comprehensive guide will show you exactly how to generate PDF documents from HTML and URLs in C# using IronPDF - a powerful generator that produces pixel-perfect results and supports every modern .NET platform, including .NET 10 with C# 14 (both released November 2025). While this tutorial covers everything from simple URL conversions to complex multi-page HTML reports, don't get intimidated - start from the beginning and work forwards. You'll learn multiple approaches to PDF generation, from capturing single web pages to batch-converting HTML files, plus how to troubleshoot common issues and optimize performance for various HTML-to-PDF scenarios.
How to Generate PDF in C#

- Install C# Library for generating PDF files.
- Generate a PDF Document from an HTML string.
- Generate a PDF document from a URL.
- Generate PDF documents from HTML source files.
- Generate PDF documents from ASP.NET web pages.
- Add custom headers and footers to generated PDF documents.
- More...
What is IronPDF?
IronPDF is a leading .NET PDF library recognized by developers worldwide for HTML-to-PDF conversion and comprehensive PDF manipulation. Built specifically for C# developers, IronPDF eliminates the complexity traditionally associated with PDF generation.
IronPDF utilizes a .NET Chromium engine to convert HTML pages to PDF documents. With HTML to PDF, there's no need to use complex APIs to position or design PDFs. IronPDF supports standard web documents: HTML, ASPX, JS, CSS, and images.
It enables you to create PDF documents using HTML5, CSS3, JavaScript, and images. You can easily edit, stamp, and add headers and footers to PDFs. Furthermore, it makes it simple to read PDF content efficiently with IronPDF and extract images.
IronPDF provides a comprehensive solution to all PDF-related problems. When you use IronPDF, you get all conversions in a single library, along with PDF manipulation tasks, requiring no external dependencies. IronPDF is a complete package and a great tool for developers and C# programmers.
IronPDF helps us to generate PDF files from the following:
- HTML Strings and Templates
- ASPX WebForms
- MVC Views
- HTML documents
- Any URL
IronPDF Features
IronPDF helps you create PDF documents from HTML 4 and 5, CSS, and JavaScript, and generates PDFs from URLs. It also loads URLs with custom network login credentials, User-Agents, Proxies, Cookies, HTTP headers, and form variables allowing login behind HTML login forms. You can easily extract images and text from PDFs, stamp new HTML content onto existing PDF pages, and add logical or HTML headers and footers. It can load and extract existing PDF files, merge and split content in PDF documents. It has a custom 'base URL' to permit accessible resource files across the web and accepts HTML encoded in any major document encoding (defaults to UTF-8). You can save and load from files, binary data, or MemoryStreams, transform PDF documents into a PrintDocument object to print without Adobe (with minimal code), and export detailed log documents regarding API success and debug errors.
Compatibility
IronPDF supports .NET languages like C#, VB.NET, and F#. It is compatible with .NET 10, .NET 9, .NET 8, .NET 6, .NET 5, .NET Core 2x and 3x, .NET Standard 2, and .NET Framework 4x. It works on Console, Web, and Desktop applications, and is supported by Microsoft Windows, Linux, macOS, Docker, and Azure. IronPDF works with Microsoft Visual Studio 2022/2026, JetBrains ReSharper, and Rider. The library is fully compatible with C# 14 (released November 2025).
Steps to Generate PDF Documents
First, create a new Visual Studio Project. This tutorial uses the Console Application template, but you can use any template according to your requirements.
You may also use your existing project in which you want to add PDF generation capabilities.
Create a Visual Studio Project
Open Microsoft Visual Studio (2022 or 2026 recommended). Click on "Create New Project". Select the C# Console Application template. Click "Next".
Assign a name to your project. This tutorial uses "CSharpGeneratePDF", but you can choose any name. Click "Next", and set the target .NET Framework. We recommend .NET 10 (latest LTS) or .NET 9. Click "Create" and a new project will be created.
Install the NuGet Package in Visual Studio
Before we begin, install the IronPDF Library using one of the following methods:
Package Manager Console
Write the following command in the Package Manager console to download and install the package:
Install-Package IronPdf

Package Manager Console showing IronPDF installation command
The NuGet Package Manager Solution
You can also install IronPDF using the NuGet Package Manager by following these steps:
Click on Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

Accessing NuGet Package Manager from Visual Studio menu
This will open the NuGet Package Manager. Click "Browse" and search for IronPDF as shown below.

Searching for IronPDF in the NuGet Package Manager
Click on IronPDF and then click "Install". This will install IronPDF in your project. You are now ready to use its features.
Directly Download IronPDF ZIP
Alternatively, you can download the IronPDF ZIP package for integration.
Remember to add this statement to the top of any .cs class file using IronPDF:
using IronPdf;using IronPdf;Check out IronPDF on NuGet for installation and updates for more about version updates and installation.
There are other IronPDF NuGet Packages available for specific deployments to Linux, Mac, Azure, and AWS targets that are documented in the IronPDF advanced NuGet installation guide.
The IronPDF library has now been installed, so let's move to the next step.
Generate PDF Files from HTML String
IronPDF can generate PDF files from HTML text quite easily. The following example illustrates this capability. Use this option when you need to convert HTML markup to PDF.
Add the following namespace:
using IronPdf;using IronPdf;Using the following code example:
// Instantiate the PDF renderer
var Renderer = new ChromePdfRenderer();
// Render HTML as PDF and save the result
Renderer.RenderHtmlAsPdf("<h1>This is the Tutorial for C# Generate PDF</h1>")
.SaveAs("GeneratePDF.pdf");// Instantiate the PDF renderer
var Renderer = new ChromePdfRenderer();
// Render HTML as PDF and save the result
Renderer.RenderHtmlAsPdf("<h1>This is the Tutorial for C# Generate PDF</h1>")
.SaveAs("GeneratePDF.pdf");In the above code, create a new ChromePdfRenderer, then call RenderHtmlAsPdf and SaveAs for the result.
Output
Run the program. It will generate a PDF document in the bin folder of the project (if no path is specified).

PDF generated from HTML string showing formatted heading
Generate Your First PDF Files
After installing IronPDF, you can effortlessly create PDF files using just a few lines of code. Copy the code below, paste it into Visual Studio, and run the program.
// Create a new PDF renderer
var pdf = new ChromePdfRenderer();
// Render HTML to PDF
PdfDocument doc = pdf.RenderHtmlAsPdf("<h1>This is a heading</h1>");
// Save the document
doc.SaveAs("FirstPDFDocument.pdf");// Create a new PDF renderer
var pdf = new ChromePdfRenderer();
// Render HTML to PDF
PdfDocument doc = pdf.RenderHtmlAsPdf("<h1>This is a heading</h1>");
// Save the document
doc.SaveAs("FirstPDFDocument.pdf");After execution, there will be a file named "FirstPDFDocument.pdf" in the bin folder of your project. Double-click the file to open it in your browser.

First PDF document displaying the rendered heading
Creating PDF files in C# or converting HTML to PDF is just a few lines of code using IronPDF.
Generate PDF Document from URL
Creating a PDF file in C# using a URL is just as easy as the previous example with three lines of code. The following code demonstrates how to create PDF from a URL with IronPDF.
using IronPdf;
// Instantiate renderer
var Renderer = new ChromePdfRenderer();
// Render URL as PDF
var PDF = Renderer.RenderUrlAsPdf("https://www.wikipedia.org");
// Save to file
PDF.SaveAs("wikipedia.pdf");using IronPdf;
// Instantiate renderer
var Renderer = new ChromePdfRenderer();
// Render URL as PDF
var PDF = Renderer.RenderUrlAsPdf("https://www.wikipedia.org");
// Save to file
PDF.SaveAs("wikipedia.pdf");Output

Wikipedia homepage captured as a PDF document with full styling preserved
Generate PDF from HTML Files
IronPDF can also generate PDFs from existing HTML files on disk. This is useful when you have pre-designed HTML templates or static files that need conversion.
using IronPdf;
// Instantiate renderer
var Renderer = new ChromePdfRenderer();
// Load and render HTML file
var PDF = Renderer.RenderHtmlFileAsPdf("HtmlPage.html");
// Save the PDF
PDF.SaveAs("FromHtmlFile.pdf");using IronPdf;
// Instantiate renderer
var Renderer = new ChromePdfRenderer();
// Load and render HTML file
var PDF = Renderer.RenderHtmlFileAsPdf("HtmlPage.html");
// Save the PDF
PDF.SaveAs("FromHtmlFile.pdf");Output

HTML file converted to PDF with all styling and layout preserved
Generate PDF Document from ASP.NET Web Forms
IronPDF provides seamless integration with ASP.NET Web Forms, allowing you to render dynamic web pages as PDF documents.
using IronPdf.AspNetCore;
// Render current page as PDF
var currentPagePdf = AspNetCore.RenderThisPageAsPdf();
// Render specific page as PDF
var specificPagePdf = AspNetCore.RenderPageAsPdf("/Page.aspx");
// Render page with parameters
var pageWithParamsPdf = AspNetCore.RenderPageAsPdf("/Page.aspx?id=100");
// Save the result
currentPagePdf.SaveAs("MyASPXPage.pdf");using IronPdf.AspNetCore;
// Render current page as PDF
var currentPagePdf = AspNetCore.RenderThisPageAsPdf();
// Render specific page as PDF
var specificPagePdf = AspNetCore.RenderPageAsPdf("/Page.aspx");
// Render page with parameters
var pageWithParamsPdf = AspNetCore.RenderPageAsPdf("/Page.aspx?id=100");
// Save the result
currentPagePdf.SaveAs("MyASPXPage.pdf");Output

ASP.NET WebForm rendered as PDF with dynamic content
Generate PDF from ASP.NET MVC View
IronPDF integrates perfectly with ASP.NET MVC applications, allowing you to render MVC Views directly as PDF documents.
using IronPdf.AspNetCore;
// Render MVC View as PDF
var viewPdf = AspNetCore.RenderViewAsPdf("/Home/Index");
// Render with model data
var modelData = new YourModelClass { Name = "John Doe", OrderId = 12345 };
var viewWithModelPdf = AspNetCore.RenderViewAsPdf("/Views/Invoice/Index", modelData);
// Save the PDF
viewPdf.SaveAs("MyMVCView.pdf");using IronPdf.AspNetCore;
// Render MVC View as PDF
var viewPdf = AspNetCore.RenderViewAsPdf("/Home/Index");
// Render with model data
var modelData = new YourModelClass { Name = "John Doe", OrderId = 12345 };
var viewWithModelPdf = AspNetCore.RenderViewAsPdf("/Views/Invoice/Index", modelData);
// Save the PDF
viewPdf.SaveAs("MyMVCView.pdf");Output

MVC View rendered as PDF with model data and styling
Add Headers and Footers to PDF Documents
Professional PDF documents often require headers and footers for branding, page numbers, dates, and metadata. IronPDF provides two methods: text-based and HTML-based headers and footers.
Add Simple Text Headers and Footers
var ChromePdfRenderer = new ChromePdfRenderer();
// Configure page numbering
ChromePdfRenderer.RenderingOptions.FirstPageNumber = 1;
// Header options
ChromePdfRenderer.RenderingOptions.TextHeader.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextHeader.CenterText = "This is Header {date} {time}";
ChromePdfRenderer.RenderingOptions.TextHeader.FontFamily = "Helvetica,Arial";
ChromePdfRenderer.RenderingOptions.TextHeader.FontSize = 12;
// Footer options
ChromePdfRenderer.RenderingOptions.TextFooter.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextFooter.FontFamily = "Arial";
ChromePdfRenderer.RenderingOptions.TextFooter.FontSize = 10;
ChromePdfRenderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
ChromePdfRenderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
// Generate PDF with headers and footers
ChromePdfRenderer.RenderHtmlAsPdf("<h1>This is my Sample Page for Generate PDF Tutorial</h1>")
.SaveAs("GeneratePDF.pdf");var ChromePdfRenderer = new ChromePdfRenderer();
// Configure page numbering
ChromePdfRenderer.RenderingOptions.FirstPageNumber = 1;
// Header options
ChromePdfRenderer.RenderingOptions.TextHeader.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextHeader.CenterText = "This is Header {date} {time}";
ChromePdfRenderer.RenderingOptions.TextHeader.FontFamily = "Helvetica,Arial";
ChromePdfRenderer.RenderingOptions.TextHeader.FontSize = 12;
// Footer options
ChromePdfRenderer.RenderingOptions.TextFooter.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextFooter.FontFamily = "Arial";
ChromePdfRenderer.RenderingOptions.TextFooter.FontSize = 10;
ChromePdfRenderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
ChromePdfRenderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
// Generate PDF with headers and footers
ChromePdfRenderer.RenderHtmlAsPdf("<h1>This is my Sample Page for Generate PDF Tutorial</h1>")
.SaveAs("GeneratePDF.pdf");PDF File Output with Header and Footer

PDF document with text-based headers and footers showing date, time, and page numbers
Add Headers and Footers with HTML
For more complex designs, use HTML-based headers and footers with full styling control.
var ChromePdfRenderer = new ChromePdfRenderer();
// Configure HTML footer
ChromePdfRenderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
Height = 15,
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>",
DrawDividerLine = true
};
// Configure HTML header
ChromePdfRenderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
Height = 20,
HtmlFragment = "<div style='text-align: center; font-weight: bold;'>This is Header {date}</div>",
};
// Generate PDF
ChromePdfRenderer.RenderHtmlAsPdf("<h1>This is sample page for Tutorial.</h1>")
.SaveAs("GeneratePDF.pdf");var ChromePdfRenderer = new ChromePdfRenderer();
// Configure HTML footer
ChromePdfRenderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
Height = 15,
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>",
DrawDividerLine = true
};
// Configure HTML header
ChromePdfRenderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
Height = 20,
HtmlFragment = "<div style='text-align: center; font-weight: bold;'>This is Header {date}</div>",
};
// Generate PDF
ChromePdfRenderer.RenderHtmlAsPdf("<h1>This is sample page for Tutorial.</h1>")
.SaveAs("GeneratePDF.pdf");
PDF with HTML-based headers and footers featuring custom styling
Encrypt PDF Documents with a Password
Security is paramount for sensitive documents. IronPDF makes it simple to encrypt PDF documents with password protection.
var ChromePdfRenderer = new ChromePdfRenderer();
// Generate PDF document
var pdfDocument = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello world</h1>");
// Set password to encrypt the document
pdfDocument.Password = "Password@1234";
// Save the secured PDF
pdfDocument.SaveAs("secured.pdf");var ChromePdfRenderer = new ChromePdfRenderer();
// Generate PDF document
var pdfDocument = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello world</h1>");
// Set password to encrypt the document
pdfDocument.Password = "Password@1234";
// Save the secured PDF
pdfDocument.SaveAs("secured.pdf");PDF File Output

Password prompt when opening encrypted PDF document
Merge and Split PDF Documents
IronPDF provides powerful capabilities for merging multiple PDF documents and splitting pages from existing documents.
var ChromePdfRenderer = new ChromePdfRenderer();
// Load existing PDFs
var pdfDocuments = new List<PdfDocument>
{
PdfDocument.FromFile("AccountPdf.pdf"),
PdfDocument.FromFile("GeneratePDF.pdf"),
PdfDocument.FromFile("wikipedia.pdf")
};
// Merge PDFs into a single document
var mergedPdfDocument = PdfDocument.Merge(pdfDocuments);
mergedPdfDocument.SaveAs("merged.pdf");
// Add a cover page
mergedPdfDocument.PrependPdf(ChromePdfRenderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>"));
// Remove the last page
mergedPdfDocument.RemovePage(mergedPdfDocument.PageCount - 1);
mergedPdfDocument.SaveAs("merged.pdf");
// Extract specific pages
mergedPdfDocument.CopyPages(1, 2).SaveAs("excerpt.pdf");var ChromePdfRenderer = new ChromePdfRenderer();
// Load existing PDFs
var pdfDocuments = new List<PdfDocument>
{
PdfDocument.FromFile("AccountPdf.pdf"),
PdfDocument.FromFile("GeneratePDF.pdf"),
PdfDocument.FromFile("wikipedia.pdf")
};
// Merge PDFs into a single document
var mergedPdfDocument = PdfDocument.Merge(pdfDocuments);
mergedPdfDocument.SaveAs("merged.pdf");
// Add a cover page
mergedPdfDocument.PrependPdf(ChromePdfRenderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>"));
// Remove the last page
mergedPdfDocument.RemovePage(mergedPdfDocument.PageCount - 1);
mergedPdfDocument.SaveAs("merged.pdf");
// Extract specific pages
mergedPdfDocument.CopyPages(1, 2).SaveAs("excerpt.pdf");Include PDFs in your project as "Content" to access them by filename.

Merged PDF document combining multiple source files
Extract Images from PDF Documents
This feature requires the System.Drawing.Common NuGet package. Use ExtractAllText to get text and ExtractAllImages to get images.
var ChromePdfRenderer = new ChromePdfRenderer();
// Load existing PDF
var pdfDocument = PdfDocument.FromFile("wikipedia.pdf");
// Get all text
var allText = pdfDocument.ExtractAllText();
// Get all Images
var allImages = pdfDocument.ExtractAllImages();
// Extract images and text by page
for (var index = 0; index < pdfDocument.PageCount; index++)
{
var pageNumber = index + 1;
var pageText = pdfDocument.ExtractTextFromPage(index);
var pageImages = pdfDocument.ExtractImagesFromPage(index);
}var ChromePdfRenderer = new ChromePdfRenderer();
// Load existing PDF
var pdfDocument = PdfDocument.FromFile("wikipedia.pdf");
// Get all text
var allText = pdfDocument.ExtractAllText();
// Get all Images
var allImages = pdfDocument.ExtractAllImages();
// Extract images and text by page
for (var index = 0; index < pdfDocument.PageCount; index++)
{
var pageNumber = index + 1;
var pageText = pdfDocument.ExtractTextFromPage(index);
var pageImages = pdfDocument.ExtractImagesFromPage(index);
}You need to add System.Drawing to save images as bitmaps.
Enable JavaScript
Modern web pages rely on JavaScript for dynamic content. IronPDF's Chromium engine fully supports JavaScript execution.
var ChromePdfRenderer = new ChromePdfRenderer();
// Enable JavaScript rendering
ChromePdfRenderer.RenderingOptions = new ChromePdfRenderOptions()
{
EnableJavaScript = true,
RenderDelay = 100 // Wait for JavaScript to execute
};var ChromePdfRenderer = new ChromePdfRenderer();
// Enable JavaScript rendering
ChromePdfRenderer.RenderingOptions = new ChromePdfRenderOptions()
{
EnableJavaScript = true,
RenderDelay = 100 // Wait for JavaScript to execute
};Increase RenderDelay for pages with heavy JavaScript processing or AJAX calls (500-1000ms for complex SPAs).
PDF OCR and Text Extraction
In many cases, you can extract embedded text from PDF documents directly:
// Load PDF
PdfDocument PDF = PdfDocument.FromFile("GeneratePDF.pdf");
// Extract all text
string Text = PDF.ExtractAllText();// Load PDF
PdfDocument PDF = PdfDocument.FromFile("GeneratePDF.pdf");
// Extract all text
string Text = PDF.ExtractAllText();If direct extraction doesn't work, your text is likely embedded in an image. Use the IronOCR library to scan documents for visual text:
using IronOcr;
var ocr = new IronTesseract();
var result = ocr.Read("scanned-document.pdf");
string recognizedText = result.Text;using IronOcr;
var ocr = new IronTesseract();
var result = ocr.Read("scanned-document.pdf");
string recognizedText = result.Text;Summary
You can generate PDF documents, manipulate and format them with just a few lines of code. IronPDF handles the complexity for you. There is a free version for development and a free trial before purchase. While other PDF libraries exist, many are either slow in performance or require complex low-level coding.
The IronPDF .NET library is now available with Iron Suite for .NET. Iron Suite contains five powerful libraries: IronPDF, IronXL, IronOCR, IronBarcode, and IronQR. These libraries work seamlessly together for comprehensive document processing. If you purchase the complete Iron Suite, you get all five products for the price of just two.
IronPDF is fully compatible with .NET 10 and C# 14 (released November 2025), along with .NET 9, .NET 8, .NET 6, and all modern .NET platforms.
You can download a project file from this IronPDF C# PDF Generation Tutorial Project.
Frequently Asked Questions
How can I generate a PDF from an HTML string in C#?
You can generate a PDF from an HTML string using the RenderHtmlAsPdf method in IronPDF's ChromePdfRenderer. This method converts HTML content into a PDF format, which can then be saved using the SaveAs method.
Can I create PDF documents from web page URLs?
Yes, you can create PDFs from web page URLs with IronPDF. Utilize the RenderUrlAsPdf method of ChromePdfRenderer to convert web page content into a PDF document.
How do I add headers and footers to a PDF in C#?
To add headers and footers to a PDF in C#, use IronPDF's ChromePdfRenderer by setting the TextHeader and TextFooter properties in the RenderingOptions. You can include dynamic elements like page numbers and dates.
Is it possible to encrypt PDF documents in C#?
Yes, you can encrypt PDF documents in C# using IronPDF by setting the Password property of the PdfDocument object to the desired password.
How can I merge or split PDF files using C#?
With IronPDF, you can merge multiple PDFs into a single document using the Merge method. To split documents, you can use the CopyPages method to extract specific pages.
What are the system requirements for using IronPDF?
IronPDF supports .NET languages such as C#, VB.NET, and F#. It is compatible with .NET 6, 5, Core 2.x, and 3.x, Standard 2, and Framework 4.x. It operates on Windows, Linux, macOS, Docker, and Azure OS.
Can I extract images from PDF files in C#?
Yes, you can extract images from PDF files using IronPDF's ExtractAllImages method. Ensure that the System.Drawing.Common NuGet package is included for handling images.
How do I install IronPDF in my C# project?
Install IronPDF in your project via the NuGet Package Manager in Visual Studio by searching for IronPDF or using the Package Manager Console with the command Install-Package IronPdf.
What additional features does IronPDF offer?
IronPDF provides features like creating PDFs from multiple sources, adding headers and footers, encrypting documents, extracting text and images, and converting HTML content. It also supports JavaScript execution and OCR capabilities for text extraction from image-embedded PDFs.
Is IronPDF compatible with .NET 10 and what benefits does that bring?
Yes, IronPDF is fully compatible with .NET 10. It works out of the box in .NET 10 projects just as it does in earlier .NET versions, across Windows, Linux, containers, and macOS. Benefits include improved runtime performance, zero configuration workarounds, and access to new C# language features along with async-friendly APIs.









