IronPrint의 PrintSettings 클래스는 인쇄 작업을 특정 프린터로 지시하는 PrinterName 속성을 노출합니다. 대상 프린터의 정확한 이름을 문자열로 지정하고 설정된 PrintSettings 객체를 IronPrint의 인쇄 메소드에 전달하면 문서는 시스템 기본 프린터 대신 해당 프린터로 전송됩니다.
이 가이드는 프린터 이름 설정, 실행 시 사용 가능한 프린터 검색, 다른 인쇄 설정과의 프린터 선택 결합 방법을 안내합니다.
빠른 시작: 프린터 이름 지정
NuGet을 통해 IronPrint 설치: Install-Package IronPrint
파일에 using IronPrint; 추가
PrintSettings 객체 생성
대상 프린터의 정확한 이름으로 PrinterName 설정
설정을 Printer.Print() 또는 Printer.PrintAsync()에 전달
NuGet 패키지 관리자를 사용하여 https://www.nuget.org/packages/IronPrint 설치하기
PM > Install-Package IronPrint
다음 코드 조각을 복사하여 실행하세요.
using IronPrint;
// Print a document to a specific printer
Printer.Print("report.pdf", new PrintSettings
{
PrinterName = "HP LaserJet Pro M404"
});
using IronPrint;
// Configure print settings with a target printer
PrintSettings settings = new PrintSettings();
settings.PrinterName = "Microsoft Print to PDF";
// Send the document to the specified printer
Printer.Print("invoice.pdf", settings);
Imports IronPrint
' Configure print settings with a target printer
Dim settings As New PrintSettings()
settings.PrinterName = "Microsoft Print to PDF"
' Send the document to the specified printer
Printer.Print("invoice.pdf", settings)
$vbLabelText $csharpLabel
우리는 먼저 PrintSettings를 인스턴스화하고 PrinterName = null로 초기화하는데 — 이는 OS 기본 프린터를 의미합니다. 그런 다음 대상 프린터의 정확한 문자열 이름으로 PrinterName을 오버라이드합니다. Printer.Print을 호출하면 IronPrint는 해당 프린터의 대기열로 작업을 직접 보냅니다.
주의할 두 가지 중요한 세부 사항. 첫째, 프린터 이름은 운영 체제에서 보고하는 것과 정확히 일치해야 합니다 — 이 비교는 대소문자를 구분합니다. "hp laserjet" 대신 "HP LaserJet"과 같은 불일치는 조용히 실패하거나 오류를 발생시킵니다. 둘째, 사용자가 ShowPrintDialog를 통해 인쇄 대화 상자를 열면, 대화 상자 선택이 코드에서 설정된 PrinterName을 무시합니다. 이는 설계에 따른 것입니다. 대화 상자는 사용자에게 최종 통제권을 부여합니다.
사용 가능한 프린터를 어떻게 발견합니까?
프린터 이름을 하드코딩하는 대신, Printer.GetPrinterNames()를 사용하여 런타임에 시스템을 쿼리할 수 있습니다. 이 메소드는 컴퓨터에 설치된 모든 프린터를 포함하는 List<string>를 반환합니다.
using IronPrint;
// Discover all installed printers
List<string> printers = Printer.GetPrinterNames();
foreach (string name in printers)
{
Console.WriteLine(name);
}
// Use the first available printer
if (printers.Count > 0)
{
Printer.Print("report.pdf", new PrintSettings
{
PrinterName = printers[0]
});
}
Imports IronPrint
' Discover all installed printers
Dim printers As List(Of String) = Printer.GetPrinterNames()
For Each name As String In printers
Console.WriteLine(name)
Next
' Use the first available printer
If printers.Count > 0 Then
Printer.Print("report.pdf", New PrintSettings With {
.PrinterName = printers(0)
})
End If
$vbLabelText $csharpLabel
우리는 GetPrinterNames()를 호출하여 운영 체제가 알고 있는 모든 프린터를 검색합니다 — 로컬, 네트워크 및 'Microsoft Print to PDF'와 같은 가상 프린터를 포함합니다. 그런 다음 목록을 반복하여 인덱스, 이름 매칭 또는 애플리케이션이 요구하는 모든 사용자 정의 논리에 따라 프린터를 선택합니다.
이 발견 후 인쇄 패턴은 다른 기기에 배포되는 응용 프로그램에 필수적입니다. 프린터 이름을 하드코딩하는 것은 단일 컴퓨터 시나리오에 적합하지만, 프로덕션 응용 프로그램에서는 사용할 수 있는 프린터를 쿼리하여 사용자가 선택하거나 이름 규칙에 따라 프로그래밍적으로 하나를 선택해야 합니다. 전용 코드 예제는 프린터 이름 가져오기 예제를 참조하세요.
프린터 이름과 다른 설정을 어떻게 결합합니까?
PrintSettings 클래스는 용지 크기, 방향, DPI, 여백, 복사 수 및 평면화를 위한 속성들과 함께 PrinterName을 노출합니다. 우리는 모든 것을 하나의 객체로 구성합니다.
using IronPrint;
// Build a fully configured print job targeting a specific printer
PrintSettings settings = new PrintSettings
{
PrinterName = "Office Color Printer",
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 2,
PaperMargins = new Margins(15, 15, 15, 15),
Grayscale = false
};
// Print a branded report to the color printer
Printer.Print("quarterly-report.pdf", settings);
Imports IronPrint
' Build a fully configured print job targeting a specific printer
Dim settings As New PrintSettings With {
.PrinterName = "Office Color Printer",
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 2,
.PaperMargins = New Margins(15, 15, 15, 15),
.Grayscale = False
}
' Print a branded report to the color printer
Printer.Print("quarterly-report.pdf", settings)
$vbLabelText $csharpLabel
가독성을 위해 객체 초기화 구문을 사용합니다. PrinterName는 작업을 "Office Color Printer"로 라우팅하고 나머지 속성은 출력 형식을 제어합니다. 300의 Dpi은 선명한 텍스트와 그래픽을 생성합니다. PaperMargins는 Margins 생성자를 통해 네 개의 밀리미터 값을 수용합니다 — 위, 오른쪽, 아래, 왼쪽.
IronPrint는 구성 전체를 검증하고 단일 작업에서 결합된 설정을 프린터 드라이버에 제출합니다. 트레이 선택 및 그레이스케일 모드와 같은 추가 옵션은 전체 인쇄 설정 가이드를 참조하십시오.
using IronPrint;
using System.Threading.Tasks;
public class PrintService
{
public async Task PrintToFirstAvailableAsync(string filePath)
{
// Discover printers without blocking the UI
List<string> printers = await Printer.GetPrinterNamesAsync();
if (printers.Count == 0)
{
Console.WriteLine("No printers found.");
return;
}
// Configure and print to the first available printer
PrintSettings settings = new PrintSettings
{
PrinterName = printers[0],
Dpi = 300
};
await Printer.PrintAsync(filePath, settings);
}
}
Imports IronPrint
Imports System.Threading.Tasks
Public Class PrintService
Public Async Function PrintToFirstAvailableAsync(filePath As String) As Task
' Discover printers without blocking the UI
Dim printers As List(Of String) = Await Printer.GetPrinterNamesAsync()
If printers.Count = 0 Then
Console.WriteLine("No printers found.")
Return
End If
' Configure and print to the first available printer
Dim settings As New PrintSettings With {
.PrinterName = printers(0),
.Dpi = 300
}
Await Printer.PrintAsync(filePath, settings)
End Function
End Class
$vbLabelText $csharpLabel
이 클래스 기반 예제는 발견 및 인쇄 논리를 재사용 가능한 서비스로 래핑합니다. 우리는 GetPrinterNamesAsync()을 호출하여 UI를 멈추지 않고 프린터 목록을 검색한 다음 첫 번째 사용 가능한 프린터를 PrinterName에 할당합니다. await Printer.PrintAsync 호출은 작업을 비동기적으로 보냅니다.
프로덕션에서는 우리는 아마도 printers[0]을 명명 규칙에 맞는 논리로 대체할 것입니다 — 예를 들어 'Label'이 포함된 프린터를 찾는 것 또는 브랜드 문서를 위한 'Color'을 찾는 것처럼. IronPrint의 비동기 메소드는 모두 동일한 PrintSettings 객체를 수용하므로, PrinterName의 동작은 동기 및 비동기 경로 간에 동일합니다.
내 다음 단계는 무엇인가요?
우리는 IronPrint의 PrintSettings.PrinterName 속성을 사용하여 C#에서 프린터 이름을 지정하는 방법에 대해 다뤘습니다. 정적 할당부터 Printer.GetPrinterNames()로 동적 런타임 검색까지. 중요 포인트: 프린터 이름은 정확히 일치해야 합니다(대소문자 구분), null는 OS 기본 프린터로 디폴트되며, 인쇄 대화 상자는 프로그래밍적 선택을 무시합니다.