USING IRONPRINT

How To Print PDF Files in .NET Core

Published January 14, 2024
Share:

Introduction

.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
    {
        public IActionResult CreatePdf()
        {
            // Create a new PDF document
            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 PDF to the server's memory
            var content = pdf.Stream.ToArray();
            // Return the PDF to the browser as a file download
            return File(content, "application/pdf", "MyFirstPdf.pdf");
        }
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        public IActionResult CreatePdf()
        {
            // Create a new PDF document
            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 PDF to the server's memory
            var content = pdf.Stream.ToArray();
            // Return the PDF to the browser as a file download
            return File(content, "application/pdf", "MyFirstPdf.pdf");
        }
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Namespace YourProjectName.Controllers
	Public Class PdfController
		Inherits Controller

		Public Function CreatePdf() As IActionResult
			' Create a new PDF document
			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 PDF to the server's memory
			Dim content = pdf.Stream.ToArray()
			' Return the PDF to the browser as a file download
			Return File(content, "application/pdf", "MyFirstPdf.pdf")
		End Function
	End Class
End Namespace
VB   C#

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. If you want to view the generated PDF, you will have to use a PDF viewer to read it 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;
public class PdfController : Controller
{
    public IActionResult PrintPdf()
    {
        // Assuming 'htmlContent' is the HTML string you want to print
        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 PrintDocument from the PDF
        var printDoc = pdf.GetPrintDocument();
        // Set the printer name
        printDoc.PrinterSettings.PrinterName = "Your Printer Name"; // Replace with your printer's name
        // Optional: Set other printer settings
        // printDoc.PrinterSettings.Copies = 2;
        // printDoc.DefaultPageSettings.Landscape = true;
        // Print the document
        printDoc.Print();
        // Return a response to the client, e.g., a confirmation message
        return Content("The document has been sent to the printer.");
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class PdfController : Controller
{
    public IActionResult PrintPdf()
    {
        // Assuming 'htmlContent' is the HTML string you want to print
        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 PrintDocument from the PDF
        var printDoc = pdf.GetPrintDocument();
        // Set the printer name
        printDoc.PrinterSettings.PrinterName = "Your Printer Name"; // Replace with your printer's name
        // Optional: Set other printer settings
        // printDoc.PrinterSettings.Copies = 2;
        // printDoc.DefaultPageSettings.Landscape = true;
        // Print the document
        printDoc.Print();
        // Return a response to the client, e.g., a confirmation message
        return Content("The document has been sent to the printer.");
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Public Class PdfController
	Inherits Controller

	Public Function PrintPdf() As IActionResult
		' Assuming 'htmlContent' is the HTML string you want to print
		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 PrintDocument from the PDF
		Dim printDoc = pdf.GetPrintDocument()
		' Set the printer name
		printDoc.PrinterSettings.PrinterName = "Your Printer Name" ' Replace with your printer's name
		' Optional: Set other printer settings
		' printDoc.PrinterSettings.Copies = 2;
		' printDoc.DefaultPageSettings.Landscape = true;
		' Print the document
		printDoc.Print()
		' Return a response to the client, e.g., a confirmation message
		Return Content("The document has been sent to the printer.")
	End Function
End Class
VB   C#

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 following URL "**https://localhost:<Your-Port>/Pdf/PrintPdf**", you'll see the following message:

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

It means that the PDF is sent to the printer.

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

< PREVIOUS
How to Print a PDF from a Network Printer Using IronPDF
NEXT >
C# Print PDF Files using IronPDF

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 12,281 View Licenses >