C# を使用して ASP.NET Framework Webアプリで PRINT します
IronPrintのPrintAsyncメソッドは、ASP.NETウェブアプリケーションで非ブロッキングのドキュメント印刷を可能にし、印刷要求処理中のUIフリーズを防ぎます。 この非同期アプローチにより、レスポンシブなウェブアプリがスレッドをブロックすることなく印刷操作を処理できるようになります。
ウェブアプリケーションでは、最終出力としてドキュメントの印刷が必要になることがよくあります。 印刷機能をウェブアプリケーションに統合するには、特に非同期操作を扱う場合に課題があります。 IronPrintは、PrintAsync関数でこれを解決します。 このチュートリアルは、ASP.NET CoreとPrintAsyncを実装して、ドキュメントをブロックせずに印刷するウェブアプリケーションの作成を示しています。
実装の前に、IronPrintが包括的な機能を提供し、プリンタ情報の取得やカスタム印刷設定を含むことに注意してください。 これらの機能により、信頼性の高い印刷機能を必要とするエンタープライズASP.NETアプリケーションに最適です。
クイックスタート: ASP.NETでの非同期PDF印刷- IronPrintをNuGet経由でインストールしてください:
Install-Package IronPrint - コントローラファイルに
IronPrintをインポートしてください - メソッドをトリガーする印刷ボタンを追加します
- コントローラアクションで
await Printer.PrintAsync("file.pdf")を呼び出してください - ボタンを押してドキュメント印刷を確認します
-
1IronPrint を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
await IronPrint.Printer.PrintAsync("Basic.pdf");Await IronPrint.Printer.PrintAsync("Basic.pdf")Await IronPrint.Printer.PrintAsync("Basic.pdf") -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronPrintを使い始めるには、無料トライアルをお試しください。
IronPrintを始めましょう
無料トライアルで今日からプロジェクトにIronPrintを使用し始めましょう。
ASP.NET Webアプリケーションで印刷する方法
- NuGetからC#の印刷ライブラリをインストールします
- コントローラーに
IronPrintをインポートします - アクションをトリガーする印刷ボタンを追加します
- コントローラーメソッドで
PrintAsyncを呼び出します
ASP.NET で非同期 PDF PRINT を実装するには?
この例は、ASP.NET Web Application (.NET Framework)プロジェクトでPrintAsyncメソッドを使用してPDFファイルを非同期に印刷する方法を示しています。 PrintAsync は非同期に印刷を開始し、同期的な Print メソッドがスレッドをブロックするのに対し、アプリケーションを応答性のある状態に保ちます。
非同期のアプローチは、複数のユーザーが同時に印刷操作をトリガーする可能性のあるウェブアプリケーションでは重要です。 同期的な Print メソッドとは異なり、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>

