Description
@liminzhu wrote:
For the WebView2 we're building though, you can use window.print. WebView opens the same printing prompt and prints what the browser does. I was messing with this,
webviewWindow->Navigate(L"https://www.twitter.com/");
EventRegistrationToken token;
webviewWindow->add_NavigationCompleted(Callback<IWebView2NavigationCompletedEventHandler>(
[](IWebView2WebView* wv, IWebView2NavigationCompletedEventArgs* args) -> HRESULT {
webviewWindow->ExecuteScript(L"window.print();", nullptr);
return S_OK;
}).Get(), &token);
That's great. We'll be among the people using that feature. However, considering that the above technique uses ExecuteScript
, will printing always still work if EcmaScript is disabled? I'm referring to WebView2 having a setting equivalent to Windows.UI.Xaml.Controls.WebViewSettings.IsJavaScriptEnabled
, that is put_IsScriptEnabled.
For this reason, I suggest adding a simple "OpenPrintDialog" method to the API, even if it is implemented by doing exactly the same as the ExecuteScript
snippet above. The advantages of "OpenPrintDialog" are:
- Allows you to change the internal implementation in a future version of WebView2, without changing the public API. Such a change might become necessary for some reason and then you'll be glad that the change can be performed without breaking compatibility with the public API.
- Can succeed regardless of whether EcmaScript is disabled.
- Protects the public API against possible future changes in the behavior of EcmaScript.
- Although the
ExecuteScript
technique works, it is obscure and will leave people wondering/asking: How to print?
Can you make a "ClosePrintDialog" method in WebView2 that closes any print dialog/window that may be open? (Does nothing if no print dialog is currently open.) Related: If a WebView2 XAML element has it's Visibility
property set to Collapsed
, or if the WebView2 element is removed/detached from the element tree, what happens to the print dialog? If it remains visible because it's in a separate floating window, then the "ClosePrintDialog" method would be especially useful to solve problems here.
Can you make a "IsPrintDialogOpen" method that returns a boolean indicating whether the/any print dialog/window is open/displayed/visible?
Can you make an event that is triggered when the print dialog/window is closed? Ideally the event arguments would include a boolean that specifies whether or not the dialog was closed via the Cancel button/key/action. An extra bonus feature would be the inclusion of a background print job identifier in the arguments of this event.
When the user closes the print dialog by clicking the "Print"/"OK" button, is it possible to intercept the print job settings and change them and/or cancel the print job? Reasons include:
- To stop a customer's kids entering 99999 in the number of copies box, starting the print job, and running away. (Computer in a shop / public location.)
- To cancel the print job if the selected printer is a virtual printer that does print-to-file, in environment where this needs to be prevented.
Virtual printers that print to files
When using a virtual printer named "Print to PDF" or "Save as PDF", when it prompts for the file name and location, and when it runs in a UWP app, have the security rules already been tested successfully? Meaning, will it enforce the same security rules as Windows.Storage.Pickers.FileSavePicker
even when running via a separate-process mode?
In our kiosk system, we must prevent kiosk users seeing the local file system. Therefore, would it be possible to make the following boolean settings in WebView2?
- Allow/disallow physical (real) printers.
- Allow/disallow virtual printers.
Examples of the virtual printers that exist in Windows are:
- "Microsoft Print to PDF" or "Save as PDF"
- "Microsoft XPS Document Writer"
- "OneNote"
- "Send to OneNote 2016"
Alternative designs include:
- A setting "Allow/disallow print to file" instead of "Allow/disallow virtual printers".
- An option to supply a list of printers that the print dialog should be restricted to.
- An option to use an external print dialog produced by the app using WebView2, instead of the WebView2's built-in print dialog. This might be possible if WebView2 supports an equivalent of CaptureSelectedContentToDataPackageAsync except with an option to capture the entire page instead of only the selected part.
Summary
- OpenPrintDialog method that succeeds regardless of whether EcmaScript is disabled.
- ClosePrintDialog method.
- IsPrintDialogOpen boolean property.
- Event triggered when the print dialog/window is closed.
- When print dialog is closed, intercept print job and change settings and optionally cancel print job.
- Option to disable virtual printers or "print-to-file" printers.
- Ability to use an external print dialog instead of the built-in print dialog, such as via a version of
WebView.CaptureSelectedContentToDataPackageAsync
but with ability to capture entire page. - Testing of print features for compatibility with the built-in PDF viewer, not only testing with HTML pages.