문서 인쇄 개요(WPF .NET)

Microsoft .NET을 통해 WPF(Windows Presentation Foundation)를 사용하는 애플리케이션 개발자는 다양한 인쇄 및 인쇄 시스템 관리 API 세트를 갖게 됩니다. 이 기능의 핵심은 XPS(XML Paper Specification) 파일 형식과 XPS 인쇄 경로입니다.

XPS 정보

XPS는 전자 문서 형식, 스풀 파일 형식 및 페이지 설명 언어입니다. XML, Open Packaging Conventions 및 기타 산업 표준을 사용하여 플랫폼 간 문서를 만드는 개방형 문서 형식입니다. XPS는 디지털 문서를 만들고, 공유하고, 인쇄하고, 보고, 보관하는 프로세스를 간소화합니다. XPS에 대한 자세한 내용은 XPS 문서를 참조하세요.

XPS 인쇄 경로

XPS 인쇄 경로는 Windows 애플리케이션에서 인쇄를 처리하는 방법을 다시 정의하는 Windows 기능입니다. XPS 인쇄 경로는 다음을 대체할 수 있습니다.

  • 서식 있는 텍스트 형식 또는 이식 가능한 문서 형식과 같은 문서 프레젠테이션 언어.
  • Windows 메타파일 또는 EMF(Enhanced Metafile)와 같은 스풀러 형식을 인쇄합니다.
  • 프린터 명령 언어 또는 PostScript와 같은 페이지 설명 언어입니다.

결과적으로 XPS 인쇄 경로는 애플리케이션 게시부터 프린터 드라이버 또는 디바이스의 최종 처리까지 XPS 형식을 유지 관리합니다.

XPS 문서의 인쇄 스풀러는 XPS 인쇄 경로와 GDI 인쇄 경로를 모두 지원합니다. XPS 인쇄 경로는 기본적으로 XPS 스풀 파일을 사용하며 XPS 프린터 드라이버가 필요합니다. XPS 인쇄 경로는 XPS 프린터 드라이버(XPSDrv) 모델을 기반으로 합니다.

XPS 인쇄 경로의 이점은 다음과 같습니다.

  • WYSIWYG 인쇄 지원.
  • 채널당 32비트, CMYK 색 모델, 명명된 색, n-잉크, 투명도 및 그라데이션과 같은 고급 색 프로필을 기본적으로 지원합니다.
  • 향상된 인쇄 성능 - XPS 기능 및 향상된 기능은 XPS 인쇄 경로를 대상으로 하는 애플리케이션에서만 사용할 수 있습니다.
  • 업계 표준 XPS 형식입니다.

기본 인쇄 시나리오의 경우 인쇄 구성 및 작업 제출을 위한 표준 UI와 함께 간단하고 직관적인 API를 사용할 수 있습니다. 고급 시나리오의 경우 API는 UI 사용자 지정 또는 UI 없음, 동기 또는 비동기 인쇄 및 일괄 처리 인쇄 기능을 지원합니다. 단순 및 고급 옵션은 모두 완전 신뢰 모드 또는 부분 신뢰 모드에서 인쇄 지원을 제공합니다.

XPS는 확장성을 염두에 두고 설계되었기 때문에 기능을 모듈식으로 XPS에 추가할 수 있습니다. 확장성 기능은 다음과 같습니다.

  • 디바이스 기능의 신속한 확장을 지원하는 인쇄 스키마입니다. 스키마의 공용 부분은 원하는 디바이스 기능을 추가하도록 정기적으로 업데이트됩니다. 자세한 내용은 확장 가능한 아키텍처를 참조하세요.
  • XPSDrv 드라이버가 XPS 문서의 직접 및 확장 가능한 인쇄를 지원하는 데 사용하는 확장 가능한 필터 파이프라인입니다. 자세한 내용은 XPSDrv 프린터 드라이버를 참조하세요.

