ASP.NET Framework 웹 앱에서 C#으로 인쇄하기
IronPrint의 PrintAsync 메소드는 ASP.NET 웹 애플리케이션에서 비동기 문서 인쇄를 가능하게 하여 인쇄 요청을 처리하는 동안 UI가 멈추는 것을 방지합니다. 이러한 비동기 방식은 반응형 웹 앱이 스레드를 차단하지 않고 인쇄 작업을 처리할 수 있도록 보장합니다.
웹 애플리케이션은 최종 출력물로 문서 인쇄가 필요한 경우가 많습니다. 웹 애플리케이션에 인쇄 기능을 통합하는 것은 특히 비동기 작업을 처리할 때 여러 가지 어려움을 수반합니다. IronPrint는 이 문제를 PrintAsync 함수로 해결합니다. 이 튜토리얼은 ASP.NET Core와 함께 PrintAsync 를 구현하여 문서를 차단 없이 인쇄하는 웹 애플리케이션을 만드는 방법을 보여줍니다.
구현 전에 IronPrint가 기능을 포함한 광범위한 프린터 정보 검색 및 맞춤 인쇄 설정을 제공한다는 점을 유의하세요. 이 기능들은 신뢰할 수 있는 인쇄 기능이 필요한 Enterprise ASP.NET 애플리케이션에 이상적입니다.
빠른 시작: ASP.NET에서 비동기 PDF 인쇄- NuGet을 통해 IronPrint 설치:
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 웹 애플리케이션에서 인쇄하는 방법
- NuGet에서 C# 인쇄 라이브러리 설치
- 컨트롤러에
IronPrint가져오기 - 작업을 실행할 인쇄 버튼 추가
- 컨트롤러 메서드에서
PrintAsync를 호출하십시오
ASP.NET 에서 비동기 PDF 인쇄를 구현하는 방법은 무엇인가요?
이 예제는 PrintAsync 메소드를 사용하여 ASP.NET Web Application (.NET Framework) 프로젝트에서 비동기적으로 PDF 파일을 인쇄하는 방법을 보여줍니다. PrintAsync 은 비동기적으로 인쇄를 시작하여 동기 Print 메소드로 인해 스레드가 차단되는 상황과 비교하여 애플리케이션이 반응성을 유지하도록 합니다.
비동기 방식은 여러 사용자가 동시에 인쇄 작업을 실행할 수 있는 웹 애플리케이션에서 매우 중요합니다. 동기 Print 메소드와 달리, PrintAsync 은 성능 저하 없이 애플리케이션이 동시 요청을 처리할 수 있도록 합니다.
인쇄 버튼은 어디에 추가해야 할까요?
"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고급 시나리오의 경우, 적절한 async/await 패턴을 구현하고 출력 작업을 사용자 정의하십시오. 다음은 오류 처리 및 사용자 지정 인쇄 설정을 보여주는 향상된 예제입니다.
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 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사용자 상호작용이 필요한 시나리오에서는 다이얼로그와 함께 인쇄 접근 방식을 고려하세요. 이는 웹 환경보다는 데스크톱 애플리케이션에 적합합니다.
실제 운영 환경 배포 시 추가 고려 사항
IronPrint 사용하여 ASP.NET 애플리케이션을 배포할 때 다음 요소를 고려하십시오.
- 라이선스 구성: ASP.NET 애플리케이션의 경우, Web.config에서 라이선스 키를 구성하세요. 적절한 설정은 Web.config에서 라이선스 키 설정 가이드를 참조하세요.
- 프린터 접근: 애플리케이션 풀 ID가 로컬 또는 네트워크 프린터에 접근할 수 있는 권한이 있는지 확인하세요. 문서 인쇄 문서는 프린터 접근 요구사항을 제공합니다.
- 오류 처리: 오프라인 프린터 또는 접근할 수 없는 문서에 대한 신뢰할 수 있는 오류 처리를 구현하세요. 기술적인 문제의 경우, 복잡한 문제를 해결하기 위해 엔지니어링 요청 프로세스를 사용하세요.
- 성능: 고량 인쇄의 경우, 인쇄 요청을 효율적으로 관리하기 위해 큐 시스템을 구현하세요. 비동기
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 메서드를 호출하여 인쇄를 트리거해야 합니다.
인쇄 과정에서 발생하는 오류를 어떻게 처리할 수 있나요?
PrintAsync 호출 주위에 try-catch 블록을 사용하여 예외를 잡고 이를 적절히 처리하여 사용자 피드백을 제공하거나 로그를 기록할 수 있습니다.
IronPrint를 사용하는 ASP.NET 애플리케이션을 배포할 때 무엇을 고려해야 하나요?
프로덕션 환경에서 애플리케이션의 라이선스 설정, 프린터 접근 권한, 오류 처리 메커니즘이 올바르게 설정되었는지 확인하십시오.
애플리케이션에서 사용 가능한 프린터 목록을 동적으로 채워넣는 방법은 무엇인가요?
IronPrint의 GetPrinterNames 메서드를 사용하여 사용자가 볼 수 있도록 사용 가능한 프린터 목록을 동적으로 표시할 수 있습니다.
대량 인쇄를 관리하는 데 있어 PrintAsync 메서드의 역할은 무엇인가요?
PrintAsync는 대량 인쇄를 지원하여, 인쇄 요청을 효율적으로 관리할 수 있는 대기열 시스템을 제공하여 애플리케이션을 차단하지 않고 작업을 처리합니다.
웹 환경에서 대화 상자를 사용하여 IronPrint로 인쇄하는 것이 가능합니까?
데스크톱 애플리케이션에서 인쇄 대화 상자를 통합하는 것은 가능하지만, 웹 환경에서는 비동기 메서드가 최상의 성능을 위해 선호됩니다.
커티스 차우는 칼턴 대학교에서 컴퓨터 과학 학사 학위를 취득했으며, Node.js, TypeScript, JavaScript, React를 전문으로 하는 프론트엔드 개발자입니다. 직관적이고 미적으로 뛰어난 사용자 인터페이스를 만드는 데 열정을 가진 그는 최신 프레임워크를 활용하고, 잘 구성되고 시각적으로 매력적인 매뉴얼을 제작하는 것을 즐깁니다.