Printing in a Windows Store App is a bit different to printing using .Net Windows Forms or GDI. In order to print a document in a Windows Store App the application should register for the print contract in each view of the application where printing will be allowed.
In order to register for the print contract, an application needs to:
Obtain a PrintManager object, for example by calling the method Windows.Graphics.Printing.PrintManager.GetForCurrentView()
Create a PrintTask object.
Handle PrintDocument events.
More information about this process can be found in the following pages on MSDN:
Printing (Windows Store apps using C#/VB/C++ and XAML)(Windows)
Printing (Windows Store apps using JavaScript and HTML)(Windows)
Amyuni PDF Creator takes care of the steps 2 and 3, while the step 1 should be handled by the hosting application where the library is being used. The property IacDocument.PrintTaskRequestedEventHandler provides an instance of the delegate TypedEventHandler<PrintManager, PrintTaskRequestedEventArgs> that should be assigned to the property PrintManager.PrintTaskRequested for handling printing events.
// Printing on a XAML Windows Store Application
TypedEventHandler<PrintManager, PrintTaskRequestedEventArgs> m_printTaskRequestedEventToken;
// Registering the Application View for printing
void RegisterForPrinting()
{
// Request a PrintManager instance
PrintManager printMan = PrintManager.GetForCurrentView();
// Add a handler for printing events.
m_printTaskRequestedEventToken = printMan.PrintTaskRequested += m_pdfCreator.Document.PrintTaskRequestedEventHandler;
}
// Unregistering the Application View for printing
void UnregisterForPrinting()
{
// Remove the handler for printing events.
PrintManager printMan = PrintManager.GetForCurrentView();
printMan.PrintTaskRequested -= m_printTaskRequestedEventToken;
}
// Printing on a XAML Windows Store Application
// Registering the Application View for printing
void MyPDFApp::MainPage::RegisterForPrinting()
{
// Request a PrintManager instance
PrintManager^ printMan = PrintManager::GetForCurrentView();
// Add a handler for printing events.
m_printTaskRequestedEventToken = printMan->PrintTaskRequested += m_pdfCreator->Document->PrintTaskRequestedEventHandler;
}
// Unregistering the Application View for printing
void MyPDFApp::MainPage::UnregisterForPrinting()
{
// Remove the handler for printing events.
PrintManager^ printMan = PrintManager::GetForCurrentView();
printMan->PrintTaskRequested -= m_printTaskRequestedEventToken;
}
// Printing from a HTML5/Javascript Windows Store Application
var doc1 = new AmyuniPDFCreator.IacDocument();
doc1.setLicenseKey("Amyuni PDF Creator WinRT-UWP Evaluation", "07EFCDAB0...5CF1B87D");
function OpenPDFAndPrint() {
// Register for print contract
var printManager = Windows.Graphics.Printing.PrintManager.getForCurrentView();
// Note: Do not assing the property IacDocument.printTaskRequestedEventHandler;
// directly to printManager.onprinttaskrequested in javascript,
// we need to wrap it in a JavaScript function instead
function onPrintTaskRequested(printEvent /*printEvent contains the print task request object*/) {
doc1.printTaskRequestedEventHandler(printManager, printEvent);
};
// Setting the event handler for printing via the PrintManager API.
printManager.onprinttaskrequested = onPrintTaskRequested;
WinJS.UI.processAll();
// Open a PDF file using the Windows Runtime dialog FileOpenPicker
var filePicker1 = new Windows.Storage.Pickers.FileOpenPicker();
filePicker1.fileTypeFilter.append(".pdf");
filePicker1.pickSingleFileAsync().then(function (selectedFile) {
return selectedFile.openAsync(Windows.Storage.FileAccessMode.read).then(function (inStream) {
return doc1.openAsync(inStream, "").done(function (openSuccedded) {
if (openSuccedded) {
// Invoke the printing UI
return Windows.Graphics.Printing.PrintManager.showPrintUIAsync();
}
});
});
});
}