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
-
Install IronPrint with NuGet Package Manager
PM > Install-Package IronPrint -
Copy and run this code snippet.
return await IronPrint.Printer.PrintAsync("Basic.pdf"); -
Deploy 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>
<a class="btn btn-primary btn-md" onclick="location.href='@Url.Action("PrintPdf", "Home")'">Print PDF</a>
</p>
</section>
</main>
@{
ViewBag.Title = "Home Page";
}
<main>
<section class="row" aria-labelledby="aspnetTitle">
<h1 id="title">ASP.NET</h1>
<p>
<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
}
}
}
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 Namespace
For 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");
}
}
}
}
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 Namespace
This 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 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 Function
For 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
How do I implement asynchronous PDF printing in ASP.NET Framework applications?
You can implement asynchronous PDF printing using IronPrint's PrintAsync method. Simply add `return await IronPrint.Printer.PrintAsync("yourfile.pdf");` in your controller action. This non-blocking approach ensures your web application remains responsive while processing print requests, preventing UI freezes during document printing operations.
Why should I use asynchronous printing instead of synchronous printing in web applications?
Asynchronous printing with IronPrint's PrintAsync method is critical for web applications where multiple users might trigger print operations simultaneously. Unlike synchronous Print methods that block threads, PrintAsync ensures your application handles concurrent requests without performance degradation, maintaining responsiveness even under heavy load.
What are the minimal steps to add PDF printing to my ASP.NET Framework project?
The minimal workflow involves 5 steps: 1) Download IronPrint library for C#, 2) Import IronPrint into your class file, 3) Add a print button to your view, 4) Implement PrintAsync in your controller action, and 5) Verify document printing works when the button is pressed. This streamlined process requires minimal code changes.
How do I add a print button to my ASP.NET view?
In your Index.cshtml or home page view, add a button that triggers a controller action. Use HTML like ``. This button will invoke the PrintPDF ActionResult method in your Home controller when clicked.
Can I customize print settings when using asynchronous printing?
Yes, IronPrint provides comprehensive features including custom print settings and printer information retrieval. These capabilities make it ideal for enterprise ASP.NET applications requiring robust printing functionality with options to configure printer selection, page orientation, margins, and other print parameters.