WPF 애플리케이션은 기본적으로 XPS 인쇄 경로를 지원하며 XPS 인쇄 API를 사용하여 XPSDrv 드라이버에 직접 인쇄할 수 있습니다. 쓰기 작업의 대상 인쇄 큐에 XPSDrv 드라이버가 없는 경우 XpsDocumentWriter 클래스의 WriteWriteAsync 메서드는 콘텐츠를 GDI 인쇄 경로에 대해 XPS에서 GDI 형식으로 자동 변환합니다.

다음 그림에서는 인쇄 하위 시스템을 보여주고 Microsoft와 독립 소프트웨어 및 하드웨어 공급업체에서 제공하는 부분을 정의합니다.

XPS 인쇄 시스템을 보여주는 스크린샷.

기본 XPS 인쇄

WPF에는 기본 및 고급 인쇄 기능을 모두 지원하는 인쇄 API가 있습니다. 광범위한 인쇄 사용자 지정 또는 전체 XPS 기능 집합에 대한 액세스가 필요하지 않은 애플리케이션의 경우 기본 인쇄 지원으로 충분할 수 있습니다. 기본 인쇄 지원은 최소한의 구성이 필요하고 친숙한 UI가 있으며 많은 XPS 기능을 지원하는 PrintDialog 컨트롤을 통해 제공됩니다.

PrintDialog

System.Windows.Controls.PrintDialog 컨트롤은 UI, 구성 및 XPS 작업 제출을 위한 단일 진입점을 제공합니다. 컨트롤을 인스턴스화하고 사용하는 방법을 알아보려면 인쇄 대화 상자를 표시하는 방법을 참조하세요.

고급 XPS 인쇄

XPS 기능의 전체 집합에 액세스하려면 고급 인쇄 API를 사용합니다. 이 섹션에서는 PrintTicket, PrintCapabilities, PrintServer, PrintQueueXpsDocumentWriter를 비롯한 몇 가지 관련 API에 대해 설명합니다. XPS 인쇄 경로 API의 전체 목록은 System.Windows.XpsSystem.Printing 네임스페이스를 참조하세요.

PrintTicket 및 PrintCapabilities

PrintTicketPrintCapabilities 클래스는 고급 XPS 기능의 기초입니다. 두 개체 모두 인쇄 스키마로 정의된 인쇄 지향 기능의 XML 형식 구조체를 포함합니다. 이 기능에는 이중, 데이터 정렬, 양면 인쇄 및 스테이플링이 포함됩니다. PrintTicket은 인쇄 작업을 처리하는 방법을 프린터에 지시합니다. PrintCapabilities 클래스는 프린터 기능을 정의합니다. 프린터 기능 쿼리를 통해 프린터에서 지원하는 기능을 완전히 활용하는 PrintTicket을 만들 수 있습니다. 마찬가지로, 지원되지 않는 기능을 방지할 수 있습니다.

다음 예제에서는 프린터의 PrintCapabilities를 쿼리하고 코드를 사용하여 PrintTicket을 만듭니다.

