Open XamDR opened 4 years ago
Great suggestion! I'm not an expert in printing though, and will have to do some digging first. Do you happen to know of any existing libraries in this regard?
I was searching last couple of days about how to implement a custom print dialog, and I only found this GitHub repo, but unfortunately there is no source code, only the .dll. I also found these posts on SO:
After reading those posts, I think there are three possible approaches in order to create a modern Print Dialog:
The standard PrintDialog that WPF uses is just a wrapper around the native PrintDialog that Windows uses. According to this article it's possible to extend and modify the common dialogs. Although the article is for Winforms, it could be adapted to WPF. Since we're talking about a completely different UI for the PrintDialog, not just about adding an extra button or combobox, the main con of this approach is that it would require a lot of P/Invoke.
Create a PrintDialog using a Window control under the hood. Designing the UI would be relatively easy since only comboboxes, texboxes and numberboxes are needed. On the other hand, for the print preview functionality, it could be possible to use a DocumentViewer
, but for this last control it would be necessary a modern style (this on its own has some value). The problem would be the code needed to encapsulate all the printing functionality.
According to the docs it is possible to use WinRT APIs from desktop apps, in particular from WPF. In this blogpost the autor claims that is only necessary to include a reference to the Nuget package Microsoft.Windows.SDK.Contracts. Using this approach would allow to have the classes related to printing functionality: PrintDocument
and PrintManager
available to WPF apps. In this case it would be necessary to create some sort of helper class similar to the PrintHelper available in the Windows Community Toolkit. Not only because this class would encapsulate the low level details of interop between WinRT and WPF, but also because printing using those APIs is not straightforward and requires some lengthy code (just check this doc), so it would be nice to encapsulate as much code as possible and expose a simple interface for client code.
It seems that the printing APIs from WinRT aren't available to WPF. According to this blog post the classes in the namespace Windows.Graphics.Printing
* aren't supported. This rules out option 3, and since option 1 is just too much work for, most likely, a uggly UI as result, the only choice would be to follow option 2. Key classes: PrintTicket
, PrintQueue
, PrintCapabilities
in the System.Printing
namespace (ReachFramework assembly).
Thanks for the info. This will be a priority after 0.9 is released.
Nice to see you're planning to develop a modern Print Dialog in the near future. I myself have explored the option 2 and I've made something like this:
@XamDR Does is support printing Xps / Pdf documents, plus will you upload the code ? Thank You !
Summary Currently the only
PrintDialog
that WPF has is the standard one and it doesn't look modern compared to thePrintDialog
that UWP apps use. It would be great if WPF could have a better, more modernPrintDialog
. Something like the following image:Rationale The WinRT API exposes a
PrintManager
class that allows to UWP apps to use a modernPrintDialog
, which also has a better UX because it integrates print preview functionality out of the box. In comparison, thePrintDialog
that WPF apps can use it's the classic one, which looks out of place when used in conjunction with this library. Besides, since printing is a basic requirement in many applications, the choice of having a modernPrintDialog
available for WPF apps would allow to implement printing with better UI and UX way simpler for many devs.