USING IRONPDF

How to Generate PDF in ASP.NET Using C#

Updated June 28, 2022
Share:

This tutorial will demonstrate how to efficiently create and edit PDF files using the IronPDF C# library.

1. IronPDF Features

IronPDF is a powerful PDF library that can do almost anything a browser can. The .NET library for developers makes it easy to create, read, and manipulate PDF documents. IronPDF uses the Chrome engine to convert documents from HTML to PDF. Among other web components, IronPDF supports HTML, ASPX, Razor HTML, and MVC View. IronPDF supports Microsoft .NET Applications (both ASP.NET Core Web Applications and traditional Windows Applications). IronPDF can also be used to make visually pleasing PDF documents.

  • HTML, HTML5, ASPX, and Razor/MVC View, ASP.NET Core MVC are all valid sources for creating PDF files, not limited to HTML files but also picture files.
  • IronPDF lets you create interactive PDF documents, fill out and submit interactive forms, merge and split a PDF file into multiple PDF documents, extract text and images from PDF files, search text in a PDF file, rasterize PDF pages to images, convert PDF to HTML, and print PDF files.
  • IronPDF can generate PDF files from a URL. For login behind HTML login forms, it also allows custom network login credentials, user agents, proxies, cookies, HTTP headers, and form variables.
  • IronPDF is a tool for viewing and filling out PDF documents.
  • IronPDF can be used to extract images from a PDF document.
  • IronPDF allows to add headers, footers, text, photos, bookmarks, watermarks, and more to any PDF file.
  • IronPDF allows you to join and split pages in a new or existing document.
  • The PDF files can be converted into PDF objects without an Acrobat viewer.
  • A PDF file can be created from a CSS file.
  • CSS files of the media type can be converted into documents.

2. Creating a New Project in Visual Studio

Open the Visual Studio and go to the File menu. Select "new project", and then select "ASP .NET Core Web App" as a starting template to generate PDF documents.

How to Generate PDF in ASP.NET Using C#, Figure 1: Create a new project in Visual Studio Create a new project in Visual Studio

Enter the project name and select the file path in the appropriate text box. Then, click the Create button and select the required .NET Framework, as in the screenshot below.

How to Generate PDF in ASP.NET Using C#, Figure 2: Configure the project within Visual Studio Configure the project within Visual Studio

Then, select the required .NET Famework and click the Create option.

How to Generate PDF in ASP.NET Using C#, Figure 3: Select .NET Framework Select .NET Framework

The Visual Studio project will now generate the structure for the selected application. Since this example are using ASP.NET MVC, it is necessary to create a Controller to write the code or use the existing Controller where you can enter the code and build/run the application.

How to Generate PDF in ASP.NET Using C#, Figure 4: Create a Controller for ASP.NET MVC Application Create a Controller for ASP.NET MVC Application

The next section shows how to add the library to test the code.

3. Install the IronPDF Library

The IronPDF Library can be downloaded and installed in four ways.

These are:

  • Using Visual Studio.
  • Using the Visual Studio Command Line.
  • Direct download from the NuGet website.
  • Direct download from the IronPDF website.

3.1 Using Visual Studio

The Visual Studio software provides the NuGet Package Manager option to install the package directly to the solution. The below screenshot shows how to open the NuGet Package Manager.

How to Generate PDF in ASP.NET Using C#, Figure 5: Navigate to NuGet Package Manager Navigate to NuGet Package Manager

It provides the search box to show the list of packages from the NuGet website. In the NuGet Package Manager, search for the keyword "IronPDF", as in the screenshot below.

How to Generate PDF in ASP.NET Using C#, Figure 6: Install IronPdf package in NuGet Package Manager UI Install IronPdf package in NuGet Package Manager UI

In the above image, it shows the list of the related search items. Select the required option to install the package to the solution.

3.2 Using the Visual Studio Command-Line

In Visual Studio, go to Tools > NuGet Package Manager > Package Manager Console

Enter the following line in the Package Manager Console tab:

Install-Package IronPdf

Now the package will download/install to the current project and be ready to use.

How to Generate PDF in ASP.NET Using C#, Figure 7: Install the package in the Package Manager Console Install the package in the Package Manager Console

3.3 Direct download from the NuGet website

The third way is to download the NuGet package directly from the website.

  • Navigate to the official package on the NuGet website.
  • Select the download package option from the menu on the right-hand side.
  • Double-click the downloaded package. It will be installed automatically.
  • Next, reload the solution and start using it in the project.

3.4 Direct download from the IronPDF website

Click the link to download the Zip file package directly from the website. Once downloaded, follow the steps below to add the package to the project.

  • Right-click the project from the solution window.
  • Then, select the options reference and browse the location of the downloaded reference.
  • Next, click OK to add the reference.

