如何在 ASP.NET Web 應用程式框架中列印
有時候,網路應用程式需要將文件列印為最終輸出。 然而,將列印功能整合到網頁應用程式中可能是一個現實世界的挑戰。 許多網路應用程式使用非同步函數,而同步印刷函數可能會引起問題。 但是,有一個解決方案! IronPrint 提供了 PrintAsync
函數,這是 Web 應用程式的重要工具。 在這個簡短的教程中,我們將演示PrintAsync
函數結合ASP. NET核心的強大功能。 這將向您展示如何模擬現實世界的網路應用程式,以列印文件作為最終輸出。
快速開始使用IronPrint
立即在您的專案中使用IronPrint,並享受免費試用。
立即在您的專案中使用IronPrint,並享受免費試用。
如何在 ASP.NET Web 應用程式框架中列印
- 下載用于網頁應用的 C# 打印庫
- 將IronPrint匯入類別檔案
- 添加一個列印按鈕以便在點擊後觸發該方法
- 在控制器中實現
PrintAsync
方法 - 當按下按鈕時,驗證文件已被列印
非同步 PDF 列印範例
此範例展示如何在 ASP.NET Web Application (.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
在異步和非異步函數中均可使用; 但是,在網路應用程式中使用標準的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