How to Create PDF Files in .NET Using IronPDF
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
- NuGet Package Manager
- NuGet Package Manager Console
- Using DLL File
- Add the IronPDF Namespace
- Create a New PDF Document
- Create a PDF Document Using HTML String
- Create a PDF Document Using the HTML File Name
- 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 an 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 documents from HTML 4 and 5, CSS, and JavaScript
- Generate PDF documents from URLs
- 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 a PDF file
- Read and fill out PDF form field data
- Extract images and text from PDF files
- Digitally sign PDF documents
- No third-party library required
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.
Console Application
- Give a name to the project. E.g., DemoApp.
Select the latest and most stable version of the .NET Framework, which is 6.0. Click on the Create button.
.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.
Package Manager - Solution Explorer
Search for IronPDF. Select IronPDF and click on Install. The library will begin installing.
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
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, add the IronPdf
namespace to your program file.
using IronPdf;
using IronPdf;
Imports IronPdf
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.
// Create a new instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Use the renderer to convert HTML to a PDF
using var pdf = renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
// Save the rendered PDF to a file
pdf.SaveAs("pixel-perfect.pdf");
// Create a new instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Use the renderer to convert HTML to a PDF
using var pdf = renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
// Save the rendered PDF to a file
pdf.SaveAs("pixel-perfect.pdf");
' Create a new instance of ChromePdfRenderer
Dim renderer = New ChromePdfRenderer()
' Use the renderer to convert HTML to a PDF
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>")
' Save the rendered PDF to a file
pdf.SaveAs("pixel-perfect.pdf")
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
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:
// Create a new instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Render an HTML file to a PDF document
var pdf = renderer.RenderHtmlFileAsPdf("IronFile.html");
// Save the rendered PDF to a file
pdf.SaveAs("IronPDF.pdf");
// Create a new instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Render an HTML file to a PDF document
var pdf = renderer.RenderHtmlFileAsPdf("IronFile.html");
// Save the rendered PDF to a file
pdf.SaveAs("IronPDF.pdf");
' Create a new instance of ChromePdfRenderer
Dim renderer = New ChromePdfRenderer()
' Render an HTML file to a PDF document
Dim pdf = renderer.RenderHtmlFileAsPdf("IronFile.html")
' Save the rendered PDF to a file
pdf.SaveAs("IronPDF.pdf")
Here, the RenderHtmlFileAsPdf method
is used to create a PDF from HTML files. You can provide the full path of the HTML file in the function parameter, or place the HTML file in the bin
folder of the source code and just provide the file name with the extension. The output PDF file will be saved in the bin
folder. The SaveAs
function allows you to save the created PDF file. You can check other .NET Core rendering options with header and 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:
// Create a new instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Render a URL to a PDF document
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Save the rendered PDF to a file
pdf.SaveAs("IronPDF PDF Library.pdf");
// Create a new instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Render a URL to a PDF document
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Save the rendered PDF to a file
pdf.SaveAs("IronPDF PDF Library.pdf");
' Create a new instance of ChromePdfRenderer
Dim renderer = New ChromePdfRenderer()
' Render a URL to a PDF document
Dim pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/")
' Save the rendered PDF to a file
pdf.SaveAs("IronPDF PDF Library.pdf")
The above code will create a PDF file from the given URL parameter passed 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
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.
Frequently Asked Questions
What is the main purpose of this .NET PDF library?
IronPDF is a .NET PDF Library that allows developers to create PDF files easily in C#, F#, and VB.NET for .NET Core and .NET Framework. It provides 'pixel perfect' rendering to create PDF documents using a single line of code without requiring third-party PDF viewers.
How can I install this PDF library in a .NET project?
You can install the IronPDF library using the NuGet Package Manager, NuGet Package Manager Console, or by adding a DLL file to your project.
What are the different methods to create a PDF document with this library?
IronPDF allows you to create a PDF document using an HTML string, an HTML file, or a URL. These methods provide flexibility in how you generate PDF files from different sources.
Can this library convert HTML to PDF?
Yes, IronPDF can convert HTML to PDF using its rendering engine, which allows for the conversion of HTML strings, HTML files, and even URLs to PDF format.
What programming languages are compatible with this PDF library?
IronPDF is compatible with C#, F#, and VB.NET, making it suitable for developers working within the .NET ecosystem.
Is there a trial available for this PDF library?
Yes, IronPDF offers a free trial that allows you to test its full functionality for 30 days before making a purchase.
Does this library require any third-party software to work?
No, IronPDF does not require any third-party PDF viewers like Acrobat Reader. It operates independently to create and manipulate PDF documents.
Can this library handle advanced PDF features like form filling and digital signatures?
Yes, IronPDF supports advanced features such as reading and filling out PDF form fields, extracting images and text, and digitally signing PDF documents.