如何在ASP.NET Web应用程序框架中打印

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

有时,Web 应用程序需要将文档打印为最终输出。 然而,将打印功能集成到 Web 应用程序中可能是一项现实世界的挑战。 许多网络应用程序使用异步函数,而同步打印功能可能会导致问题。 但是,有一个解决方案! IronPrint 提供了 PrintAsync 函数,这是网络应用程序的重要工具。 在本简短教程中,我们将演示PrintAsync函数与ASP.NET Core结合的强大功能。 这将向您展示如何模拟一个以打印文档为最终输出的真实世界 Web 应用程序。

开始使用 IronPrint

立即在您的项目中开始使用IronPrint,并享受免费试用。

第一步:
green arrow pointer

异步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>
HTML

索引页


在控制器中实现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
$vbLabelText   $csharpLabel
查克尼特·宾
软件工程师
Chaknith 负责 IronXL 和 IronBarcode 的工作。他在 C# 和 .NET 方面拥有深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的洞察力,有助于提升产品、文档和整体体验。