C# を使用して ASP.NET Framework Web アプリで印刷する

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

場合によっては、Web アプリケーションで最終出力としてドキュメントを印刷する必要があります。 ただし、印刷機能を Web アプリケーションに統合することは、現実的には困難な場合があります。 多くの Web アプリケーションは非同期関数を使用しており、同期印刷関数は問題を引き起こす可能性があります。 しかし、解決策はあります! IronPrint は、Web アプリケーションにとって重要なツールであるPrintAsync機能を提供します。 この簡単なチュートリアルでは、ASP.NET Core と組み合わせたPrintAsync関数の威力を紹介します。 ここでは、最終出力としてドキュメントを印刷する実際の Web アプリケーションをシミュレートする方法を説明します。

クイックスタート: ASP.NET での IronPrint を使用した非同期 PDF 印刷

IronPrintのPrintAsync APIの使い方がいかに簡単かを示す最小限の例をご紹介します。コントローラーにたった1行追加するだけで、アプリをフリーズさせることなく印刷を開始できます。定型的なコードを書くことなく、すぐに使い始めることができます。

Nuget Icon今すぐ NuGet で PDF を作成してみましょう:

  1. NuGet パッケージ マネージャーを使用して IronPrint をインストールします

    PM > Install-Package IronPrint

  2. このコード スニペットをコピーして実行します。

    return await IronPrint.Printer.PrintAsync("Basic.pdf");
  3. 実際の環境でテストするためにデプロイする

    今すぐ無料トライアルでプロジェクトに IronPrint を使い始めましょう
    arrow pointer

非同期PDF印刷の例

この例では、 PrintAsyncメソッドを使用して、ASP.NET Web アプリケーション (.NET Framework)プロジェクトで 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ウェブアプリケーションでドキュメントを非同期に印刷するにはどうすればいいですか?

IronPrintのPrintAsyncメソッドを使用して、ASP.NETウェブアプリケーションでドキュメントを非同期に印刷できます。このメソッドを使うことで、印刷操作がアプリケーションのメインスレッドをブロックすることなく開始され、応答性が保持されます。

ASP.NETウェブアプリケーションに印刷機能を統合する手順は何ですか?

印刷機能を統合するには、NuGetからIronPrintのようなライブラリをダウンロードし、クラスファイルにインポートし、UIに印刷ボタンを追加し、コントローラーでPrintAsyncメソッドを実装し、印刷操作をテストして機能を確認する必要があります。

PrintAsyncメソッドはウェブアプリケーションでの印刷をどのように向上させますか?

PrintAsyncメソッドは、操作を非同期に発生させることで、印刷中にメインアプリケーションスレッドがブロックされないため、アプリケーションの応答性とユーザーエクスペリエンスを向上させます。

ウェブアプリケーションで非同期関数を使用する利点は何ですか?

非同期関数は、メインアプリケーションスレッドをブロックすることなくタスクを実行できるようにし、他の操作がスムーズに続けられ、アプリケーション全体の応答性とパフォーマンスを向上させます。

ASP.NETビューに印刷ボタンを追加するにはどうすればいいですか?

'Index.cshtml'またはホームページビューにボタンを追加し、onclickイベントをトリガーして、コントローラー内のActionResultメソッドを実行し、たとえばlocation.href='@Url.Action("PrintPdf", "Home")'を使用して印刷を開始します。

ウェブアプリケーションで同期印刷からどのような問題が生じる可能性がありますか?

同期印刷はメインアプリケーションスレッドをブロックし、応答性を低下させ、印刷操作が完了するまでアプリケーションインターフェースがフリーズする可能性があります。

ASP.NETアプリケーションでドキュメントが印刷されたことを確認するにはどうすればいいですか?

印刷機能を実装した後、アプリケーション内で印刷ボタンを押して、ドキュメントが期待通りに印刷されるか確認し、PrintAsyncメソッドが正しく機能していることを確認します。

PrintPdfアクションメソッドの役割は何ですか?

コントローラー内のPrintPdfアクションメソッドはPrintAsyncメソッドを使用して印刷操作を開始し、メインスレッドをブロックすることなく印刷ジョブを処理し、最終的に完了時にビューを返します。

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 34,704 | Version: 2025.11 リリース