如何在 ASP.NET Web 應用程式框架中列印

查克尼思·賓
查克尼思·賓
2025年3月31日
已更新 2025年3月31日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

有時候,網路應用程式需要將文件列印為最終輸出。 然而,將列印功能整合到網頁應用程式中可能是一個現實世界的挑戰。 許多網路應用程式使用非同步函數,而同步印刷函數可能會引起問題。 但是,有一個解決方案! IronPrint 提供了 PrintAsync 函數,這是 Web 應用程式的重要工具。 在這個簡短的教程中,我們將演示PrintAsync函數結合ASP. NET核心的強大功能。 這將向您展示如何模擬現實世界的網路應用程式,以列印文件作為最終輸出。

快速開始使用IronPrint

立即在您的專案中使用IronPrint,並享受免費試用。

第一步:
green arrow pointer

非同步 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>
HTML

索引頁面


在控制器中實作 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
$vbLabelText   $csharpLabel
查克尼思·賓
軟體工程師
Chaknith 致力於 IronXL 和 IronBarcode。他在 C# 和 .NET 方面擁有豐富的專業知識,協助改進軟體並支持客戶。他從用戶互動中獲得的洞察力有助於提高產品、文檔和整體體驗。