USING IRONPRINT

How To Print PDF Files in .NET Core

.NET Core, an open-source, cross-platform framework developed by Microsoft, has been gaining popularity for its flexibility, performance, and support for cloud-based applications. However, when it comes to working with PDF files, particularly for tasks like printing PDF documents, developers require a robust and feature-rich PDF library. This is where IronPDF helps the developers.

IronPDF is a comprehensive library designed for the .NET framework, including .NET Core and ASP.NET Core, which simplifies the process of working with PDF documents. It not only allows for the creation and manipulation of PDF files but also provides a seamless way to print these documents, whether it’s directly to a printer or by converting them to formats suitable for print.

In this tutorial, we will delve into the capabilities of IronPDF within a .NET Core environment. From setting up your project and creating your first PDF document to configuring print settings and implementing advanced printing features, we will guide you through each step. This tutorial aims to equip you with the knowledge and tools necessary to handle printing PDF files in your .NET Core applications efficiently.

How To Print PDF Files in .NET Core

  1. Create an ASP.NET Core Web project in Visual Studio
  2. Install the PDF Library using NuGet Package Manager
  3. Create or Load the PDF document in the controller
  4. Use the PDF library to print the loaded PDF file

Setting Up Your .NET Core Project

Installing IronPDF - The .NET PDF Library

To start working with PDFs in your .NET application, the first step is to integrate the IronPDF library. IronPDF is a powerful and versatile library that enables .NET developers to create, edit, and, most importantly, print PDF documents with ease. Let’s walk through the installation process:

Creating Your .NET Core Project: Open Visual Studio and select "Create a new project." In the project template selection window, filter by "Web" under "All platforms" and select "ASP.NET Core Web App."

How To Print PDF Files in .NET Core: Figure 1 - Selecting the ASP.NET Core Web App to create a new project

Installing IronPDF: Go to the "NuGet Package Manager" and search for "IronPDF" to install it into your project. Ensure that the IronPDF library is correctly installed and referenced in your project files. You need to include the appropriate using statements in your code, such as using IronPdf;

How To Print PDF Files in .NET Core: Figure 2 - Using the NuGet browser to find the IronPDF library

Creating a Basic PDF Document in ASP.NET Core

To create a PDF document using IronPDF in an ASP.NET Core web application, you will begin by adding some code to one of your controllers. Here's a simple example to get you started:

Set Up a New Controller

Create a new controller in your project, which will be responsible for handling PDF creation requests. You can name it PdfController, for instance.

Write the Action Method

Inside your new controller, write an action method named CreatePdf that returns a PDF file as a result.

using IronPdf;
using Microsoft.AspNetCore.Mvc;

namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        // Action method for creating a PDF document
        public IActionResult CreatePdf()
        {
            // Create a new PDF document from HTML content
            var renderer = new ChromePdfRenderer();
            var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, IronPDF!</h1><p>This is a simple PDF document created in an ASP.NET Core web app.</p>");

            // Save the generated PDF to the server's memory
            var content = pdf.Stream.ToArray();

            // Return the PDF to the browser as a downloadable file
            return File(content, "application/pdf", "MyFirstPdf.pdf");
        }
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;

namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        // Action method for creating a PDF document
        public IActionResult CreatePdf()
        {
            // Create a new PDF document from HTML content
            var renderer = new ChromePdfRenderer();
            var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, IronPDF!</h1><p>This is a simple PDF document created in an ASP.NET Core web app.</p>");

            // Save the generated PDF to the server's memory
            var content = pdf.Stream.ToArray();

            // Return the PDF to the browser as a downloadable file
            return File(content, "application/pdf", "MyFirstPdf.pdf");
        }
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc

Namespace YourProjectName.Controllers
	Public Class PdfController
		Inherits Controller

		' Action method for creating a PDF document
		Public Function CreatePdf() As IActionResult
			' Create a new PDF document from HTML content
			Dim renderer = New ChromePdfRenderer()
			Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello, IronPDF!</h1><p>This is a simple PDF document created in an ASP.NET Core web app.</p>")

			' Save the generated PDF to the server's memory
			Dim content = pdf.Stream.ToArray()

			' Return the PDF to the browser as a downloadable file
			Return File(content, "application/pdf", "MyFirstPdf.pdf")
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

Run Your Application

Start your application and navigate to the CreatePdf action in your PdfController. For example, if your application is running on localhost with port 5000, go to http://localhost:<Your-Port>/Pdf/CreatePdf in your web browser.

Download the PDF

Upon accessing the URL, the PDF document will be generated and downloaded through your web browser. To view the generated PDF, you will need a PDF viewer installed on your computer.

Printing PDF Documents in .NET Core Using IronPDF

Once you've mastered the creation of PDF documents within your ASP.NET Core Web App, the next step is to implement printing functionality. IronPDF provides a straightforward way to print a PDF document inside your project, to a printer accessible by the server on which your application is running.

Configuring the Default Printer and Printer Name

To print a PDF document, you will need to configure the printer settings within your application. IronPDF allows you to specify the printer by its name, which can be a locally installed printer or a network printer. Additionally, you can define other settings such as the paper source or orientation.

Here's an example method within your PdfController class program that configures the printer settings and initiates the print job:

using IronPdf;
using Microsoft.AspNetCore.Mvc;

namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        // Action method for printing a PDF document
        public IActionResult PrintPdf()
        {
            // HTML string to be converted to PDF
            var htmlContent = "<h1>Invoice</h1><p>Thank you for your business!</p>";

            // Render the HTML content to a PDF in memory
            var renderer = new ChromePdfRenderer();
            var pdf = renderer.RenderHtmlAsPdf(htmlContent);

            // Get the print document from the PDF
            var printDoc = pdf.GetPrintDocument();

            // Set the printer name (replace with your printer's actual name)
            printDoc.PrinterSettings.PrinterName = "Your Printer Name"; 

            // Optional: Configure additional printer settings
            // e.g., printDoc.PrinterSettings.Copies = 2;
            // e.g., printDoc.DefaultPageSettings.Landscape = true;

            // Send the document to the printer
            printDoc.Print();

            // Return a confirmation response to the client
            return Content("The document has been sent to the printer.");
        }
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;

namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        // Action method for printing a PDF document
        public IActionResult PrintPdf()
        {
            // HTML string to be converted to PDF
            var htmlContent = "<h1>Invoice</h1><p>Thank you for your business!</p>";

            // Render the HTML content to a PDF in memory
            var renderer = new ChromePdfRenderer();
            var pdf = renderer.RenderHtmlAsPdf(htmlContent);

            // Get the print document from the PDF
            var printDoc = pdf.GetPrintDocument();

            // Set the printer name (replace with your printer's actual name)
            printDoc.PrinterSettings.PrinterName = "Your Printer Name"; 

            // Optional: Configure additional printer settings
            // e.g., printDoc.PrinterSettings.Copies = 2;
            // e.g., printDoc.DefaultPageSettings.Landscape = true;

            // Send the document to the printer
            printDoc.Print();

            // Return a confirmation response to the client
            return Content("The document has been sent to the printer.");
        }
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc

Namespace YourProjectName.Controllers
	Public Class PdfController
		Inherits Controller

		' Action method for printing a PDF document
		Public Function PrintPdf() As IActionResult
			' HTML string to be converted to PDF
			Dim htmlContent = "<h1>Invoice</h1><p>Thank you for your business!</p>"

			' Render the HTML content to a PDF in memory
			Dim renderer = New ChromePdfRenderer()
			Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)

			' Get the print document from the PDF
			Dim printDoc = pdf.GetPrintDocument()

			' Set the printer name (replace with your printer's actual name)
			printDoc.PrinterSettings.PrinterName = "Your Printer Name"

			' Optional: Configure additional printer settings
			' e.g., printDoc.PrinterSettings.Copies = 2;
			' e.g., printDoc.DefaultPageSettings.Landscape = true;

			' Send the document to the printer
			printDoc.Print()

			' Return a confirmation response to the client
			Return Content("The document has been sent to the printer.")
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

Remember to replace "Your Printer Name" with the actual name of the printer in your environment. The printer should be accessible to the server where the ASP.NET Core application is running. When you run the program and go to the URL "**https://localhost:<Your-Port>/Pdf/PrintPdf**", you'll see the output message indicating that the PDF has been sent to the printer.

How To Print PDF Files in .NET Core: Figure 3 - Output message from previous code

Conclusion

Throughout this tutorial, we've explored the functionality and capabilities of IronPDF within the context of an ASP.NET Core application. From setting up your project with IronPDF, creating and manipulating PDF documents, to the more intricate processes involved in printing these documents, IronPDF has proven to be a robust and versatile tool for handling PDFs in .NET Core.

For those interested in utilizing IronPDF, it's worth noting that the library offers a free trial, allowing you to evaluate its features before committing. If you find it suits your needs, IronPDF licenses start from $749, providing a scalable solution for both small and large-scale projects. Below you can see the pricing for IronXL licensing, and you can click here to view more.

How To Print PDF Files in .NET Core: Figure 4 - IronPDF licensing page

Frequently Asked Questions

What is IronPrint?

IronPrint is Iron Software's .NET printing library, compatible with multiple platforms such as Windows, macOS, Android, and iOS.

What is the purpose of this tutorial?

This tutorial aims to equip developers with the knowledge and tools necessary to handle printing PDF files in .NET Core applications efficiently using IronPDF.

How do I start using IronPDF in my .NET Core project?

To start using IronPDF, you need to create a new ASP.NET Core Web project in Visual Studio and install IronPDF via the NuGet Package Manager.

What are the basic steps to print a PDF in .NET Core using IronPDF?

To print a PDF using IronPDF, create or load the PDF document in the controller, configure printer settings, and use the PDF library to send the document to the printer.

How can I create a PDF document in ASP.NET Core using IronPDF?

In ASP.NET Core, you can create a PDF by rendering HTML content as a PDF document using the ChromePdfRenderer class from IronPDF.

Can IronPDF be used to print directly to a printer?

Yes, IronPDF can print directly to a printer by configuring printer settings and initiating a print job within your ASP.NET Core application.

What is required to configure the printer settings for IronPDF?

You need to specify the printer name and optionally configure additional settings like the number of copies or page orientation in your application code.

Is there a free trial available for IronPDF?

Yes, IronPDF offers a free trial, allowing you to evaluate its features before committing to a purchase.

Where can I find more detailed documentation on IronPrint?

Detailed documentation on IronPrint is available on Iron Software's website, under the IronPrint section.

What platforms does IronPDF support?

IronPDF supports various platforms, including .NET Core, ASP.NET Core, and is compatible with Windows, macOS, Android, and iOS.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
< PREVIOUS
How to Print a PDF from a Network Printer Using IronPDF
NEXT >
C# Print PDF Files using IronPDF

Ready to get started? Version: 2025.6 just released

View Licenses >