ASP.NET Webアプリケーションフレームワークで印刷する方法
時には、ウェブアプリケーションが最終出力としてドキュメントを印刷する必要があることがあります。 しかし、印刷機能をウェブアプリケーションと統合することは、現実の課題となり得ます。 多くのWebアプリケーションは非同期関数を使用しており、同期印刷関数は潜在的に問題を引き起こす可能性があります。 しかし、解決策があります! IronPrintは、ウェブアプリケーションにとって重要なツールであるPrintAsync
関数を提供します。 この短いチュートリアルでは、PrintAsync
関数とASP.NET Coreを組み合わせた力を示します。 これは、ドキュメントを最終出力として印刷する実際のWebアプリケーションをシミュレートする方法を示します。
IronPrintを使い始める
今日から無料トライアルでIronPrintをあなたのプロジェクトで使い始めましょう。
今日から無料トライアルでIronPrintをあなたのプロジェクトで使い始めましょう。
ASP.NET Webアプリケーションフレームワークで印刷する方法
- Webアプリケーションでの印刷用C#ライブラリをダウンロード
- クラスファイルにIronPrintをインポートする
- クリックするとメソッドを実行するプリントボタンを追加する
- コントローラーに
PrintAsync
メソッドを実装する - ボタンを押したときにドキュメントが印刷されたことを確認します
非同期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>
コントローラーで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