How to Print in an ASP.NET Web Application Framework

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

時々、ウェブアプリケーションは最終出力としてドキュメントを印刷する必要があります。 しかし、印刷機能をウェブアプリケーションと統合することは、実際の課題となることがあります。 多くのウェブアプリケーションは非同期関数を使用しており、同期印刷機能が問題を引き起こす可能性があります。 しかし、解決策があります! IronPrintは、ウェブアプリケーションにとって重要なツールであるPrintAsync関数を提供します。 この短いチュートリアルでは、ASP.NET Coreと組み合わせたPrintAsync機能の力を示します。 これにより、ドキュメントを最終出力として印刷する実際のウェブアプリケーションをシミュレートする方法を示します。

クイックスタート:IronPrintによるASP.NETでの非同期PDF印刷

IronPrintのPrintAsync APIを使用する簡単な例を示します。コントローラーで1行書くだけで印刷を開始し、アプリをフリーズさせることなく始めることができます。定型コードなしですぐに始めてください。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPrint with NuGet Package Manager

    PM > Install-Package IronPrint

  2. Copy and run this code snippet.

    return await IronPrint.Printer.PrintAsync("Basic.pdf");
  3. Deploy to test on your live environment

    Start using IronPrint in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小ワークフロー(5ステップ)

  1. Webアプリケーションで印刷するためのC#ライブラリをダウンロード
  2. クラスファイルにIronPrintをインポート
  3. メソッドを呼び出すための印刷ボタンを追加
  4. コントローラーでPrintAsyncメソッドを実装
  5. ボタンを押すときにドキュメントが印刷されたことを確認

非同期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

Indexページ


コントローラーでPrintAsyncを実装

HomeControllerPrintAsyncメソッドを実装します。 このメソッドは印刷操作を非同期で行うことを可能にし、アプリケーションの応答性を向上させます。

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
テクニカルライター

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

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

準備はいいですか?
Nuget ダウンロード 34,016 | バージョン: 2025.11 ただ今リリースされました