Updated March 26, 2025
Share:

How to Print in an ASP.NET Web Application Framework

by Curtis

Sometimes, web applications need to print a document as the final output. However, integrating the print function with a web application can be a real-world challenge. Many web applications use asynchronous functions, and a synchronous print function could potentially cause issues. But, there's a solution! IronPrint offers the PrintAsync function, a crucial tool for web applications. In this brief tutorial, we'll demonstrate the power of the PrintAsync function combined with ASP. NET core. This will show you how to simulate a real-world web application that prints a document as the final output.

Get started with IronPrint

Start using IronPrint in your project today with a free trial.

First Step:
green arrow pointer


Asynchronous PDF Printing Example

This example demonstrates how to print a PDF file asynchronously in an ASP.NET Web Application (.NET Framework) project using the PrintAsync method. By using PrintAsync, the print operation is initiated asynchronously, allowing the application to remain responsive, as opposed to blocking the thread with traditional synchronous Print methods.

Add a Print Button

In your "Index.cshtml" (or home page view), add a button that triggers an action when clicked. This button will invoke an ActionResult method in your controller. Here’s how you can implement it:

@{
    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>
@{
    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>
HTML

Index page


Implement PrintAsync in the Controller

In your HomeController, you’ll implement the PrintAsync method. This method allows the print operation to occur asynchronously, enhancing the responsiveness of the application.

Please note
In this example, the function is not asynchronous, and PrintAsync works in both asynchronous and asynchronous functions; however, using the standard Print method in a web application would not work.

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();
        }

        public ActionResult PrintPdf()
        {
            // Your printing logic here
            Printer.PrintAsync("Basic.pdf").Wait();

            return 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();
        }

        public ActionResult PrintPdf()
        {
            // Your printing logic here
            Printer.PrintAsync("Basic.pdf").Wait();

            return 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

		Public Function PrintPdf() As ActionResult
			' Your printing logic here
			Printer.PrintAsync("Basic.pdf").Wait()

			Return View()
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel
Chaknith Bin

Chaknith Bin

Software Engineer

 LinkedIn

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.