Print in an ASP.NET Framework Web App with C#
IronPrint's PrintAsync method enables non-blocking document printing in ASP.NET web applications, preventing UI freezes while processing print requests. This asynchronous approach ensures responsive web apps can handle print operations without blocking threads.
Web applications often need document printing as final output. Integrating print functionality with web applications presents challenges, particularly when dealing with asynchronous operations. IronPrint solves this with the PrintAsync function. This tutorial demonstrates implementing PrintAsync with ASP.NET Core to create a web application that prints documents without blocking.
Before implementation, note that IronPrint provides comprehensive features including printer information retrieval and custom print settings. These capabilities make it ideal for enterprise ASP.NET applications requiring robust printing functionality.
Quickstart: Async PDF Printing in ASP.NET- Install IronPrint via NuGet:
Install-Package IronPrint - Import
IronPrintinto the controller file - Add a print button to trigger the method
- Call
await Printer.PrintAsync("file.pdf")in the controller action - Verify document printing on button press
-
1Install IronPrint with NuGet Package Manager
-
2Copy and run this code snippet.
return await IronPrint.Printer.PrintAsync("Basic.pdf");C# -
3Deploy to test on your live environment
Start using IronPrint in your project today with a free trial
Get started with IronPrint
Start using IronPrint in your project today with a free trial.
How to Print in an ASP.NET Web Application
- Install the C# printing library from NuGet
- Import
IronPrintinto the controller - Add a print button to trigger the action
- Call
PrintAsyncin the controller method
How Do I Implement Asynchronous PDF Printing in ASP.NET?
This example demonstrates printing a PDF file asynchronously in an ASP.NET Web Application (.NET Framework) project using the PrintAsync method. PrintAsync initiates printing asynchronously, keeping the application responsive compared to synchronous Print methods that block threads.
The asynchronous approach is critical in web applications where multiple users might trigger print operations simultaneously. Unlike the synchronous Print method, PrintAsync ensures your application handles concurrent requests without performance degradation.
Where Should I Add the Print Button?
In your "Index.cshtml" (or home page view), add a button that triggers an action when clicked. This button invokes an ActionResult method in your controller:
@{
ViewBag.Title = "Home Page";
}
<main>
<section class="row" aria-labelledby="aspnetTitle">
<h1 id="title">ASP.NET</h1>
<p>
<!-- Button that triggers the PrintPdf ActionResult -->
<a class="btn btn-primary btn-md" onclick="location.href='@Url.Action("PrintPdf", "Home")'">Print PDF</a>
</p>
</section>
</main>

