如何在ASP.NET Web应用程序框架中打印
有时,Web 应用程序需要将文档打印为最终输出。 然而,将打印功能集成到 Web 应用程序中可能是一项现实世界的挑战。 许多网络应用程序使用异步函数,而同步打印功能可能会导致问题。 但是,有一个解决方案! IronPrint 提供了 PrintAsync
函数,这是网络应用程序的重要工具。 在本简短教程中,我们将演示PrintAsync
函数与ASP.NET Core结合的强大功能。 这将向您展示如何模拟一个以打印文档为最终输出的真实世界 Web 应用程序。
开始使用 IronPrint
立即在您的项目中开始使用IronPrint,并享受免费试用。
立即在您的项目中开始使用IronPrint,并享受免费试用。
如何在ASP.NET Web应用程序框架中打印
- 下载用于网页应用程序打印的 C# 库
- 将IronPrint导入到类文件中
- 添加打印按钮以在单击时触发方法
- 在控制器中实现
PrintAsync
方法 - 验证按下按钮时文档已打印
异步PDF打印示例
此示例演示如何在ASP.NET Web 应用程序 (.NET Framework) 项目中使用PrintAsync
方法异步打印 PDF 文件。 通过使用PrintAsync
,打印操作是异步启动的,这允许应用程序保持响应,而不是像传统同步Print
方法那样阻塞线程。
添加打印按钮
在您的“Index.cshtml”(或主页视图)中,添加一个按钮,单击时触发操作。 这个按钮将在您的控制器中调用一个ActionResult
方法。 以下是实现它的方法:
@{
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>
在控制器中实现PrintAsync
在您的HomeController中,您将实现PrintAsync
方法。 该方法允许打印操作异步进行,提高了应用程序的响应能力。
[{i:(在此示例中,该函数不是异步的,PrintAsync
在异步和同步函数中都能工作; 然而,在 Web 应用程序中使用标准的 Print
方法是行不通的。
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