C# Generate PDF (Code Example)

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.


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. 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.

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 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
anchor-generate-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";
            PDF.SaveAs(OutputPath);

        }
/**
PDF from HTML String
anchor-generate-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";
            PDF.SaveAs(OutputPath);

        }
'''
'''PDF from HTML String
'''anchor-generate-pdf-from-html-string
'''*
Private Sub HTMLString()
			' Render any HTML fragment or document to HTML
			Dim Renderer = New IronPdf.ChromePdfRenderer()
			Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPDF</h1>")

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

End Sub
VB   C#

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

Note the aptly named methods:

  • ChromePdfRenderer
  • 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 without using iTextSharp for PdfSharp.

/**
PDF from ASPX
anchor-generate-pdf-from-aspx
**/
protected void Page_Load(object sender, EventArgs e)
        {

            IronPdf.AspxToPdf.RenderThisPageAsPdf();
        }
/**
PDF from ASPX
anchor-generate-pdf-from-aspx
**/
protected void Page_Load(object sender, EventArgs e)
        {

            IronPdf.AspxToPdf.RenderThisPageAsPdf();
        }
'''
'''PDF from ASPX
'''anchor-generate-pdf-from-aspx
'''*
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 make IronPDF extremely handy and highly sought-after. With a PDF file format library that is able to comprehend both CSS and JavaScript, you won’t need any other .NET PDF package, only IronPDF.


7. Generate PDF with Custom Viewport

Here are a few examples of 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 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#.

/**
Set Viewport
anchor-generate-pdf-with-custom-viewport
**/
using IronPdf;
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.RenderHTMLFileAsPdf("Assets/Responsive.html");
/**
Set Viewport
anchor-generate-pdf-with-custom-viewport
**/
using IronPdf;
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.RenderHTMLFileAsPdf("Assets/Responsive.html");
'''
'''Set Viewport
'''anchor-generate-pdf-with-custom-viewport
'''*
Imports IronPdf
Private Renderer As 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.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 using C#. This is done by using the following few lines of code for the settings:

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