How to Print in an ASP.NET Web Application Framework
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.
How to Print in an ASP.NET Web Application Framework
- Download a C# library for printing in web applications
- Import IronPrint into the class file
- Add a print button to trigger the method once clicked
- Implement the
PrintAsync
method in the controller - Verify that the document has been printed when the button is pressed
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>
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
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