Comment imprimer dans un framework d'application Web ASP.NET

Chaknith Bin
Chaknith Bin
mars 31, 2025
Mise à jour mars 31, 2025
Partager:
This article was translated from English: Does it need improvement?
Translated
View the article in English

Parfois, les applications web doivent imprimer un document comme résultat final. Cependant, intégrer la fonction d'impression avec une application web peut être un défi concret. De nombreuses applications web utilisent des fonctions asynchrones, et une fonction d'impression synchrone pourrait potentiellement causer des problèmes. Mais, il y a une solution ! IronPrint propose la fonction PrintAsync, un outil crucial pour les applications web. Dans ce bref tutoriel, nous allons démontrer la puissance de la fonction PrintAsync combinée avec ASP. NET Core. Cela vous montrera comment simuler une application web du monde réel qui imprime un document comme résultat final.

Démarrer avec IronPrint

Commencez à utiliser IronPrint dans votre projet dès aujourd'hui avec un essai gratuit.

Première étape :
green arrow pointer

Exemple d'impression PDF asynchrone

Cet exemple démontre comment imprimer un fichier PDF de manière asynchrone dans un projet d'application Web ASP.NET (.NET Framework) en utilisant la méthode PrintAsync. En utilisant PrintAsync, l'opération d'impression est initiée de manière asynchrone, permettant à l'application de rester réactive, contrairement au blocage du thread avec les méthodes Print synchrones traditionnelles.

Ajouter un bouton d'impression

Dans votre "Index.cshtml" (ou vue de la page d'accueil), ajoutez un bouton qui déclenche une action lorsque l'on clique dessus. Ce bouton appellera une méthode ActionResult dans votre contrôleur. Voici comment vous pouvez l'implémenter :

@{
    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

Page d'index


Implémenter PrintAsync dans le contrôleur

Dans votre HomeController, vous allez implémenter la méthode PrintAsync. Cette méthode permet à l'opération d'impression de se produire de manière asynchrone, améliorant ainsi la réactivité de l'application.

A noter
Dans cet exemple, la fonction n'est pas asynchrone, et PrintAsync fonctionne à la fois dans des fonctions asynchrones et synchrones; cependant, l'utilisation de la méthode standard Print dans une application web ne fonctionnerait pas.

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 Bin
Ingénieur logiciel
Chaknith travaille sur IronXL et IronBarcode. Il possède une expertise approfondie en C# et .NET, aidant à améliorer le logiciel et à soutenir les clients. Ses idées issues des interactions avec les utilisateurs contribuent à de meilleurs produits, une documentation améliorée et une expérience globale enrichie.