C# Generate PDF (Code Example)
Photo from Unsplash
Originally posted on: https://ironpdf.com/docs/questions/generate-pdf-in-csharp/
We are always looking for a way to generate PDF documents and work with them in C# projects more quickly, accurately, and efficiently.
Having easy-to-use functions from a library allows us to focus more on the work, and less on the time-heavy details of trying to generate PDF files, whether in .NET Core or .NET Framework.
Here are a few examples of how to generate PDF C# project tasks that you can use in your work, including generate PDF from HTML String, generate PDF from ASPX Input, and more.
How to Easily Generate a PDF File in C#
- Download the Nuget Package to Generate PDF Documents “IronPDF”
- Follow Easy Naming Conventions to Create a PDF
- Generate a PDF file from HTML string
- Create a PDF file from ASPX files in C#
1. Install the Free C# Library
To make the most out of this tutorial, install the IronPDF C# HTML to PDF library. You can use it free for development in your project and implement the steps below yourself. With it, you can create PDF files using C# and will not need other Nuget packages. It can even convert HTML to PDF with image, form, data, pages, CSS, text, header, footer, images supported in a single solution.
PM > Install-Package IronPdf
How to Tutorial
2. Work with .NET Framework & .NET Core
The IronPDF library simplifies PDF file tasks into easy-to-use and understandable .NET methods. This enhances developer productivity and speeds up development in .NET Core or .NET. IronPDF is compatible with any .NET Framework project from Version 4 upward, and .NET Core from version 2 upwards. Use it when you need to create PDF file content using C# .NET
3. Follow Easy Naming Conventions to Create a PDF
What makes the IronPDF ‘PDF’ library so special when generating PDF Documents in .NET?
First of all: speed. Not only does IronPDF create a PDF document for .NET quicker, but it also increases developer productivity because of the easy naming convention employed for methods in the IronPDF .NET PDF library. Using Html to PDF technology, you can create PDF documents using C# predictably and easily inside web applications and all other .NET project types.
Secondly, ease of use. As mentioned in the previous point, the IronPDF .NET PDF library is very easy to work with. It is easy to identify method names and understand their purpose. .NET developers will not struggle to get generate PDF in .NET with this PDF document library in either VB.NET or using C#.
4. Generate PDF from HTML String
How easy is it to generate a PDF with IronPDF and .NET?
Creating a PDF is pretty easy. Here is a very quick example of how to generate a PDF from an HTML input string.
PDF from HTML String
private void HTMLString()
// Render any HTML fragment or document to HTML
var Renderer = new IronPdf.ChromePdfRenderer();
var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPDF</h1>");
var OutputPath = "ChromePdfRenderer.pdf";
The above C# code uses 4 lines of code to create PDF file documents from an HTML string.
Note the aptly named methods:
Any newbie and expert alike will be able to identify, access, and use them in their VB.NET or C# code to generate PDFs.
5. Generate PDF from ASPX
Another example of ease of use:
The following code makes use of IronPDF to generate a PDF directly from an ASPX file without using iTextSharp for PdfSharp.
PDF from ASPX
protected void Page_Load(object sender, EventArgs e)
7. Generate PDF with Custom Viewport
The following code allows you to programmatically set the size of your response Viewport when generating your PDF document.
How to: Create PDF file using “Html to PDF” from an HTML String. You can convert HTML into an HTML file using C#. You can then save it to disk or server as web content using the IronPDF API using C#.
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
//Choose screen or print CSS media
Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Screen;
//Set the width of the responsive virtual browser window in pixels
Renderer.ViewPortWidth = 1280;
// Render an HTML document or snippet as a string
Renderer.RenderingOptions.RenderDelay = 500; //milliseconds