简体   繁体   中英

Printing in Universal Apps

I'm developing a Windows 10 Universal App that needs to perform calculations and generate a printout based on the results. I don't know where to get started with printing. What is the easiest way to create a new document? In WPF we had FixedDocument and FlowDocument. I don't see those available in the Universal App format.

Declare the PrintManager and PrintDocument . The PrintManager type is in the Windows.Graphics.Printing namespace along with types to support other Windows printing functionality. The PrintDocument type is in the Windows.UI.Xaml.Printing namespace along with other types that support preparing XAML content for printing. You can make it easier to write your printing code by adding the following using or Imports statements to your page.

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

The PrintDocument class is used to handle much of the interaction between the app and the PrintManager , but it exposes several callbacks of its own. During registration, create instances of PrintManager and PrintDocument and register handlers for their printing events. In the UWP print sample, registration is performed by the RegisterForPrinting method.

public virtual void RegisterForPrinting()
{
   printDocument = new PrintDocument();
   printDocumentSource = printDocument.DocumentSource;
   printDocument.Paginate += CreatePrintPreviewPages;
   printDocument.GetPreviewPage += GetPrintPreviewPage;
   printDocument.AddPages += AddPrintPages;

   PrintManager printMan = PrintManager.GetForCurrentView();
   printMan.PrintTaskRequested += PrintTaskRequested;
}

When the user goes to a page that supports, it initiates the registration within the OnNavigatedTo method.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
   // Initalize common helper class and register for printing
   printHelper = new PrintHelper(this);
   printHelper.RegisterForPrinting();

   // Initialize print content for this scenario
   printHelper.PreparePrintContent(new PageToPrint());

   // Tell the user how to print
   MainPage.Current.NotifyUser("Print contract registered with customization, use the Print button to print.", NotifyType.StatusMessage);
}

When the user leaves the page, disconnect the printing event handlers. If you have a multiple-page app and don't disconnect printing, an exception is thrown when the user leaves the page and then comes back to it.

You can read more on Print from your app and also find the TIP: Tip Most of the examples in this topic are based on the print sample. To see the full code, download the Universal Windows Platform (UWP) print sample from the Windows-universal-samples repo on GitHub.

GetPrintPreviewPage event is fierd once time for each page, when i return to a page the event not fierd. I use this code to persist the current preview page.

private async void PrintDoc_GetPreviewPage(object p_sender, GetPreviewPageEventArgs p_args) { SelectedPagePreview = p_args.PageNumber;

i am testing on a windows 8.1 & tablet x64.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM