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

時には、ウェブアプリケーションが最終出力としてドキュメントを印刷する必要があることがあります。 しかし、印刷機能をウェブアプリケーションと統合することは、現実の課題となり得ます。 多くの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に深い専門知識を持ち、ソフトウェアの改善と顧客サポートを支援しています。ユーザーとの対話から得た彼の洞察は、より良い製品、文書、および全体的な体験に貢献しています。