4. Generate a PDF file

There are three ways to generate a PDF file.

  • Generate PDF from URL
  • Generate PDF from HTML string
  • Generate PDF from string

4.1 Generate PDF from URL

IronPDF makes it easy to create a PDF document by generating an HTML file from a URL and converting it to a PDF document. The HTML data will be downloaded using IronPDF's built-in Chrome browser.

The following steps make it easy to create PDF files:

using var pdfFile = new IronPdf.ChromePdfRenderer().RenderUrlAsPdf("https://www.google.co.in/");

//Read the File as Byte Array.
byte [] bytes = pdfFile.BinaryData;
//Convert File to Base64 string and send to Client.
string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
return Content(base64);
using var pdfFile = new IronPdf.ChromePdfRenderer().RenderUrlAsPdf("https://www.google.co.in/");

//Read the File as Byte Array.
byte [] bytes = pdfFile.BinaryData;
//Convert File to Base64 string and send to Client.
string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
return Content(base64);
Dim pdfFile = (New IronPdf.ChromePdfRenderer()).RenderUrlAsPdf("https://www.google.co.in/")

'Read the File as Byte Array.
Dim bytes() As Byte = pdfFile.BinaryData
'Convert File to Base64 string and send to Client.
Dim base64 As String = Convert.ToBase64String(bytes, 0, bytes.Length)
Return Content(base64)
VB   C#

In the above code, the PDF creation process is done.

4.2 Generate PDF from HTML String

HTML strings can also be transformed into PDF files with the help of IronPDF. The sample code for transforming HTML strings into documents is shown below. It also allows you to convert any HTML element into a PDF file.

using var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello world!!</h1>");
using var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello world!!</h1>");
Dim pdf = (New IronPdf.ChromePdfRenderer()).RenderHtmlAsPdf("<h1>Hello world!!</h1>")
VB   C#

The accompanying example shows how to translate HTML with RenderHtmlAsPdf. In addition, the function which helps to convert HTML pages to a string can take any number of HTML codes. Additionally, the SaveAs function can be used to save the document to the server location.

5. Generate PDF from HTML Files

This section will show how to transform HTML files into PDF files with the help of IronPDF. The sample code for transforming HTML into documents is shown below. It also allows you to convert any HTML element into a PDF file.

using var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlFileAsPdf("demo.html");
//Read the File as Byte Array.
byte [] bytes = pdf.BinaryData;
//Convert File to Base64 string and send to Client.
string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
return Content(base64);
using var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlFileAsPdf("demo.html");
//Read the File as Byte Array.
byte [] bytes = pdf.BinaryData;
//Convert File to Base64 string and send to Client.
string base64 = Convert.ToBase64String(bytes, 0, bytes.Length);
return Content(base64);
Dim pdf = (New IronPdf.ChromePdfRenderer()).RenderHtmlFileAsPdf("demo.html")
'Read the File as Byte Array.
Dim bytes() As Byte = pdf.BinaryData
'Convert File to Base64 string and send to Client.
Dim base64 As String = Convert.ToBase64String(bytes, 0, bytes.Length)
Return Content(base64)
VB   C#

The accompanying example shows how to translate HTML files with RenderHtmlFileAsPdf. In addition, the function that helps to convert HTML pages from the file into a string can take any number of HTML codes. The above code will return data to download the PDF file through the click of a button.

Below is the sample page created using the ASP.NET MVC.

How to Generate PDF in ASP.NET Using C#, Figure 8: The ASP.NET application to generate PDF file The ASP.NET application to generate PDF file

Below is the created PDF document's snapshot.

How to Generate PDF in ASP.NET Using C#, Figure 9: The result PDF file generated from HTML files The result PDF file generated from HTML files

6. Conclusion

Finally, when producing HTML, IronPDF takes the user's browser preferences into account. IronPDF is capable of producing HTML at a faster rate. IronPDF includes royalty-free redistribution protection. The IronPDF is recommended because of its high-performance levels and the vast range of capabilities available to developers working with the Portable Document Format. IronPDF also offers excellent assistance and documentation to guarantee that customers can take advantage of all the program's remarkable features.

IronPDF offers a free trial key, or you can currently buy five products from Iron Software for the price of just two. Visit the pricing page to learn more about pricing.

< PREVIOUS
Programmatically Fill PDF Forms in C# (Coding Tutorial)
NEXT >
C# Extract Text From PDF (Code Example Tutorial)

Ready to get started? Version: 2024.6 just released

Start Free Trial Total downloads: 9,661,997
View Licenses >