/// <summary>
/// Returns a print ticket, which is a set of instructions telling a printer how
/// to set its various features, such as duplexing, collating, and stapling.
/// </summary>
/// <param name="printQueue">The print queue to print to.</param>
/// <returns>A print ticket.</returns>
public static PrintTicket GetPrintTicket(PrintQueue printQueue)
{
    PrintCapabilities printCapabilites = printQueue.GetPrintCapabilities();

    // Get a default print ticket from printer.
    PrintTicket printTicket = printQueue.DefaultPrintTicket;

    // Modify the print ticket.
    if (printCapabilites.CollationCapability.Contains(Collation.Collated))
        printTicket.Collation = Collation.Collated;
    if (printCapabilites.DuplexingCapability.Contains(Duplexing.TwoSidedLongEdge))
        printTicket.Duplexing = Duplexing.TwoSidedLongEdge;
    if (printCapabilites.StaplingCapability.Contains(Stapling.StapleDualLeft))
        printTicket.Stapling = Stapling.StapleDualLeft;

    // Returns a print ticket, which is a set of instructions telling a printer how
    // to set its various features, such as duplexing, collating, and stapling.
    return printTicket;
}
''' <summary>
''' Returns a print ticket, which is a set of instructions telling a printer how
''' to set its various features, such as duplexing, collating, and stapling.
''' </summary>
''' <param name="printQueue">The print queue to print to.</param>
''' <returns>A print ticket.</returns>
Public Shared Function GetPrintTicket(printQueue As PrintQueue) As PrintTicket

    Dim printCapabilites As PrintCapabilities = printQueue.GetPrintCapabilities()

    ' Get a default print ticket from printer.
    Dim printTicket As PrintTicket = printQueue.DefaultPrintTicket

    ' Modify the print ticket.
    If printCapabilites.CollationCapability.Contains(Collation.Collated) Then
        printTicket.Collation = Collation.Collated
    End If
    If printCapabilites.DuplexingCapability.Contains(Duplexing.TwoSidedLongEdge) Then
        printTicket.Duplexing = Duplexing.TwoSidedLongEdge
    End If
    If printCapabilites.StaplingCapability.Contains(Stapling.StapleDualLeft) Then
        printTicket.Stapling = Stapling.StapleDualLeft
    End If

    ' Returns a print ticket, which is a set of instructions telling a printer how
    ' to set its various features, such as duplexing, collating, and stapling.
    Return printTicket

End Function

PrintServer 및 PrintQueue

PrintServer 클래스는 네트워크 인쇄 서버를 나타내고 PrintQueue 클래스는 프린터 및 프린터와 연결된 출력 작업 큐를 나타냅니다. 이러한 API는 함께 서버에 대한 인쇄 작업의 고급 관리를 지원합니다. PrintServer 또는 해당 파생 클래스 중 하나는 PrintQueue를 관리하는 데 사용됩니다.

다음 예제에서는 LocalPrintServer를 만들고 코드를 사용하여 로컬 컴퓨터의 PrintQueueCollection에 액세스합니다.

