Share via


인쇄 미리 보기 UI 사용자 지정하기

이 항목은 인쇄 미리 보기 UI에서 인쇄 옵션 및 설정을 사용자 지정하는 방법을 설명합니다. 인쇄에 대해 자세히 알아보려면 앱에서 인쇄를 참조하세요.

이 항목의 예시 대부분은 UWP(Universal Windows Platform) 인쇄 샘플을 기반으로 합니다.

중요 API

인쇄 옵션 사용자 지정하기

기본적으로 인쇄 미리 보기 UI에는 ColorMode, CopiesOrientation 인쇄 옵션이 표시됩니다. 또한 이러한 옵션 외에도 인쇄 미리 보기 UI에 추가할 수 있는 몇 가지 일반적인 프린터 옵션이 있습니다.

이러한 옵션은 StandardPrintTaskOptions 클래스에 정의됩니다. 인쇄 미리 보기 UI에 표시된 옵션 목록에서 옵션을 추가하거나 제거할 수 있습니다. 표시되는 순서를 변경하고 사용자에게 표시되는 기본 설정을 설정할 수도 있습니다.

그러나 이 메서드를 사용하여 수정한 내용은 인쇄 미리 보기 UI에만 영향을 줍니다. 사용자는 항상 인쇄 미리 보기 UI에서 **기타 설정*을 탭하여 프린터에서 지원하는 모든 옵션에 액세스할 수 있습니다.

참고

앱에서 표시할 인쇄 옵션을 지정할 수 있지만, 선택한 프린터에서 지원하는 인쇄 옵션만 인쇄 미리 보기 UI에 표시됩니다. 선택한 프린터에서 지원하지 않는 옵션이 인쇄 UI에 표시되지 않습니다.

표시할 옵션 정의하기

앱의 화면이 로드되면 인쇄 계약에 등록됩니다. 해당 등록의 일부에는 PrintTaskRequested 이벤트 처리기 정의하기가 포함됩니다. 인쇄 미리 보기 UI에 표시되는 옵션을 사용자 지정하는 코드인 **PrintTaskRequested*-가 이벤트 처리기에 추가됩니다.

PrintTaskRequested 이벤트 처리기를 수정하여 인쇄 미리 보기 UI에 표시할 인쇄 설정을 구성하는 printTask.options 지침을 포함합니다. 사용자 지정된 인쇄 옵션 목록을 표시하려는 앱의 화면의 경우 화면이 인쇄될 때 표시할 옵션을 지정하는 코드를 포함하도록 도우미 클래스의 **PrintTaskRequested*- 이벤트 처리기를 재정의합니다.

protected override void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
{
   PrintTask printTask = null;
   printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequestedArgs =>
   {
         IList<string> displayedOptions = printTask.Options.DisplayedOptions;

         // Choose the printer options to be shown.
         // The order in which the options are appended determines the order in which they appear in the UI
         displayedOptions.Clear();
         displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Copies);
         displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
         displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.MediaSize);
         displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Collation);
         displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Duplex);

         // Preset the default value of the printer option
         printTask.Options.MediaSize = PrintMediaSize.NorthAmericaLegal;

         // Print Task event handler is invoked when the print job is completed.
         printTask.Completed += async (s, args) =>
         {
            // Notify the user when the print operation fails.
            if (args.Completion == PrintTaskCompletion.Failed)
            {
               await scenarioPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
               {
                     MainPage.Current.NotifyUser("Failed to print.", NotifyType.ErrorMessage);
               });
            }
         };

         sourceRequestedArgs.SetSource(printDocumentSource);
   });
}

중요

displayedOptions.clear()를 호출하면 **기타 설정*- 링크를 포함하여 인쇄 미리 보기 UI에서 모든 인쇄 옵션이 제거됩니다. 인쇄 미리 보기 UI에 표시할 옵션을 추가해야 합니다.

기본 옵션 지정하기

