USING IRONPDF

How to Generate PDF in ASP.NET Using C#

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 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 Framework 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 is 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:

  • 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 IronPDF Package on NuGet.
  • 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

Visit the IronPDF Download Page to download the Zip file package directly. 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 from HTML content.

  • 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:

// Example ASP.NET Controller demonstrating PDF generation from a URL
public class PdfController : Controller
{
    // Action to generate PDF from a specific URL
    public IActionResult GeneratePdf()
    {
        // Create a new instance of the ChromePdfRenderer
        var renderer = new ChromePdfRenderer();

        // Render a PDF from a specified URL
        PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.google.co.in/");

        // Return the PDF as a file result to be downloaded
        return File(pdf.BinaryData, "application/pdf", "output.pdf");
    }
}
// Example ASP.NET Controller demonstrating PDF generation from a URL
public class PdfController : Controller
{
    // Action to generate PDF from a specific URL
    public IActionResult GeneratePdf()
    {
        // Create a new instance of the ChromePdfRenderer
        var renderer = new ChromePdfRenderer();

        // Render a PDF from a specified URL
        PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.google.co.in/");

        // Return the PDF as a file result to be downloaded
        return File(pdf.BinaryData, "application/pdf", "output.pdf");
    }
}
$vbLabelText   $csharpLabel

In the above code, the PDF creation process is demonstrated using a URL.

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.

// Generate PDF from HTML string
using var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello world!!</h1>");

// Save the generated PDF document to the specified location
pdf.SaveAs("output.pdf");
// Generate PDF from HTML string
using var pdf = new IronPdf.ChromePdfRenderer().RenderHtmlAsPdf("<h1>Hello world!!</h1>");

// Save the generated PDF document to the specified location
pdf.SaveAs("output.pdf");
$vbLabelText   $csharpLabel

The above example illustrates how to convert an HTML string into a PDF document using the RenderHtmlAsPdf method. Additionally, the SaveAs function is used to save the PDF document to a 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 allows converting an HTML file into a PDF file.

// Example ASP.NET Controller demonstrating PDF generation from an HTML file
public class PdfController : Controller
{
    // Action to generate PDF from an HTML file
    public IActionResult GeneratePdf()
    {
        // Create a new instance of the ChromePdfRenderer
        var renderer = new ChromePdfRenderer();

        // Render a PDF from an HTML file
        PdfDocument pdf = renderer.RenderHtmlFileAsPdf("demo.html");

        // Return the PDF as a file result to be downloaded
        return File(pdf.BinaryData, "application/pdf", "output.pdf");
    }
}
// Example ASP.NET Controller demonstrating PDF generation from an HTML file
public class PdfController : Controller
{
    // Action to generate PDF from an HTML file
    public IActionResult GeneratePdf()
    {
        // Create a new instance of the ChromePdfRenderer
        var renderer = new ChromePdfRenderer();

        // Render a PDF from an HTML file
        PdfDocument pdf = renderer.RenderHtmlFileAsPdf("demo.html");

        // Return the PDF as a file result to be downloaded
        return File(pdf.BinaryData, "application/pdf", "output.pdf");
    }
}
$vbLabelText   $csharpLabel

The above example illustrates how to convert an HTML file into a PDF document using the RenderHtmlFileAsPdf method. The code will return data to download the PDF file through the click of a button.

Below is a 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 snapshot of the created PDF document.

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 IronPDF Licensing Page to learn more about pricing.

Frequently Asked Questions

What is IronPDF?

IronPDF is a powerful .NET PDF library that allows developers to create, read, and manipulate PDF documents using C#. It uses the Chrome engine to convert documents from HTML to PDF and supports various web components like HTML, ASPX, Razor HTML, MVC View, and more.

How can I install IronPDF in an ASP.NET project?

You can install IronPDF in an ASP.NET project via the NuGet Package Manager in Visual Studio, the Visual Studio Command-Line, or by directly downloading it from the NuGet website or IronPDF website.

Can IronPDF generate PDF files from a URL?

Yes, IronPDF can generate PDF files from a URL by using built-in Chrome browser capabilities to download HTML data and convert it into a PDF document.

Is it possible to create a PDF from an HTML string using IronPDF?

Yes, IronPDF can transform HTML strings into PDF files using the RenderHtmlAsPdf method.

What are the steps to create a new ASP.NET project for PDF generation?

To create a new ASP.NET project for PDF generation, open Visual Studio, create a new ASP.NET Core Web App project, select the required .NET Framework, and configure your project settings.

What are some features of IronPDF?

IronPDF offers features such as creating interactive PDF documents, merging and splitting PDFs, extracting text and images, searching text, converting PDF to HTML, and adding headers, footers, bookmarks, and watermarks.

How can I generate a PDF from an HTML file?

You can generate a PDF from an HTML file using IronPDF's RenderHtmlFileAsPdf method, which allows you to convert an HTML file into a PDF document.

Does IronPDF support form interaction and login functionalities?

Yes, IronPDF supports form interaction, such as filling out and submitting interactive forms, and it allows custom network login credentials for handling logins behind HTML forms.

Can IronPDF be used in both ASP.NET Core and traditional Windows Applications?

Yes, IronPDF supports both ASP.NET Core Web Applications and traditional Windows Applications, making it versatile for different types of .NET applications.

What is the benefit of using IronPDF?

IronPDF offers high-performance PDF generation and manipulation capabilities with royalty-free redistribution protection. It also comes with excellent support and documentation, making it a reliable choice for developers.

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.
< PREVIOUS
Programmatically Fill PDF Forms in C# (Coding Tutorial)
NEXT >
C# Extract Text From PDF (Code Example Tutorial)