How Do I Configure PrintAsync in My Controller?
In your HomeController, implement the PrintAsync method. This method performs print operations asynchronously, enhancing application responsiveness. Before implementing, ensure proper license key configuration for production use.
using IronPrint;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace WebApplication4.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
return View();
}
// Action method to handle the printing operation
// This makes use of the PrintAsync method to avoid blocking the main thread
public ActionResult PrintPdf()
{
// Wait for the asynchronous print operation to complete
Printer.PrintAsync("Basic.pdf").Wait();
// Return some view, for example, a confirmation page or the index page
return View(); // Replace with an appropriate view
}
}
}Imports IronPrint
Imports System.Threading.Tasks
Imports System.Web.Mvc
Namespace WebApplication4.Controllers
Public Class HomeController
Inherits Controller
Public Function Index() As ActionResult
Return View()
End Function
Public Function About() As ActionResult
ViewBag.Message = "Your application description page."
Return View()
End Function
Public Function Contact() As ActionResult
Return View()
End Function
' Action method to handle the printing operation
' This makes use of the PrintAsync method to avoid blocking the main thread
Public Function PrintPdf() As ActionResult
' Wait for the asynchronous print operation to complete
Printer.PrintAsync("Basic.pdf").Wait()
' Return some view, for example, a confirmation page or the index page
Return View() ' Replace with an appropriate view
End Function
End Class
End NamespaceFor advanced scenarios, implement proper async/await patterns and customize the print operation. Here's an enhanced example demonstrating error handling and custom print settings:
using IronPrint;
using System;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace WebApplication4.Controllers
{
public class HomeController : Controller
{
// Async action method with proper error handling
public async Task<ActionResult> PrintPdfAdvanced()
{
try
{
// Create custom print settings
var printSettings = new PrintSettings
{
// Select specific printer
PrinterName = "Microsoft Print to PDF",
// Set paper size
PaperSize = PaperSize.A4,
// Configure orientation
PaperOrientation = PaperOrientation.Portrait,
// Set number of copies
NumberOfCopies = 1,
// Configure DPI for high-quality output
Dpi = 300
};
// Print asynchronously with custom settings
await Printer.PrintAsync("Basic.pdf", printSettings);
// Log successful print operation (optional)
System.Diagnostics.Debug.WriteLine("Document printed successfully");
// Return success view or redirect
TempData["PrintMessage"] = "Document sent to printer successfully!";
return RedirectToAction("Index");
}
catch (Exception ex)
{
// Handle printing errors gracefully
System.Diagnostics.Debug.WriteLine($"Printing error: {ex.Message}");
TempData["ErrorMessage"] = "Unable to print document. Please try again.";
return RedirectToAction("Index");
}
}
}
}Imports IronPrint
Imports System
Imports System.Threading.Tasks
Imports System.Web.Mvc
Namespace WebApplication4.Controllers
Public Class HomeController
Inherits Controller
' Async action method with proper error handling
Public Async Function PrintPdfAdvanced() As Task(Of ActionResult)
Try
' Create custom print settings
Dim printSettings As New PrintSettings With {
' Select specific printer
.PrinterName = "Microsoft Print to PDF",
' Set paper size
.PaperSize = PaperSize.A4,
' Configure orientation
.PaperOrientation = PaperOrientation.Portrait,
' Set number of copies
.NumberOfCopies = 1,
' Configure DPI for high-quality output
.Dpi = 300
}
' Print asynchronously with custom settings
Await Printer.PrintAsync("Basic.pdf", printSettings)
' Log successful print operation (optional)
System.Diagnostics.Debug.WriteLine("Document printed successfully")
' Return success view or redirect
TempData("PrintMessage") = "Document sent to printer successfully!"
Return RedirectToAction("Index")
Catch ex As Exception
' Handle printing errors gracefully
System.Diagnostics.Debug.WriteLine($"Printing error: {ex.Message}")
TempData("ErrorMessage") = "Unable to print document. Please try again."
Return RedirectToAction("Index")
End Try
End Function
End Class
End NamespaceThis enhanced implementation showcases concepts from the Print Settings guide, including specifying printer names, configuring paper sizes, and handling errors appropriately.
When working with printer selection in web environments, leverage the Get Printer Names functionality to dynamically populate available printer lists:
// Get list of available printers
public ActionResult GetAvailablePrinters()
{
var printers = Printer.GetPrinterNames();
ViewBag.PrinterList = new SelectList(printers);
return View();
}' Get list of available printers
Public Function GetAvailablePrinters() As ActionResult
Dim printers = Printer.GetPrinterNames()
ViewBag.PrinterList = New SelectList(printers)
Return View()
End FunctionFor scenarios requiring user interaction, consider implementing a Print with Dialog approach, though this suits desktop applications better than web environments.
Additional Considerations for Production Deployment
When deploying your ASP.NET application with IronPrint, consider these factors:
- License Configuration: For ASP.NET applications, configure your license key in Web.config. See the Setting License Key in Web.config guide for proper setup.
- Printer Access: Ensure the application pool identity has permissions to access local or network printers. The Print Your Documents documentation provides printer access requirements.
- Error Handling: Implement robust error handling for offline printers or inaccessible documents. For technical issues, use the Engineering Request process to resolve complex problems.
- Performance: For high-volume printing, implement a queue system to manage print requests efficiently. The asynchronous
PrintAsyncis ideal for such implementations.
For comprehensive printing capabilities, consult the API Reference for detailed documentation of all methods and properties in the IronPrint namespace.
Frequently Asked Questions
What is the main advantage of using IronPrint's PrintAsync method in ASP.NET web applications?
IronPrint's PrintAsync method enables non-blocking document printing, ensuring your ASP.NET web application remains responsive while handling print requests.
How can I integrate IronPrint into my ASP.NET web application?
To integrate IronPrint, install the library via NuGet, import it into your controller files, add a print button for triggering the print action, and use the PrintAsync method for asynchronous printing.
Why should I choose asynchronous printing in my web application?
Asynchronous printing with IronPrint's PrintAsync prevents UI freezes and allows concurrent handling of multiple print requests, which boosts the responsiveness of your web application.
How can I configure custom print settings using IronPrint?
You can configure custom print settings by creating an instance of PrintSettings, specifying parameters such as printer name, paper size, orientation, copies, and DPI, and passing it to the PrintAsync method.
Where do I add the print button in my ASP.NET web application?
Add the print button in the 'Index.cshtml' or your home page view. This button should invoke an ActionResult method in your controller to trigger printing.
How can I handle errors during the printing process?
Use try-catch blocks around your PrintAsync calls to catch exceptions and handle them gracefully, providing user feedback or logging as necessary.
What should I consider for deploying an ASP.NET application using IronPrint?
Ensure your application's license configuration, printer access permissions, and error handling mechanisms are properly set up in production.
How can I dynamically populate available printer lists in my application?
Use the GetPrinterNames method from IronPrint to retrieve and dynamically display a list of available printers to users.
What is the role of the PrintAsync method in managing high-volume printing?
PrintAsync supports high-volume printing by enabling a queue system that efficiently manages print requests without blocking the application.
Is it possible to use IronPrint for printing with a dialog in a web environment?
While it's possible to integrate a print dialog in desktop applications, this approach is less suitable for web environments, where asynchronous methods are preferred for best performance.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.