How to Generate PDF in ASP.NET Using C#

Adobe designed the Portable Document Format (PDF) to allow text and visual formatting in documents. You'll need an additional program to open a PDF file. PDF documents have become increasingly relevant in today's culture. Portable Document Format files are used in a range of organizations for invoicing and document generation. Developers utilize the PDF format to assist their clients in creating documents. Creating PDFs has never been easier thanks to today's libraries. In order to identify which library is the best for a particular project, we must analyze elements such as constructing, reading, and converting.

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, Razer HTML, and MVC View. IronPDF supports the Microsoft.NET application (both ASP.NET core Web applications and traditional Windows applications). IronPDF can also be used to make visually pleasing PDF documents.

With IronPDF, we can create a PDF document from HTML5, JavaScript, CSS, and pictures. The files may also have headers and footers. We can easily read a PDF file thanks to IronPDF. IronPDF also has a complete PDF conversion engine as well as a robust HTML-to-PDF converter that can handle PDF files.

  • HTML, HTML5, ASPX, and Razor/MVC View, ASP.net core MVC are all valid sources for creating PDF files. We can convert not only HTML files, but also picture files to PDF.
  • 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 helps us to 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 us to add headers, footers, text, photos, bookmarks, watermarks, and more to our PDF file.
  • IronPDF allows us to join and split pages in a new or existing document.
  • We can convert PDF files 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 software and go to the File menu. Select "new project", and then select "console application". In this article, we are going to use a console application to generate PDF documents.

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

Then, select the required framework and click the create option.

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

Next, we can 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.

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

In the above image, we can see the list of the related search items. We need to 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.

3.3 Direct download from the NuGet website

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

  • Navigate to the Link.
  • 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 here to download the latest 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. We can create PDF files programmatically by using the code below.

The following steps make it easy to create PDF files:

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

//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().RenderUrlAsPdf("https://www.google.co.in/");

//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()).RenderUrlAsPdf("https://www.google.co.in/")

'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#

We can convert the link into a document, and it will then generate a PDF file. In the next step, we are converting the generated PDF file object into binary, and then later it can be used for downloading the files. In the above code, we are now done with the PDF creation.

4.2 Generate PDF from HTML String

We can transform HTML strings 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. We can create a PDF file by using an HTML string.

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. If needed we can use the "save as" function to save the document to the server location.

5. Generate PDF from HTML Files

We can 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. We can create PDF files by using an HTML string.

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 file with RenderHtmlFileAsPdf. In addition, the function which helps to convert HTML pages from the file into a string can take any number of HTML codes. We can use the above code to download the PDF file through the click of a button.

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

Below is the created PDF document's snapshot.

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. We prefer IronPDF 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 30-day trial key, or you can currently buy five products from Iron Software for the price of just two. To learn more about pricing, click here.