使用 C# 在 ASP.NET Framework Web 应用程序中进行打印

This article was translated from English: Does it need improvement?
Translated
View the article in English

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

快速入门:在 ASP.NET 中使用 IronPrint 进行异步 PDF 打印

这里提供一个简单的示例,展示了如何使用 IronPrint 的PrintAsync API——只需在控制器中编写一行代码即可启动打印,而不会导致应用程序卡顿。无需编写样板代码,即可快速上手。

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronPrint

    PM > Install-Package IronPrint

  2. 复制并运行这段代码。

    return await IronPrint.Printer.PrintAsync("Basic.pdf");
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronPrint,免费试用!
    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方法。 这种方法允许打印操作异步进行,从而提高应用程序的响应速度。

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();
        }

        // Action method to handle the printing operation
        // This makes use of the PrintAsync method to avoid blocking the main thread
        public ActionResult PrintPdf()
        {
            // Wait for the asynchronous print operation to complete
            Printer.PrintAsync("Basic.pdf").Wait();

            // Return some view, for example, a confirmation page or the index page
            return View(); // Replace with an appropriate 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();
        }

        // Action method to handle the printing operation
        // This makes use of the PrintAsync method to avoid blocking the main thread
        public ActionResult PrintPdf()
        {
            // Wait for the asynchronous print operation to complete
            Printer.PrintAsync("Basic.pdf").Wait();

            // Return some view, for example, a confirmation page or the index page
            return View(); // Replace with an appropriate 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

		' Action method to handle the printing operation
		' This makes use of the PrintAsync method to avoid blocking the main thread
		Public Function PrintPdf() As ActionResult
			' Wait for the asynchronous print operation to complete
			Printer.PrintAsync("Basic.pdf").Wait()

			' Return some view, for example, a confirmation page or the index page
			Return View() ' Replace with an appropriate view
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

常见问题解答

如何在 ASP.NET Web 应用程序中异步打印文档?

您可以使用 IronPrint 的 PrintAsync 方法在 ASP.NET Web 应用程序中异步打印文档。此方法允许在不阻塞应用程序主线程的情况下启动打印操作,从而保持响应性。

在 ASP.NET Web 应用程序中集成打印功能的步骤是什么?

要集成打印功能,您应从 NuGet 下载类似 IronPrint 的库,将其导入您的类文件,在 UI 中添加一个打印按钮,在控制器中实现 PrintAsync 方法,并通过测试打印操作验证功能。

PrintAsync 方法如何增强 Web 应用程序中的打印功能?

PrintAsync 方法通过允许异步进行操作来增强打印功能,这意味着在打印期间主应用程序线程不会被阻塞,从而提高了应用程序的响应性和用户体验。

在 Web 应用程序中使用异步功能有什么好处?

异步功能允许任务在不阻塞主应用程序线程的情况下执行,使其他操作能够继续顺畅运行,从而提升应用程序的整体响应性和性能。

如何在 ASP.NET 视图中添加打印按钮?

在您的 'Index.cshtml' 或主页视图中,您可以添加一个按钮,并通过 onclick 事件触发您控制器中的 ActionResult 方法,例如使用 location.href='@Url.Action("PrintPdf", "Home")' 进行打印。

Web 应用程序中的同步打印可能带来哪些挑战?

同步打印可能会阻塞主应用程序线程,导致响应性下降,并在打印操作完成前可能冻结应用程序界面。

如何验证在 ASP.NET 应用程序中文档已被打印?

在实现打印功能后,您应该通过在应用程序中按下打印按钮进行测试,验证文档是否按预期打印,以确保 PrintAsync 方法正常工作。

控制器中的 PrintPdf 动作方法有什么作用?

控制器中的 PrintPdf 动作方法使用 PrintAsync 方法启动打印操作,允许应用程序在不阻塞主线程的情况下处理打印任务,并在完成后返回视图。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 34,704 | Version: 2025.11 刚刚发布