Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
For developers, there are many scenarios where the need arises to create PDF files programmatically. You may need to generate PDF reports and other PDF files programmatically in your software. It would be long, expensive, and inefficient to write your code and functions from scratch. This is where IronPDF comes in. Although creating PDF files programmatically in .NET can be frustrating, thankfully there is now a solution that makes it so much more manageable.
We will cover the following topics in this tutorial:
IronPdf
LibraryIronPdf
NamespaceIronPDF is a C# PDF library that provides a set of classes that can be used to create PDF files programmatically. These classes are located in the `IronPdf`` assembly and are designed to be easy to use with any .NET language, including C#, VB.NET, F#, etc. The library offers many functions for creating PDF documents, manipulating existing PDFs, and reading PDFs.
The current version of the IronPDF library supports the following features:
Let's start with how we can use the IronPDF library to create PDFs programmatically.
This article will use the Console Application template for this demonstration. You can use any according to your requirements and choice. You may also use your existing project in which you wish to add a PDF file-generating tool.
This tutorial will use the Visual Studio and C# programming language to build our project. You should have basic knowledge of HTML and C# Language. It is also recommended that you familiarize yourself with Visual Studio.
We will look at the multiple methods for creating PDF files programmatically using the IronPDF C# library. Let's begin by creating a C# project.
Let's move now to Step 2.
Before proceeding further, we should install the IronPdf
library in our project. We can do this in various ways.
We can install the IronPdf
C# Library from the NuGet Package Manager. Open the NuGet Package Manager by clicking on Tools > NuGet Package Manager > Manage NuGet Package Solution. This will open the NuGet Package Manager Solution Explorer.
Open NuGet Package Manager
Click on Browse and write IronPdf in the search bar. The following result will appear. Select IronPdf and click on the Install button; the library will begin installing.
Search and install IronPdf package
We can use the NuGet Package Manager Console to install our library with ease. It does not require any administrative privilege to install. You will use a NuGet command to install the IronPdf
library in your project.
Install-Package IronPdf
Install IronPdf package via Commandline
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.
Visit a more detailed installation guide.
IronPdf
NamespaceNow add the IronPdf
namespace to your program. You have to add a given line of code on top of the code.
using IronPdf
This will enable you to access the functions of IronPDF. You must add this line of code to every file where you wish to use the IronPDF features.
We will look at multiple methods to create PDFs, as provided by IronPDF. We will see how easy it is to use simple functions to create PDF files. Let's get started with creating PDFs using HTML string.
In the IronPDF library, HTML is used for styling purposes. You must have basic knowledge of HTML language to generate PDF documents using the IronPDF library.
The IronPDF library makes it very easy to process HTML strings and convert them into PDF format. Let's look at how to do it using code.
using IronPdf;
IronPdf.License.LicenseKey = "YourLicenseKey";
string htmlString = "<h1> Generated PDF File!</h1> <p> This file is generated by IronPDF library from HTML String!</p> <p>It is great Library to manipulate PDF Files.</p>";
var renderer = new ChromePdfRenderer();
renderer.RenderHtmlAsPdf(htmlString).SaveAs("PDF from HTML String.pdf");
using IronPdf;
IronPdf.License.LicenseKey = "YourLicenseKey";
string htmlString = "<h1> Generated PDF File!</h1> <p> This file is generated by IronPDF library from HTML String!</p> <p>It is great Library to manipulate PDF Files.</p>";
var renderer = new ChromePdfRenderer();
renderer.RenderHtmlAsPdf(htmlString).SaveAs("PDF from HTML String.pdf");
Imports IronPdf
IronPdf.License.LicenseKey = "YourLicenseKey"
Dim htmlString As String = "<h1> Generated PDF File!</h1> <p> This file is generated by IronPDF library from HTML String!</p> <p>It is great Library to manipulate PDF Files.</p>"
Dim renderer = New ChromePdfRenderer()
renderer.RenderHtmlAsPdf(htmlString).SaveAs("PDF from HTML String.pdf")
This code will generate a PDF file containing the content stored in the htmlString variable. In this code sample, the RenderHtmlAsPdf
function has been used. This function performs the conversion of HTML String to a PDF document.
You must be familiar with HTML tags to generate PDF files using the IronPDF library. We use the SaveAs function to save the output PDF file. You can see the output PDF file below.
Output:
PDF file output
The IronPDF library provides a fantastic feature to create a PDF file from an HTML file. IronPDF directly converts everything in an HTML file, 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 some sample code for creating a PDF file from an HTML file:
using IronPdf;
IronPdf.License.LicenseKey = "YourLicenseKey";
var Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderHtmlFileAsPdf("IronFile.html").SaveAs("IronPDF file.pdf");
using IronPdf;
IronPdf.License.LicenseKey = "YourLicenseKey";
var Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderHtmlFileAsPdf("IronFile.html").SaveAs("IronPDF file.pdf");
Imports IronPdf
IronPdf.License.LicenseKey = "YourLicenseKey"
Dim Renderer = New ChromePdfRenderer()
Dim PDF = Renderer.RenderHtmlFileAsPdf("IronFile.html").SaveAs("IronPDF file.pdf")
We use the RenderHtmlFileAsPdf
function to create PDFs from HTML files. This function is the most important function used in the given code sample. You can give the HTML File location in the function parameter or put the HTML file in the bin folder of the source code, so 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.
Sometimes we need to create PDF files of a URL programmatically in our program. This can be a very technical task if we code everything from scratch. IronPDF helps us here by providing an excellent function for creating PDFs from URLs. You can use any URL to create a PDF.
Let's examine how to do it:
using IronPdf;
IronPdf.License.LicenseKey = "YourLicenseKey";
var Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderUrlAsPdf("https://ironpdf.com/blog/using-ironpdf/csharp-generate-pdf-tutorial/").SaveAs("IronPDF Generate PDF.pdf");
using IronPdf;
IronPdf.License.LicenseKey = "YourLicenseKey";
var Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderUrlAsPdf("https://ironpdf.com/blog/using-ironpdf/csharp-generate-pdf-tutorial/").SaveAs("IronPDF Generate PDF.pdf");
Imports IronPdf
IronPdf.License.LicenseKey = "YourLicenseKey"
Dim Renderer = New ChromePdfRenderer()
Dim PDF = Renderer.RenderUrlAsPdf("https://ironpdf.com/blog/using-ironpdf/csharp-generate-pdf-tutorial/").SaveAs("IronPDF Generate PDF.pdf")
The above code will create a PDF of the given URL parameter to the RenderUrlAsPdf
function. This function creates a PDF of the URL. This would otherwise be a very complex task if you attempted to do it from scratch, so IronPDF provides developers with a simple process to convert any URL to a PDF. The SaveAs function is used for saving output files in the bin folder. To set the file format to PDF, you must specify a .pdf extension with a file name parameter in the SaveAs function. You can see the bin folder in your project folder. The output is displayed below.
Output:
Create PDF file from URL
ASP.NET is a server-side programming framework that extends the functionality of HTML and supports web development in a browser. ASP.NET pages are written in HTML, CSS and JavaScript, and can be coded as ASPX files (.aspx). ASP.NET web forms create user-friendly, functionality-rich websites, online banking, intranets, and accounting systems.
We will use the IronPDF library to convert ASP.NET web forms to PDF. Initially rendered as a web page, we will transform this into a PDF to view and download in a web browser. We will use the AspxToPdf class of IronPDF to complete the conversion.
Let's look at how to do it:
Web Form project structure in Visual Studio
Default.aspx.cs
file. Add the IronPdf
namespace to the file, which is the most important line.using IronPdf;
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
And that's it! A PDF file of the Aspx webpage has been created. When you run the project, you will see a PDF of the web page in the browser. The result is the same as when you make a PDF by pressing Ctrl+P. Here you didn't need to press Ctrl+P to create a PDF of the aspx file.
This has been done on the server-side. In this code, we use the AspxToPdf class of IronPDF. The RenderThisPageAsPdf function creates the PDF of the Aspx webpage.
The above code shows the PDF file in the browser. We can download the PDF file directly in our system by adding the following line of code in the Page_Load function:
AspxToPdf.RenderThisPageAsPdf(IronPdf.AspxToPdf.FileBehavior.Attachment);
This code will download the PDF file of the aspx web page in the system. We can also set the name of the Aspx PDF file. There are multiple options available for modifications and formatting such as headers, footers, printing options, quality, etc. For more details, visit this link.
Output:
Output of rendering a PDF file from server side
Explore the latest Microsoft technology Blazor application for creating PDF files
When creating PDFs, most problems are encountered during the formatting of PDF documents. It isn't easy to format PDF programmatically. But, we now have a solution in the shape of IronPDF. IronPDF assists with various formatting options to allow us to customize the PDF file we are creating. It provides us with methods to add headers, footers, margins, and many other formatting options.
Using IronPDF enables us to set page orientation, page size, and titles. It also supports the changing of font size, font color, font type, and other font-related operations. You can add footers and headers with a divider line and custom text. Let's look at how to customize PDF files using IronPDF.
Our setup of the application is now completed. Next, add the following code to the PDF file:
using IronPdf;
IronPdf.License.LicenseKey = "YourLicenseKey";
ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
Renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Portrait;
Renderer.RenderingOptions.SetCustomPaperSizeInInches(14, 40);
Renderer.RenderingOptions.PrintHtmlBackgrounds = true;
Renderer.RenderingOptions.Title = "PDF Generated";
Renderer.RenderingOptions.Zoom = 100;
//Margin Setting
Renderer.RenderingOptions.MarginTop = 40;
Renderer.RenderingOptions.MarginLeft = 20;
Renderer.RenderingOptions.MarginRight = 20;
Renderer.RenderingOptions.MarginBottom = 40;
Renderer.RenderingOptions.FirstPageNumber = 1;
//Header Setting
Renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
Renderer.RenderingOptions.TextHeader.CenterText = "Header!";
Renderer.RenderingOptions.TextHeader.Font = IronSoftware.Drawing.FontTypes.Helvetica;
//Footer Setting
Renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
Renderer.RenderingOptions.TextFooter.Font = IronSoftware.Drawing.FontTypes.Arial;
Renderer.RenderingOptions.TextFooter.FontSize = 10;
Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
string htmlStr = "<h1>PDF Formatting!</h1><h3>Functions and properties for formatting </h3> <p>IronPDF the best ever library for PDF operations and formatting. </p>";
Renderer.RenderHtmlAsPdf(htmlStr).SaveAs("PDF-Formatted.pdf");
using IronPdf;
IronPdf.License.LicenseKey = "YourLicenseKey";
ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
Renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Portrait;
Renderer.RenderingOptions.SetCustomPaperSizeInInches(14, 40);
Renderer.RenderingOptions.PrintHtmlBackgrounds = true;
Renderer.RenderingOptions.Title = "PDF Generated";
Renderer.RenderingOptions.Zoom = 100;
//Margin Setting
Renderer.RenderingOptions.MarginTop = 40;
Renderer.RenderingOptions.MarginLeft = 20;
Renderer.RenderingOptions.MarginRight = 20;
Renderer.RenderingOptions.MarginBottom = 40;
Renderer.RenderingOptions.FirstPageNumber = 1;
//Header Setting
Renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
Renderer.RenderingOptions.TextHeader.CenterText = "Header!";
Renderer.RenderingOptions.TextHeader.Font = IronSoftware.Drawing.FontTypes.Helvetica;
//Footer Setting
Renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
Renderer.RenderingOptions.TextFooter.Font = IronSoftware.Drawing.FontTypes.Arial;
Renderer.RenderingOptions.TextFooter.FontSize = 10;
Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
string htmlStr = "<h1>PDF Formatting!</h1><h3>Functions and properties for formatting </h3> <p>IronPDF the best ever library for PDF operations and formatting. </p>";
Renderer.RenderHtmlAsPdf(htmlStr).SaveAs("PDF-Formatted.pdf");
Imports IronPdf
IronPdf.License.LicenseKey = "YourLicenseKey"
Dim Renderer As ChromePdfRenderer = New IronPdf.ChromePdfRenderer()
Renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Portrait
Renderer.RenderingOptions.SetCustomPaperSizeInInches(14, 40)
Renderer.RenderingOptions.PrintHtmlBackgrounds = True
Renderer.RenderingOptions.Title = "PDF Generated"
Renderer.RenderingOptions.Zoom = 100
'Margin Setting
Renderer.RenderingOptions.MarginTop = 40
Renderer.RenderingOptions.MarginLeft = 20
Renderer.RenderingOptions.MarginRight = 20
Renderer.RenderingOptions.MarginBottom = 40
Renderer.RenderingOptions.FirstPageNumber = 1
'Header Setting
Renderer.RenderingOptions.TextHeader.DrawDividerLine = True
Renderer.RenderingOptions.TextHeader.CenterText = "Header!"
Renderer.RenderingOptions.TextHeader.Font = IronSoftware.Drawing.FontTypes.Helvetica
'Footer Setting
Renderer.RenderingOptions.TextFooter.DrawDividerLine = True
Renderer.RenderingOptions.TextFooter.Font = IronSoftware.Drawing.FontTypes.Arial
Renderer.RenderingOptions.TextFooter.FontSize = 10
Renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}"
Renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"
Dim htmlStr As String = "<h1>PDF Formatting!</h1><h3>Functions and properties for formatting </h3> <p>IronPDF the best ever library for PDF operations and formatting. </p>"
Renderer.RenderHtmlAsPdf(htmlStr).SaveAs("PDF-Formatted.pdf")
This piece of code is formatting the PDF files. RenderingOptions
is the primary tool to set different properties while creating PDF documents.
For the last lines, we use the RenderHtmlAsPdf
function to create a PDF of HTML String, but we can use an HTML file to convert it into PDF while preserving the current formatting. This is a very handy feature of IronPDF that gives you complete freedom to customize everything to do with PDF documents.
Output:
PDF output with formatting support
This tutorial shows how we can create PDF programmatically using the IronPDF C# library. We have seen how to create PDF files from HTML String, HTML files, URLs, and ASP.NET web forms. 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. IronPDF is ideal for developers and companies who need to manipulate PDF files within their software.
You can try the free version to test it out, and there is a free trial key that doesn't require any credit card payments. Further, the current special offer allows you to get nine products by IronPDF for the price of just two.
9 .NET API products for your office documents