/// <summary>
/// Return a collection of print queues, which individually hold the features or states
/// of a printer as well as common properties for all print queues.
/// </summary>
/// <returns>A collection of print queues.</returns>
public static PrintQueueCollection GetPrintQueues()
{
    // Create a LocalPrintServer instance, which represents 
    // the print server for the local computer.
    LocalPrintServer localPrintServer = new();

    // Get the default print queue on the local computer.
    //PrintQueue printQueue = localPrintServer.DefaultPrintQueue;

    // Get all print queues on the local computer.
    PrintQueueCollection printQueueCollection = localPrintServer.GetPrintQueues();

    // Return a collection of print queues, which individually hold the features or states
    // of a printer as well as common properties for all print queues.
    return printQueueCollection;
}
''' <summary>
''' Return a collection of print queues, which individually hold the features or states
''' of a printer as well as common properties for all print queues.
''' </summary>
''' <returns>A collection of print queues.</returns>
Public Shared Function GetPrintQueues() As PrintQueueCollection

    ' Create a LocalPrintServer instance, which represents 
    ' the print server for the local computer.
    Dim localPrintServer As LocalPrintServer = New LocalPrintServer()

    ' Get the default print queue on the local computer.
    'Dim  printQueue As PrintQueue = localPrintServer.DefaultPrintQueue

    ' Get all print queues on the local computer.
    Dim printQueueCollection As PrintQueueCollection = localPrintServer.GetPrintQueues()

    ' Return a collection of print queues, which individually hold the features or states
    ' of a printer as well as common properties for all print queues.
    Return printQueueCollection

End Function

XpsDocumentWriter

많은 WriteWriteAsync 메서드가 있는 XpsDocumentWriter는 XPS 문서를 PrintQueue에 추가하는 데 사용됩니다. 예를 들어 Write(FixedDocumentSequence, PrintTicket) 메서드는 인쇄 티켓이 있는 XPS 문서를 큐에 동기적으로 추가하는 데 사용됩니다. WriteAsync(FixedDocumentSequence, PrintTicket) 메서드는 인쇄 티켓이 있는 XPS 문서를 큐에 비동기적으로 추가하는 데 사용됩니다.

다음 예제에서는 XpsDocumentWriter를 만들고 코드를 사용하여 동기 및 비동기적으로 XPS 문서를 PrintQueue에 추가합니다.

/// <summary>
/// Asynchronously, add the XPS document together with a print ticket to the print queue.
/// </summary>
/// <param name="xpsFilePath">Path to source XPS file.</param>
/// <param name="printQueue">The print queue to print to.</param>
/// <param name="printTicket">The print ticket for the selected print queue.</param>
public static void PrintXpsDocumentAsync(string xpsFilePath, PrintQueue printQueue, PrintTicket printTicket)
{
    // Create an XpsDocumentWriter object for the print queue.
    XpsDocumentWriter xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);

    // Open the selected document.
    XpsDocument xpsDocument = new(xpsFilePath, FileAccess.Read);

    // Get a fixed document sequence for the selected document.
    FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();

    // Asynchronously, add the XPS document together with a print ticket to the print queue.
    xpsDocumentWriter.WriteAsync(fixedDocSeq, printTicket);
}

/// <summary>
/// Synchronously, add the XPS document together with a print ticket to the print queue.
/// </summary>
/// <param name="xpsFilePath">Path to source XPS file.</param>
/// <param name="printQueue">The print queue to print to.</param>
/// <param name="printTicket">The print ticket for the selected print queue.</param>
public static void PrintXpsDocument(string xpsFilePath, PrintQueue printQueue, PrintTicket printTicket)
{
    // Create an XpsDocumentWriter object for the print queue.
    XpsDocumentWriter xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(printQueue);

    // Open the selected document.
    XpsDocument xpsDocument = new(xpsFilePath, FileAccess.Read);

    // Get a fixed document sequence for the selected document.
    FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();

    // Synchronously, add the XPS document together with a print ticket to the print queue.
    xpsDocumentWriter.Write(fixedDocSeq, printTicket);
}
''' <summary>
''' Asynchronously, add the XPS document together with a print ticket to the print queue.
''' </summary>
''' <param name="xpsFilePath">Path to source XPS file.</param>
''' <param name="printQueue">The print queue to print to.</param>
''' <param name="printTicket">The print ticket for the selected print queue.</param>
Public Shared Sub PrintXpsDocumentAsync(xpsFilePath As String, printQueue As PrintQueue, printTicket As PrintTicket)

    ' Create an XpsDocumentWriter object for the print queue.
    Dim xpsDocumentWriter As XpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(printQueue)

    ' Open the selected document.
    Dim xpsDocument As XpsDocument = New XpsDocument(xpsFilePath, FileAccess.Read)

    ' Get a fixed document sequence for the selected document.
    Dim fixedDocSeq As FixedDocumentSequence = xpsDocument.GetFixedDocumentSequence()

    ' Asynchronously, add the XPS document together with a print ticket to the print queue.
    xpsDocumentWriter.WriteAsync(fixedDocSeq, printTicket)

End Sub

''' <summary>
''' Synchronously, add the XPS document together with a print ticket to the print queue.
''' </summary>
''' <param name="xpsFilePath">Path to source XPS file.</param>
''' <param name="printQueue">The print queue to print to.</param>
''' <param name="printTicket">The print ticket for the selected print queue.</param>
Public Shared Sub PrintXpsDocument(xpsFilePath As String, printQueue As PrintQueue, printTicket As PrintTicket)

    ' Create an XpsDocumentWriter object for the print queue.
    Dim xpsDocumentWriter As XpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(printQueue)

    ' Open the selected document.
    Dim xpsDocument As XpsDocument = New XpsDocument(xpsFilePath, FileAccess.Read)

    ' Get a fixed document sequence for the selected document.
    Dim fixedDocSeq As FixedDocumentSequence = xpsDocument.GetFixedDocumentSequence()

    ' Synchronously, add the XPS document together with a print ticket to the print queue.
    xpsDocumentWriter.Write(fixedDocSeq, printTicket)

End Sub

GDI 인쇄 경로

WPF 애플리케이션은 기본적으로 XPS 인쇄 경로를 지원하지만 XpsDocumentWriter 클래스의 Write 또는 WriteAsync 메서드 중 하나를 호출하고 XpsDrv가 아닌 프린터의 인쇄 큐를 선택하여 GDI 인쇄 경로로 출력할 수도 있습니다.

XPS 기능이나 지원이 필요하지 않은 애플리케이션의 경우 현재 GDI 인쇄 경로가 변경되지 않은 상태로 유지됩니다. GDI 인쇄 경로 및 다양한 XPS 변환 옵션에 대한 자세한 내용은 MXDC(Microsoft XPS 문서 변환기)XPSDrv 프린터 드라이버를 참조하세요.

XPSDrv 드라이버 모델

XPS 인쇄 경로는 XPS 사용 프린터 또는 드라이버에 인쇄할 때 XPS를 기본 인쇄 스풀 형식으로 사용하여 스풀러 효율성을 향상시킵니다. 렌더링 서비스를 위해 GDI에 대한 일련의 호출로 애플리케이션 출력을 나타내는 EMF와 달리 XPS 스풀 형식은 문서를 나타냅니다. 따라서 XPS 스풀 파일이 XPS 기반 프린터 드라이버에 출력되는 경우 드라이버가 해당 형식의 데이터에 대해 직접 작동하므로 추가 해석이 필요하지 않습니다. 이 기능은 EMF 파일 및 GDI 기반 인쇄 드라이버에 필요한 데이터 및 색 공간 변환을 제거합니다.

간소화된 스풀링 프로세스에서는 문서가 스풀링되기 전에 EMF 데이터 파일과 같은 중간 스풀 파일을 생성할 필요가 없습니다. 작은 스풀 파일 크기를 통해 XPS 인쇄 경로는 네트워크 트래픽을 줄이고 인쇄 성능을 향상시킬 수 있습니다. EMF에 비해 XPS 스풀 파일 크기는 XPS 인쇄 경로를 사용할 때 일반적으로 줄어듭니다. 스풀 파일 크기 축소는 다음과 같은 여러 메커니즘을 통해 수행됩니다.

  • XPS 파일의 문서 내에서 사용되는 문자만 저장하는 글꼴 하위 집합입니다.
  • XPS 콘텐츠의 래스터화를 방지하기 위해 기본적으로 투명도 및 그라데이션 기본 형식을 지원하는 고급 그래픽 지원.
  • 문서에서 여러 번 사용되는 회사 로고 이미지와 같은 일반적인 리소스 식별. 일반 리소스는 공유 리소스로 처리되며 한 번만 로드됩니다.
  • 모든 XPS 문서에서 사용되는 ZIP 압축.

벡터 그래픽이 매우 복잡하거나, 다중 계층화되거나, 비효율적으로 작성된 경우 XPS 스풀 파일 크기가 줄어들지 않을 수 있습니다. GDI 스풀 파일과 달리 XPS 파일에는 화면 표시를 위해 디바이스 글꼴과 컴퓨터 기반 글꼴이 포함되어 있지만 두 종류의 글꼴은 모두 하위 집합되고 프린터 드라이버가 파일을 프린터로 전송하기 전에 디바이스 글꼴을 제거할 수 있습니다.

PrintQueue.AddJob 메서드를 사용하여 XPS 파일을 인쇄할 수도 있습니다. 자세한 내용은 XPS 파일을 인쇄하는 방법을 참조하세요.

참고 항목