UWP development of entry (24) – Win10 style print dialog

Although often seen Adi Wang issued a “look at that development of the UWP and hanged himself,” the chart …… could not help restart about this series. Recently UWP helpful to the print API, to write a special trip to this great idea for a continued soft second.

Long before the Print dialog box similar to this:

The new Win10 style Print dialog box looks like the following figure is very intuitive, including preview.

First, let us build a minimalist UWP program, did not write too long, then maybe hands are born ……

        <Button Width="160" Height="80" Click="Button_Click" Background="Red">PrintButton>

When we tried to click on this button, to display the Print dialog box by PrintHelper class.

        private async void Button_Click(object sender, RoutedEventArgs e)
            var printHelper = new PrintHelper();
            await printHelper.ShowPrintUIAsync();


Here is the MainPage all content. Then let us see the realization PrintHelper.

In the constructor, we need to create an instance of Printdocument and PrintManger, registration for events related to printing.

        public PrintHelper()
            printDocument = new PrintDocument();
            printDocumentSource = printDocument.DocumentSource;
            //printDocument.Paginate += PrintDocument_Paginate;
            printDocument.GetPreviewPage += PrintDocument_GetPreviewPage;
            printDocument.AddPages += PrintDocument_AddPages;
            PrintManager printMan = PrintManager.GetForCurrentView();
            printMan.PrintTaskRequested += PrintMan_PrintTaskRequested;


PrintDocument is mapped to a document to be printed, the next we will attempt to build a preview and other relevant information through it.

printDocument.Paginate event is mainly used for the preparation of all the preview page, the event will display in the Print dialog box, it is executed once. If you are printing a single page of the event can not handle.

printDocument.GetPreviewPage event when displaying specific preview page is executed. For example, two pages, the user before and after the preview page, each page is set by the preview here.

Sample code because only one page, so we went directly to the PrintContent assignment.

        private void PrintDocument_GetPreviewPage(object sender, GetPreviewPageEventArgs e)
            PrintDocument printDoc = (PrintDocument)sender;
            printDoc.SetPreviewPage(e.PageNumber, PrintContent);

printDocument.AddPages event is triggered when the actual printing operation occurs, printDocument will come through the completion of the document prepared by the method AddPage and AddPageComplete and print operations.

        private void PrintDocument_AddPages(object sender, AddPagesEventArgs e)
            PrintDocument printDoc = (PrintDocument)sender;

After completion of the above event registration, we look PrintManger, this can be understood as before UWP version of WPF in PrintDialog. We finally passed it to start the Print dialog box UI. According to the document, first we must call PrintManager.GetForCurrentView () method, which returns the currently active UWP Window associated PrintManager, then we need to register the event printMan.PrintTaskRequested, this event will be triggered when the printing operation occurs.

        private void PrintMan_PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs args)
            PrintTask printTask = null;
            printTask = args.Request.CreatePrintTask("1", sourceRequested =>


In this event, the general will CreatePrintTask, then do some print configuration, and finally specify printDocumentSource.

PrintHelper in the rest of the code, and only when the packaging simple parameter passing:

        public async Task ShowPrintUIAsync()
            if (PrintManager.IsSupported())
                await PrintManager.ShowPrintUIAsync();
        public virtual void PreparePrintContent(UIElement printContent)
            PrintContent = printContent;


You can refer to the specific Sample code on Github:


Leave a Reply