コントローラーで 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 NamespaceImports 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高度なシナリオについては、適切な非同期/待機パターンを実装し、印刷操作をカスタマイズしてください。 ここでは、エラー処理とカスタム印刷設定の例を示します:
using IronPrint;
using System;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace WebApplication4.Controllers
{
public class HomeController : Controller
{
// Async action method with proper error handling
public async Task<ActionResult> PrintPdfAdvanced()
{
try
{
// Create custom print settings
var printSettings = new PrintSettings
{
// Select specific printer
PrinterName = "Microsoft Print to PDF",
// Set paper size
PaperSize = PaperSize.A4,
// Configure orientation
PaperOrientation = PaperOrientation.Portrait,
// Set number of copies
NumberOfCopies = 1,
// Configure DPI for high-quality output
Dpi = 300
};
// Print asynchronously with custom settings
await Printer.PrintAsync("Basic.pdf", printSettings);
// Log successful print operation (optional)
System.Diagnostics.Debug.WriteLine("Document printed successfully");
// Return success view or redirect
TempData["PrintMessage"] = "Document sent to printer successfully!";
return RedirectToAction("Index");
}
catch (Exception ex)
{
// Handle printing errors gracefully
System.Diagnostics.Debug.WriteLine($"Printing error: {ex.Message}");
TempData["ErrorMessage"] = "Unable to print document. Please try again.";
return RedirectToAction("Index");
}
}
}
}using IronPrint;
using System;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace WebApplication4.Controllers
{
public class HomeController : Controller
{
// Async action method with proper error handling
public async Task<ActionResult> PrintPdfAdvanced()
{
try
{
// Create custom print settings
var printSettings = new PrintSettings
{
// Select specific printer
PrinterName = "Microsoft Print to PDF",
// Set paper size
PaperSize = PaperSize.A4,
// Configure orientation
PaperOrientation = PaperOrientation.Portrait,
// Set number of copies
NumberOfCopies = 1,
// Configure DPI for high-quality output
Dpi = 300
};
// Print asynchronously with custom settings
await Printer.PrintAsync("Basic.pdf", printSettings);
// Log successful print operation (optional)
System.Diagnostics.Debug.WriteLine("Document printed successfully");
// Return success view or redirect
TempData["PrintMessage"] = "Document sent to printer successfully!";
return RedirectToAction("Index");
}
catch (Exception ex)
{
// Handle printing errors gracefully
System.Diagnostics.Debug.WriteLine($"Printing error: {ex.Message}");
TempData["ErrorMessage"] = "Unable to print document. Please try again.";
return RedirectToAction("Index");
}
}
}
}Imports IronPrint
Imports System
Imports System.Threading.Tasks
Imports System.Web.Mvc
Namespace WebApplication4.Controllers
Public Class HomeController
Inherits Controller
' Async action method with proper error handling
Public Async Function PrintPdfAdvanced() As Task(Of ActionResult)
Try
' Create custom print settings
Dim printSettings As New PrintSettings With {
' Select specific printer
.PrinterName = "Microsoft Print to PDF",
' Set paper size
.PaperSize = PaperSize.A4,
' Configure orientation
.PaperOrientation = PaperOrientation.Portrait,
' Set number of copies
.NumberOfCopies = 1,
' Configure DPI for high-quality output
.Dpi = 300
}
' Print asynchronously with custom settings
Await Printer.PrintAsync("Basic.pdf", printSettings)
' Log successful print operation (optional)
System.Diagnostics.Debug.WriteLine("Document printed successfully")
' Return success view or redirect
TempData("PrintMessage") = "Document sent to printer successfully!"
Return RedirectToAction("Index")
Catch ex As Exception
' Handle printing errors gracefully
System.Diagnostics.Debug.WriteLine($"Printing error: {ex.Message}")
TempData("ErrorMessage") = "Unable to print document. Please try again."
Return RedirectToAction("Index")
End Try
End Function
End Class
End NamespaceImports IronPrint
Imports System
Imports System.Threading.Tasks
Imports System.Web.Mvc
Namespace WebApplication4.Controllers
Public Class HomeController
Inherits Controller
' Async action method with proper error handling
Public Async Function PrintPdfAdvanced() As Task(Of ActionResult)
Try
' Create custom print settings
Dim printSettings As New PrintSettings With {
' Select specific printer
.PrinterName = "Microsoft Print to PDF",
' Set paper size
.PaperSize = PaperSize.A4,
' Configure orientation
.PaperOrientation = PaperOrientation.Portrait,
' Set number of copies
.NumberOfCopies = 1,
' Configure DPI for high-quality output
.Dpi = 300
}
' Print asynchronously with custom settings
Await Printer.PrintAsync("Basic.pdf", printSettings)
' Log successful print operation (optional)
System.Diagnostics.Debug.WriteLine("Document printed successfully")
' Return success view or redirect
TempData("PrintMessage") = "Document sent to printer successfully!"
Return RedirectToAction("Index")
Catch ex As Exception
' Handle printing errors gracefully
System.Diagnostics.Debug.WriteLine($"Printing error: {ex.Message}")
TempData("ErrorMessage") = "Unable to print document. Please try again."
Return RedirectToAction("Index")
End Try
End Function
End Class
End Namespaceこの強化された実装は、印刷設定ガイドの概念を示しており、プリンタ名の指定、用紙サイズの設定、およびエラーの適切な処理を含みます。
ウェブ環境でのプリンタ選択時には、Get Printer Names機能を活用して利用可能なプリンタリストを動的に充実させてください:
// Get list of available printers
public ActionResult GetAvailablePrinters()
{
var printers = Printer.GetPrinterNames();
ViewBag.PrinterList = new SelectList(printers);
return View();
}// Get list of available printers
public ActionResult GetAvailablePrinters()
{
var printers = Printer.GetPrinterNames();
ViewBag.PrinterList = new SelectList(printers);
return View();
}' Get list of available printers
Public Function GetAvailablePrinters() As ActionResult
Dim printers = Printer.GetPrinterNames()
ViewBag.PrinterList = New SelectList(printers)
Return View()
End Function' Get list of available printers
Public Function GetAvailablePrinters() As ActionResult
Dim printers = Printer.GetPrinterNames()
ViewBag.PrinterList = New SelectList(printers)
Return View()
End Functionユーザーの対話を必要とするシナリオでは、Dialogを用いた印刷アプローチの実装を検討してください。ただし、これはウェブ環境よりもデスクトップアプリケーションに適しています。
本番展開における追加の考慮事項
ASP.NETアプリケーションをIronPrintでデプロイする際には、これらの要素を考慮してください:
- ライセンス設定: ASP.NETアプリケーションでは、Web.configにライセンスキーを設定してください。適切な設定方法はWeb.configでのライセンスキー設定ガイドを参照してください。
- プリンタアクセス: アプリケーションプールの識別情報がローカルまたはネットワークプリンタにアクセスできる権限があることを確認してください。 ドキュメントを印刷するドキュメントには、プリンタアクセス要件が記載されています。
- エラー処理: オフラインプリンタやアクセス不可能なドキュメントの信頼性のあるエラー処理を実装してください。 技術的な問題が発生した場合、エンジニアリングリクエストプロセスを使用して複雑な問題を解決してください。
- パフォーマンス: 大量印刷の場合、キューシステムを実装して効率的に印刷要求を管理してください。 非同期
PrintAsyncは、こうした実装に理想的です。
包括的な印刷機能については、IronPrint名前空間のすべてのメソッドおよびプロパティの詳細ドキュメントについてAPIリファレンスを参照してください。
よくある質問
IronPrintのPrintAsyncメソッドをASP.NETウェブアプリケーションで使用する主な利点は何ですか?
IronPrintのPrintAsyncメソッドは、非ブロッキングの書類印刷を可能にし、ASP.NETウェブアプリケーションが印刷リクエストを処理する間も応答性を維持します。
IronPrintをASP.NETウェブアプリケーションに統合するにはどうすればいいですか?
IronPrintを統合するには、NuGet経由でライブラリをインストールし、コントローラーファイルにインポートし、印刷アクションをトリガーするための印刷ボタンを追加し、非同期印刷のためにPrintAsyncメソッドを使用します。
ウェブアプリケーションで非同期印刷を選ぶべき理由は何ですか?
IronPrintのPrintAsyncによる非同期印刷はUIフリーズを防ぎ、複数の印刷リクエストを同時に処理できるため、ウェブアプリケーションの応答性が向上します。
IronPrintを使ったカスタム印刷設定をどのように設定できますか?
PrintSettingsのインスタンスを作成し、プリンタ名、用紙サイズ、方向、部数、DPIなどのパラメータを指定し、それをPrintAsyncメソッドに渡すことでカスタム印刷設定を構成できます。
ASP.NETウェブアプリケーションのどこに印刷ボタンを追加しますか?
'Index.cshtml'やホームページビューに印刷ボタンを追加します。このボタンは、印刷をトリガーするためにコントローラー内のActionResultメソッドを呼び出す必要があります。
印刷プロセス中のエラーをどのように処理できますか?
try-catchブロックをPrintAsync呼び出しの周りに使用して例外をキャッチし、ユーザーフィードバックの提供や必要に応じたログ記録で優雅に処理します。
IronPrintを使用したASP.NETアプリケーションのデプロイで考慮すべき点は何ですか?
本番環境でのアプリケーションのライセンス構成、プリンタアクセス権限、エラーハンドリング機構が適切に設定されていることを確認してください。
アプリケーションで利用可能なプリンタリストを動的に取得するにはどうすればいいですか?
IronPrintのGetPrinterNamesメソッドを使用して、利用可能なプリンタのリストを取得し、ユーザーに動的に表示します。
高ボリューム印刷を管理する上でのPrintAsyncメソッドの役割は何ですか?
PrintAsyncは、キューシステムを有効にしてアプリケーションをブロックすることなく効率的に印刷リクエストを管理することで、高ボリューム印刷をサポートします。
ウェブ環境でダイアログ付きの印刷にIronPrintを使用することは可能ですか?
デスクトップアプリケーションに印刷ダイアログを統合することは可能ですが、ウェブ環境では非同期メソッドが最適なパフォーマンスに推奨されるため、このアプローチは適切ではありません。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。