question

Voytec-6484 avatar image
0 Votes"
Voytec-6484 asked ·

UWP C# Printing an (actual UWP) page.

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.

windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AryaDing-MSFT avatar image
0 Votes"
AryaDing-MSFT answered ·

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.


· 14 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I can't get 3rd part working :/

I am trying to do what docuemntation says but I can't understand it and it doesn't work.
I also downloaded sample from GIT like documentations mentioned but it doesn't work either.

0 Votes 0 ·

What is this?:
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.WebHostHidden]
[Windows.Foundation.Metadata.Composable(typeof(Windows.UI.Xaml.Printing.IPrintDocumentFactory), Windows.Foundation.Metadata.CompositionType.Public, 65536, "Windows.Foundation.UniversalApiContract")]
[Windows.Foundation.Metadata.Static(typeof(Windows.UI.Xaml.Printing.IPrintDocumentStatics), 65536, "Windows.Foundation.UniversalApiContract")]

I don't know how to delacre API PrintDocument:

 public class PrintDocument : DependencyObject { }


0 Votes 0 ·

@Voytec-6484 You could declare PrintDocument in MainPage class and initialize it in OnNavigatedTo method, you don’t need to create PrintDocument class by yourself, this API will be implemented automatically. As follows:

   public sealed partial class MainPage : Page
   {
    private PrintDocument printDocument;
    public MainPage()
         {
             this.InitializeComponent();
         }
     protected override void OnNavigatedTo(NavigationEventArgs e)
      {……}
   } 




0 Votes 0 ·
Show more comments