Generate PDF C#

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 allow us to focus more on the work, and less on the time-heavy details of trying to generate PDFs, 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

PDF from HTML String
private void HTMLString()
        {
            // Render any HTML fragment or document to HTML
            var Renderer = new IronPdf.HtmlToPdf();
            var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPDF</h1>");

            var OutputPath = "HtmlToPDF.pdf";
            PDF.SaveAs(OutputPath);

        }
Private Sub HTMLString()
			' Render any HTML fragment or document to HTML
			Dim Renderer = New IronPdf.HtmlToPdf()
			Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPDF</h1>")

			Dim OutputPath = "HtmlToPDF.pdf"
			PDF.SaveAs(OutputPath)

End Sub
Jump to Article
PDF from ASPX
protected void Page_Load(object sender, EventArgs e)
        {

            IronPdf.AspxToPdf.RenderThisPageAsPdf();
        }
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

			IronPdf.AspxToPdf.RenderThisPageAsPdf()
End Sub
Jump to Article
Set Viewport
using IronPdf;
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();

 //Choose screen or print CSS media
Renderer.PrintOptions.CssMediaType = PdfPrintOptions.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.RenderHTMLFileAsPdf("Assets/Responsive.html");
Imports IronPdf
Private Renderer As New IronPdf.HtmlToPdf()

 'Choose screen or print CSS media
Renderer.PrintOptions.CssMediaType = PdfPrintOptions.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.RenderHTMLFileAsPdf("Assets/Responsive.html")
Jump to Article
Try IronPDF free for development


Step 1

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.

Access the software by direct file download or through the latest NuGet Package install to load into Visual Studio and your project.


 PM > Install-Package IronPdf


How to Tutorial

2. Work with .NET Framework & .NET Core

The IronPDF library simplifies PDF 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.


3. Follow Easy Naming Conventions

What makes the IronPDF PDF library so special when generating PDFs in .NET?

First of all: speed. Not only does IronPDF generate PDF files for .NET quicker, it increases developer productivity because of the easy naming convention employed for methods in the IronPDF .NET PDF library.

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 library in either VB.NET or C#.


4. Generate PDF from HTML String

How easy is it to generate a PDF with IronPDF and .NET?

Pretty easy. Here is a very quick example on how to generate a PDFf from an HTML input string.

private void HTMLString()
        {
            // Render any HTML fragment or document to HTML
            var Renderer = new IronPdf.HtmlToPdf();
            var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPDF</h1>");

            var OutputPath = "HtmlToPDF.pdf";
            PDF.SaveAs(OutputPath);

        }
private void HTMLString()
        {
            // Render any HTML fragment or document to HTML
            var Renderer = new IronPdf.HtmlToPdf();
            var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPDF</h1>");

            var OutputPath = "HtmlToPDF.pdf";
            PDF.SaveAs(OutputPath);

        }
Private Sub HTMLString()
			' Render any HTML fragment or document to HTML
			Dim Renderer = New IronPdf.HtmlToPdf()
			Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPDF</h1>")

			Dim OutputPath = "HtmlToPDF.pdf"
			PDF.SaveAs(OutputPath)

End Sub
VB   C#

The above C# code uses 4 lines of code to generate a PDF file from an HTML string.

Note the aptly named methods:

  • HtmlToPdf
  • RenderHtmlAsPdf
  • SaveAs

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.

protected void Page_Load(object sender, EventArgs e)
        {

            IronPdf.AspxToPdf.RenderThisPageAsPdf();
        }
protected void Page_Load(object sender, EventArgs e)
        {

            IronPdf.AspxToPdf.RenderThisPageAsPdf();
        }
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

			IronPdf.AspxToPdf.RenderThisPageAsPdf()
End Sub
VB   C#


6. JavaScript Support

Features such as Responsiveness as well as the support of JavaScript makes IronPDF extremely handy and highly sought-after. With a PDF library that is able to comprehend both CSS and JavaScript, you won’t need another .NET PDF library.


7. Generate PDF with Custom Viewport

Here are a few examples on Responsiveness and utilizing JavaScript inside a converted PDF document.

The following code allows you to programmatically set the size of your response Viewport when generating PDF documents.

using IronPdf;
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();

 //Choose screen or print CSS media
Renderer.PrintOptions.CssMediaType = PdfPrintOptions.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.RenderHTMLFileAsPdf("Assets/Responsive.html");
using IronPdf;
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();

 //Choose screen or print CSS media
Renderer.PrintOptions.CssMediaType = PdfPrintOptions.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.RenderHTMLFileAsPdf("Assets/Responsive.html");
Imports IronPdf
Private Renderer As New IronPdf.HtmlToPdf()

 'Choose screen or print CSS media
Renderer.PrintOptions.CssMediaType = PdfPrintOptions.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.RenderHTMLFileAsPdf("Assets/Responsive.html")
VB   C#

IronPDF supports JavaScript quite nicely via the Chromium rendering engine. One caveat though, you may have to add a delay to a page render to allow JavaScript time to execute whilst generating PDFs. This is done by using the following few lines of code:

Renderer.PrintOptions.EnableJavaScript = true;
Renderer.PrintOptions.RenderDelay = 500; //milliseconds
Renderer.PrintOptions.EnableJavaScript = true;
Renderer.PrintOptions.RenderDelay = 500; //milliseconds
Renderer.PrintOptions.EnableJavaScript = True
Renderer.PrintOptions.RenderDelay = 500 'milliseconds
VB   C#