如何在 ASP.NET Web 應用程式框架中列印
2025年4月15日
已更新 2025年4月15日
This article was translated from English: Does it need improvement?
Translated
View the article in English
有時候,網路應用程式需要將文件列印為最終輸出。 然而,將列印功能整合到網頁應用程式中可能是一個現實世界的挑戰。 許多網路應用程式使用非同步函數,而同步印刷函數可能會引起問題。 但是,有一個解決方案! IronPrint 提供了 PrintAsync
函數,這是 Web 應用程式的重要工具。 在這個簡短的教程中,我們將演示PrintAsync
函數結合ASP. NET核心的強大功能。 這將向您展示如何模擬現實世界的網路應用程式,以列印文件作為最終輸出。
快速開始使用IronPrint
立即在您的專案中使用IronPrint,並享受免費試用。
如何在 ASP.NET Web 應用程式框架中列印
- 下載用于網頁應用的 C# 打印庫
- 將IronPrint匯入類別檔案
- 添加一個列印按鈕以便在點擊後觸發該方法
- 在控制器中實現
PrintAsync
方法 - 當按下按鈕時,驗證文件已被列印
### 在控制器中實作 PrintAsync 在您的**HomeController**中,您將實作`PrintAsync`方法。 此方法允許列印操作異步進行,從而提高應用程式的響應能力。 [{i:(在此範例中,該函數非異步,而`PrintAsync`在異步和非異步函數中均可使用; 但是,在網路應用程式中使用標準的`Print`方法將不起作用。 ```cs 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(); } } } ```