옵션의 기본값을 인쇄 미리 보기 UI에서 설정할 수도 있습니다. 마지막 예시에서 다음의 코드 줄은 MediaSize 옵션의 기본값을 설정합니다.

         // Preset the default value of the printer option
         printTask.Options.MediaSize = PrintMediaSize.NorthAmericaLegal;

새 인쇄 옵션 추가하기

여기서는 새 인쇄 옵션을 만들고, 옵션이 지원하는 값 목록을 정의한 다음, 인쇄 미리 보기에 옵션을 추가하는 방법을 설명합니다. 이전 섹션과 마찬가지로 PrintTaskRequested 이벤트 처리기에 새 인쇄 옵션을 추가합니다.

먼저 PrintTaskOptionDetails 개체를 가져옵니다. 인쇄 미리 보기 UI에 새 인쇄 옵션을 추가하는 데 사용됩니다. 그런 다음 인쇄 미리 보기 UI에 표시되는 옵션 목록을 지우고 사용자가 앱에서 인쇄하려고 할 때 표시할 옵션을 추가합니다. 그런 다음 새 인쇄 옵션을 만들고 옵션 값 목록을 초기화합니다. 마지막으로 새 옵션을 추가하고 **OptionChanged*- 이벤트에 대한 처리기를 할당합니다.

protected override void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
{
   PrintTask printTask = null;
   printTask = e.Request.CreatePrintTask("C# Printing SDK Sample", sourceRequestedArgs =>
   {
         PrintTaskOptionDetails printDetailedOptions = PrintTaskOptionDetails.GetFromPrintTaskOptions(printTask.Options);
         IList<string> displayedOptions = printDetailedOptions.DisplayedOptions;

         // Choose the printer options to be shown.
         // The order in which the options are appended determines the order in which they appear in the UI
         displayedOptions.Clear();

         displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Copies);
         displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.Orientation);
         displayedOptions.Add(Windows.Graphics.Printing.StandardPrintTaskOptions.ColorMode);

         // Create a new list option
         PrintCustomItemListOptionDetails pageFormat = printDetailedOptions.CreateItemListOption("PageContent", "Pictures");
         pageFormat.AddItem("PicturesText", "Pictures and text");
         pageFormat.AddItem("PicturesOnly", "Pictures only");
         pageFormat.AddItem("TextOnly", "Text only");

         // Add the custom option to the option list
         displayedOptions.Add("PageContent");

         printDetailedOptions.OptionChanged += printDetailedOptions_OptionChanged;

         // Print Task event handler is invoked when the print job is completed.
         printTask.Completed += async (s, args) =>
         {
            // Notify the user when the print operation fails.
            if (args.Completion == PrintTaskCompletion.Failed)
            {
               await scenarioPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
               {
                     MainPage.Current.NotifyUser("Failed to print.", NotifyType.ErrorMessage);
               });
            }
         };

         sourceRequestedArgs.SetSource(printDocumentSource);
   });
}

옵션은 인쇄 미리 보기 UI에 추가된 순서와 동일한 순서로 표시되며, 첫 번째 옵션이 창 맨 위에 표시됩니다. 이 예시에서는 옵션 목록의 맨 아래에 표시되도록 사용자 지정 옵션이 마지막으로 추가됩니다. 그러나 목록의 아무 곳에나 배치할 수 있습니다. 사용자 지정 인쇄 옵션을 마지막으로 추가할 필요는 없습니다.

사용자가 사용자 지정 옵션에서 선택한 옵션을 변경하면 인쇄 미리 보기 이미지를 업데이트합니다. 아래와 같이 InvalidatePreview 메서드를 호출하여 인쇄 미리 보기 UI에서 이미지를 다시 그립니다.

async void printDetailedOptions_OptionChanged(PrintTaskOptionDetails sender, PrintTaskOptionChangedEventArgs args)
{
   // Listen for PageContent changes
   string optionId = args.OptionId as string;
   if (string.IsNullOrEmpty(optionId))
   {
         return;
   }

   if (optionId == "PageContent")
   {
         await scenarioPage.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
         {
            printDocument.InvalidatePreview();
         });
   }
}

관련 항목