[Windows 8] How to print a document ?

April 16th, 2012 | Posted by Tom in .NET | Article | Windows 8 | Read: 1,136

Here is a simple code sample that can help you if you plan to add the Print feature in your application.

First, you need to setup your application and specify preview pages, options, etc.:

var pd = new PrintDocument();

pd.Paginate += (sender, args) =>

{

    // Set the number of pages to preview

    pd.SetPreviewPageCount(1, PreviewPageCountType.Final);

};

pd.AddPages += (sender, args) =>

{

    // Add a page/document to the print list

    pd.AddPage(this);

    pd.AddPagesComplete();

};

pd.GetPreviewPage += (sender, args) =>

{

    // Indicate the page number to display in the preview window

    pd.SetPreviewPage(args.PageNumber, this);

};

 

var printManager = PrintManager.GetForCurrentView();

printManager.PrintTaskRequested += (sender, args) =>

{

    // Create a print task and set its options

    var printTask = args.Request.CreatePrintTask(“My First WinRT Impression”, requestedArgs => requestedArgs.SetSource(pd.DocumentSource));

    printTask.Options.Orientation = PrintOrientation.Landscape;

};

Finally, you need to display the Print Page to the user:

private async void PrintButtonClick(object sender, RoutedEventArgs e)

{

    // Display the print ui

    await PrintManager.ShowPrintUIAsync();

}

And that’s all!

For your information, if you saved the document as a XPS file, this one is saved in “Documents” library !

 

Happy WinRT coding!

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

One Response




Leave a Reply