PDF Printing Library in UWP
The print button from the main toolbar triggers an event you can subscribe to. You can then use PSPDFKit.PrintHelper
to present a print dialog to the user. Note that it isn’t necessary to subscribe to the event in order to initiate printing; it’s merely a convenience.
Toolbar Print Button
The following code from the Catalog app demonstrates how to subscribe to the print button click event in the toolbar. It’s important to only subscribe once the PSPDFKit.UI.PdfView
has completed initialization:
public PrintPage() { InitializeComponent(); ViewModel.Initialize(PDFView); PDFView.InitializationCompletedHandler += PDFView_InitializationCompletedHandler; } // Once the API is initialized, we tell it we want to know when the user clicks the print button. private void PDFView_InitializationCompletedHandler(PSPDFKit.PDFView sender, int args) { sender.Controller.OnPrint += API_OnPrint; } private void API_OnPrint(PSPDFKit.API sender, object args) { ViewModel.OnPrint(this); }
Windows Print Service Dialog
To display the Windows printing service dialog, you’ll need to place a named Canvas
UIElement
in your page’s XAML:
<Canvas x:Name="PrintCanvas" Opacity="0"/>
To open the dialog, register an instance of the PrintHelper
class with the PSPDFKit.Pdf.Document
, the page you’re printing from, and the name of the Canvas
element.
To be informed of the status of the print job, add an event handler to PrintHelper.PrintingCompleteHandler
:
internal async void OnPrint(Windows.UI.Xaml.Controls.Page owningPage) { try { // For printing a `Canvas`, `UIElement` is required in the visual tree on the current page. var printHelper = new PrintHelper(PDFView.Document, owningPage, "PrintCanvas", "MyDocumentFileName.pdf"); printHelper.PrintingCompleteHandler += PrintHelper_PrintingCompleteHandler; await printHelper.ShowPrintUIAsync(); } catch (Exception e) { var messageDialog = new MessageDialog(e.ToString()); await messageDialog.ShowAsync(); } }
The print status tells you if the job was canceled or completed or something else:
private static async void PrintHelper_PrintingCompleteHandler(PrintHelper sender, Windows.Graphics.Printing.PrintTaskCompletedEventArgs args) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { var messageDialog = new MessageDialog("Print Status: " + args.Completion.ToString()); await messageDialog.ShowAsync(); }); }
PrintHelper
The PrintHelper
also exposes two notable properties: PrintMediaSize
and Watermark
.
Setting the default media size for the printing dialog can be done through PrintMediaSize
, using Microsoft’s PrintMediaSize
enum.
Watermarks can be added during the printing process as follows:
var printHelper = await PrintHelper.CreatePrintHelperFromSourceAsync(documentSource, this, "PrintCanvas", "PrintWithoutUI"); using (var image = await AssetLoader.GetWatermarkAsync()) { printHelper.Watermark = new Watermark(image) { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, SizeOnPage = new Size(200, 200) }; } await printHelper.ShowPrintUIAsync();
For more information on watermarks, refer to our watermarks guide.