UWP C# Printing an (actual UWP) page.

VoyTec 591 Reputation points
2021-04-22T22:13:45.95+00:00

Hello VS Community,

I ask for help. Basically I need to print an (actual UWP) Page. How do I do that? Any help will be submitted here should include all librares and namespaces, please (step by step - for a beginner). If it is needed - also how to fit the UWP Page to fit one page of print area.
I will be gratefull for help.

Universal Windows Platform (UWP)
No comments
{count} votes

Accepted answer
  1. AryaDing-MSFT 2,826 Reputation points
    2021-04-23T06:24:47.403+00:00

    Hi,

    Welcome to Microsoft Q&A!

    If you want to print page in uwp app, you could refer to the following steps.

    1.Write a xaml page that contains a print button. The OnPrintButtonClick event informs the user of the print situation.

    <Page..>  
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
            <StackPanel>  
            <Button  Click="OnPrintButtonClick">Print</Button>         
            <TextBlock Text="It's a good day !"/>  
            </StackPanel>  
        </Grid>  
    </Page>  
    

    2.Declare the PrintManager and PrintDocument, register for printing

    private PrintManager printMan;  
            private PrintDocument printDocument;  
            private IPrintDocumentSource  printDocumentSource;  
    
            protected override void OnNavigatedTo(NavigationEventArgs e)  
            {  
                // Register for PrintTaskRequested event  
                printMan = PrintManager.GetForCurrentView();  
    
                printMan.PrintTaskRequested += PrintTaskRequested;  
    
                // Build a PrintDocument and register for callbacks  
               printDocument = new PrintDocument();  
                printDocSource = printDocument.DocumentSource;  
                printDocument.Paginate += CreatePrintPreviewPages;   
                printDocument.GetPreviewPage += GetPrintPreviewPage;  
                printDocument.AddPages += AddPrintPages;  
            }  
    

    3.Handle the print process, achieve the PrintTaskRequested, CreatePrintPreviewPages, GetPrintPreviewPage, AddPrintPages event, you could refer to the official document to know detailed code of these event handler.


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful