How to Create PDF Files in .NET Using IronPDF

PDF means "Portable Document Format". There are many scenarios when a developer needs to create simple PDF document in the application. In C# ASP.NET Core, this can be a very tedious task but thanks to IronPDF ".NET Core PDF Library", it has become so easy with just one line of source code. In this tutorial, we will learn how to create PDF file using .NET language.

Topics Covered In This Tutorial

We 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 URL
  • Summary

IronPDF

IronPDF .NET PDF Library allows developers to create PDF file 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 single line of code. It processes PDF documents without Acrobat Reader or any other PDF viewer. IronPDF can be used for creating PDF file 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

  • Easily creates PDF document from HTML 4 and 5, CSS, and JavaScript
  • Generate PDF documents from URL
  • Load URLs with custom-network login credentials, user-agents, proxies, cookies, HTTP headers, and form fields or variables allowing login behind HTML login forms for creating PDF file
  • Read and fill PDF (Portable Document Format) form-field data
  • Extract images and texts from PDF files
  • Digitally Sign PDF documents
  • No third party library required

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

Create a C# Project

This tutorial will use the Visual Studio 2022 version so I assume you must have installed it.

  • Open Visual Studio 2022.
  • Create a new C# .NET Core project. Select .NET Core console application.

    Console Application

    Console Application

  • Give a name to the project. E.g DemoApp.
  • The latest and most stable version of the .NET framework is 6.0. We are going to use this. Click on Create button.

    .NET Framework

    .NET Framework

Install the IronPDF Library

To create PDF document, we will first need to install IronPDF library. You can install it by any given method below.

1. NuGet Package Manager

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

    Package Manager - Solution Explorer

    Package Manager - Solution Explorer

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

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

PM> install-package IronPDF
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 DLL file from this link.

  • 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 the IronPDF's features.

Create a new PDF Document

IronPDF is ready and now we can create our first PDF document for our .NET Core applications. There are multiple ways through which we can create PDF document. Let's have a look at some of them below using code examples.

1. Create 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 PDF.

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

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

You must be familiar with HTML tags to generate PDF files or PDF pages using the IronPDF library. We use the SaveAs function to save the output PDF file. You can see the output PDF page below.

Output

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, we used the RenderHTMLFileAsPdf function to create PDF from HTML files and it is the most important function in the above code. You can give the full path of 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 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 link.

3. Create PDF using URLs

Sometimes we need to create PDF files of a URL programmatically in our application. You can use any URL to create a PDF document and return file in IronPDF library.

This is the sample code:

var Render = new ChromePdfRenderer();
var PDF = Render.RenderUrlAsPdf("https://ironpdf.com/");
PDF.SaveAs("IronPDF PDF Library.pdf");
var Render = new ChromePdfRenderer();
var PDF = Render.RenderUrlAsPdf("https://ironpdf.com/");
PDF.SaveAs("IronPDF PDF Library.pdf");
Dim Render = New ChromePdfRenderer()
Dim PDF = Render.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 function. This function creates 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

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 becomes 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 HTMLto PDF from a remote server. It actually spins up an instance of a real standard 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 30-day trial key to test out the full potential of IronPDF and generate PDF document 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 at this link.PDF means "Portable Document Format". There are many scenarios when a developer needs to create simple PDF document in the application. In C# ASP.NET Core, this can be a very tedious task but thanks to IronPDF ".NET Core PDF Library", it has become so easy with just one line of source code. In this tutorial, we will learn how to create PDF file using .NET language.