USING IRONPDF

How to Create PDF Files in .NET Using IronPDF

Updated February 25, 2024
Share:

This tutorial demonstrates how to create a PDF file using the .NET language in just one line of source code with the IronPDF library.

Topics Covered In This Tutorial

The tutorial will cover the following topics:

  • PDF Library for PDF File Creation - IronPDF
  • Create a C# .NET Core Project
  • Install IronPDF
    1. NuGet Package Manager
    2. NuGet Package Manager Console
    3. Using DLL File
  • Add the IronPDF Namespace
  • Create a New PDF Document
    1. Create a PDF Document Using HTML String
    2. Create a PDF Document Using the HTML File Name
    3. Create a PDF Document Using a URL
  • Summary

IronPDF

IronPDF .NET PDF Library allows developers to create PDF files easily in C#, F#, and VB.NET for .NET Core and .NET Framework. IronPDF's rendering is "pixel perfect" to desktop versions of Google Chrome and easily creates PDF documents using a single line of code. It processes PDF documents without Acrobat Reader or any other PDF viewer. IronPDF can be used for creating PDF files using HTML string, HTML file, or URL. IronPDF is a free library for the first 30 days that is used to create PDFs.

Some Important Features of IronPDF .NET Library

Let's start with how to use IronPDF to create PDF documents.

Create a C# Project

This tutorial will use the latest version of Visual Studio.

  • Open Visual Studio.
  • Create a new C# .NET Core project. Select .NET Core Console Application.

    How to Create PDF Files in .NET Using IronPDF, Figure 1: Console Application Console Application

  • Give a name to the project. E.g., DemoApp.
  • Select the latest and most stable version of the .NET Framework is 6.0. Click on the Create button.

    How to Create PDF Files in .NET Using IronPDF, Figure 2: .NET Framework .NET Framework

Install the IronPDF Library

To create a PDF document, firstly, install the IronPDF library. You can install it by any given method below.

1. NuGet Package Manager

The simplest approach is to install the IronPDF C# .NET Core Library from the NuGet Package Manager.

  • Open the Package Manager by clicking on Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
  • Or right-click on the project in Solution Explorer and click Manage NuGet Packages.

    How to Create PDF Files in .NET Using IronPDF, Figure 3: Package Manager - Solution Explorer Package Manager - Solution Explorer

  • Search for IronPDF. Select IronPDF and click on the Install. The library will begin installing.

    How to Create PDF Files in .NET Using IronPDF, Figure 4: Install IronPDF Install IronPDF

2. NuGet Package Manager Console

Open the NuGet Package Manager Console by clicking on Tools > NuGet Package Manager > Package Manager Console. Type the following command in the Command-line.

Install-Package IronPdf

How to Create PDF Files in .NET Using IronPDF, Figure 5: Package Manager Console Package Manager Console

3. Using a DLL File

The third way to use IronPDF in your project is to add a DLL file from the IronPDF library. You can download the IronPDF DLL file directly from this website.

  • Download the DLL zip file. Extract it to a specific folder.
  • Open a project in Visual Studio. In the Solution Explorer, right-click on References and browse for the IronPDF DLL file.

Add the IronPDF Namespace

Once the installation is done, now add the IronPdf namespace to your program file.

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

Note: You must add this line of code to every file where you wish to use IronPDF's features.

Create a New PDF Document

IronPDF is ready and let's create PDF documents using this .NET Core application. There are multiple ways to create PDF documents. Let's have a look at some of them below using code examples.

1. Create a PDF File Using HTML String

The IronPDF library makes it very easy to process HTML strings and convert them into PDF format. Here is the code that easily creates PDFs.

// Render any HTML fragment or document to HTML
var renderer = new ChromePdfRenderer();
using var pdf = renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
pdf.SaveAs("pixel-perfect.pdf");
// Render any HTML fragment or document to HTML
var renderer = new ChromePdfRenderer();
using var pdf = renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
pdf.SaveAs("pixel-perfect.pdf");
' Render any HTML fragment or document to HTML
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>")
pdf.SaveAs("pixel-perfect.pdf")
VB   C#

This code will generate a PDF file containing the content passed in the RenderHtmlAsPdf method. This function performs the conversion of an HTML fragment to a PDF document.

You must be familiar with HTML tags to generate PDF files or PDF pages using the IronPDF library. The SaveAs method is used to save the output PDF file. You can see the output PDF page below.

Output

How to Create PDF Files in .NET Using IronPDF, Figure 6: HTML String to PDF HTML String to PDF

2. Create PDF Using HTML File Name

The IronPDF library provides a fantastic feature to create a PDF file from an HTML file. IronPDF directly converts everything in an HTML document, including images, CSS, forms, etc., to a PDF document. It does not require any other library or function for further processing. IronPDF directly converts any HTML file to a PDF file.

Let's look at the following code for creating a PDF file from an HTML file:

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("IronFile.html");
pdf.SaveAs("IronPDF.pdf");
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf("IronFile.html");
pdf.SaveAs("IronPDF.pdf");
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlFileAsPdf("IronFile.html")
pdf.SaveAs("IronPDF.pdf")
VB   C#

Here, the RenderHtmlFileAsPdf method is used to create PDF from HTML files, and it is the most important function in the above code. You can give the full path of the HTML file in the function parameter or just place the HTML file in the bin folder of the source code. Then you will only need to put the file name with the extension in the RenderHtmlFileAsPdf function's parameter. The output PDF file will be saved in the bin folder when you run this code. The SaveAs function will help you to save created PDF files.

You can check other .NET Core rendering options along with header footer options from this IronPDF rendering options tutorial.

3. Create PDF Using URLs

Sometimes, there is a need to create PDF files of a URL programmatically in the application. You can use any URL to create a PDF document and return a file in the IronPDF library.

This is the sample code:

var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
pdf.SaveAs("IronPDF PDF Library.pdf");
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
pdf.SaveAs("IronPDF PDF Library.pdf");
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/")
pdf.SaveAs("IronPDF PDF Library.pdf")
VB   C#

The above code will create a PDF file of the given URL parameter to the RenderUrlAsPdf method. This function creates a PDF document of the URL. The SaveAs function is used for saving output files in the bin folder. You can see the bin folder in your project folder.

Output

How to Create PDF Files in .NET Using IronPDF, Figure 7: URL to PDF Document URL to PDF Document

Summary

IronPDF is a complete solution for working with PDF documents. It provides the ability to convert from different formats to PDF. The manipulation and formatting of PDF files become very easy with the IronPDF library function. All that is required is just a few lines of code to create and format the PDF file programmatically. Its main highlight is the HTML converter, and it does not render HTML to PDF from a remote server. It spins up an instance of a real standards-compliant web browser behind the scenes (without any additional software needing to be installed). The HTML is rendered with complete accuracy --- and in a vector format suitable for the highest standards of commercial printing. The output is a clean and high-quality PDF. IronPDF is ideal for developers and companies who need to manipulate PDF files within their software. It is openly commercial, with licensing and pricing details all published on the website.

You can try this PDF library's free version to test out its functionality. There is a free trial key to test out the full potential of IronPDF and generate PDF documents in your applications. Further, the current special offer allows you to get five products by IronPDF for the price of just two. More information about licensing can be found on this IronPDF licensing page.

< PREVIOUS
How to Download PDF From URL in C# (Step-By-Step) Tutorial
NEXT >
Creating a PDF Generator in ASP.NET Core using IronPDF

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 11,622,374 View Licenses >