microsoft-ui-xaml
microsoft-ui-xaml copied to clipboard
Question: Printing in WinUI 3 Desktop
Hi all
I'm looking for guidance on printing from WinUI 3 Desktop please.
I tried the PrintHelper
from Community Toolkit 8 preview 4 and get a System.Runtime.InteropServices.COMException: 'Interface not registered (0x80040155)'
I put this down to issues with getting the Windows.Graphics.Printing.PrintManager
as reported in #4375 and #4363.
So I suppose I'm trying to answer a few questions:
- Is the PrintManager + PrintDocument combination going to be the preferred printing technique for WinUI 3 Desktop (as per UWP)
- If not what is plan for WinUI 3 Desktop?
- Is there a vision of when the capability will be available? there's nothing Print specific on the roadmap at the moment
- Has anyone found workarounds/solutions to getting printing working in Preview 4?
I would welcome any thoughts, pointers etc. please. I know there's a huge amount of working going on to get the controls working properly, but Print has just got to the top of my stack and I'm a bit worried that it has fallen through the cracks which is going to cause issues as I can't ship without it.
BTW. We do have some fallback ideas, maybe WebView HTML generation or PDF generation, but to be honest I rather liked the UWP approach to printing and really hope it makes it through to WinUI 3 Desktop.
Thanks for your time, and all your hard work. I do think this unified approach is the right way to go and I can't wait to get away from NET Native and shipping modern apps with up-to-date backing tech (NET5/6 + WinUI)
I agree that at least adding printing APIs to the roadmap would be very helpful to plan for WinUI 3.0 adoption.
@MikeHillberg and @codendone do you have any insights here?
Thank your for opening this issue. I myself planned to do it some time ago, but back then I was busy and I forgot about it. AFAIK, there is the interface IPrintManagerInterop which has the methods: GetForWindow
and ShowPrintUIForWindowAsync
, which can take hwnds as parameters. Also, according to the docs, these are for desktop apps only. There is also this managed wrapper I found sometime ago, but if I recall correctly I was unable to make it work in a demo WPF app.
If it is the intended default, I hope the API sees improvements to large document handling. Printing a 100+ page document quickly leads to out-of-memory scenarios in UWP as you need to render all the pages up front and in-memory in XAML before handing off to be printed. Some form of "on demand" callback would help work around that.
I'm sorry, the PrintDocument API hasn't been updated for WinUI3 yet, and it's an accident that the API is showing up right now. I'll look into the roadmap, but it's not something we're actively working on yet.
@JohnnyWestlake, you should be able to hand off a page at a time rather than all 100 at once. After each call to PrintDocument.AddPage you don't need the keep the pageVisual
around.
I'm sorry, the PrintDocument API hasn't been updated for WinUI3 yet, and it's an accident that the API is showing up right now. I'll look into the roadmap, but it's not something we're actively working on yet.
Hi @MikeHillberg, do you have an update on where printing sits in the roadmap please. I really need to get some printed output from a Win UI 3 (Reunion 0.5) Desktop app and would appreciate guidance from the team please. Thanks
Hi, any updates on this, or is there any workaround? At now we try to find a way to print documents in our application
Hi @MikeHillberg, We are testing a port of an app from UWP to WinUI Desktop. PrintManager is also what we require too. It will be great to let us know what the future guidance for this is. Thanks.
Windows App SDK is now 1.0 stable and this bug still prevents printing (and using PrintHelper from Community Toolkit). Any news?
@MartinRothschink et al.
You can vote on this and other things you want here:
https://portal.productboard.com/winappsdk/1-windows-app-sdk/c/50-support-printmanager-api
Thanks, voted.
Still no updates?
Working with WinUI and still waiting on PrintDocument to be updated to WinUI.
same
I just tested (Windows 10 21H1) and PrintDocument works :
I just tested (Windows 10 21H1) and PrintDocument works :
can you maybe share your code for your testproject, so we can investigate what we maybe do wrong?
can you maybe share your code for your testproject, so we can investigate what we maybe do wrong?
I made a test sample : WinUI3_Print.zip (Windows App SDK 1.1.0-preview1, .NET 6, VS 2022, Windows 10 21H1)
Sorry this stupid question, but I am not very experienced with VS. I tried to re-compile this project using VS2022 and I've got some errors "spacename Forms not found" while calling PrintDialog. Similarly, I've got an error with System.Drawing.Common, this could be resolved by installing a package via NuGet-PackageManager. For the Forms I cannot find any source and the assembly is marked with warn sign. Some SDKs are also marked with the warn sign, see below.
I would appreciate any help.
Sorry this stupid question, but I am not very experienced with VS. I tried to re-compile this project using VS2022 and I've got some errors "spacename Forms not found" while calling PrintDialog. Similarly, I've got an error with System.Drawing.Common, this could be resolved by installing a package via NuGet-PackageManager. For the Forms I cannot find any source and the assembly is marked with warn sign. Some SDKs are also marked with the warn sign, see below. I would appreciate any help. !
I have put in comments (at beginning of MainWindow.xaml.cs) the links to the 3 Assemblys to add, depending on .NET 6 version For example, I had .NET 6.0.3 and now I have .NET 6.0.4 So I must remove the 3 Assemblys ([Right-click][Remove] on each) and add them ([Dependencies][Right-click][Add COM Reference...][Browse] from : "C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.4\System.Drawing.Common.dll" "C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.4\System.Windows.Forms.dll" "C:\Program Files (x86)\dotnet\shared\Microsoft.WindowsDesktop.App\6.0.4\System.Resources.Extensions.dll"
Could get it to work with your example @castorix, but with the PrintHelper from the Windows Community Toolkit, I still get the same error as the @bridgesquared
Ok, I've got it working, but... these are old-style dialogs, which I do not really want in my WnUI3(!) app 😊 So I will rather wait, until the PrintManager is working, similarly to UWP apps.
Ok, I've got it working, but... these are old-style dialogs, which I do not really want in my WnUI3(!) app 😊 So I will rather wait, until the PrintManager is working, similarly to UWP apps.
Instead of default dialogs, you can use your own windows Test with random controls + PrintPreviewControl :
@marb2000 has an app with a printing sample you can find here: https://github.com/marb2000/PrintSample
Generally, *ForCurrentView() methods don't work in WinUI 3 desktop apps, which don't have a core window. Instead, there is a COM interop you can use: IPrintManagerInterop
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
printMan = PrintManagerInterop.GetForWindow(hWnd);
await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd)
This should work in WinAppSDK version 1.1.1 and downlevel.
We will update documentation to reflect this.
Getting System.ArgumentException: 'Value does not fall within the expected range.' On this line : await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd)
Getting System.ArgumentException: 'Value does not fall within the expected range.' On this line : await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd)
The sample works for me on Windows 11 (21H2), but gives me the same System.ArgumentException
as above when running on Windows 10 (21H2). Is printing support intended to be for Windows 11 only?
Yes I'm on Windows 10 when I get this error.
Nop. It should be working on Windows10 too. My team is investigating why these APIs don't work on Win10 given they were there since long time ago. Maybe there is some piece of the puzzle that we lost.
had to change the first line, instead of using "this" i used "App.StartupWindow"
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(App.StartupWindow); printMan = PrintManagerInterop.GetForWindow(hWnd); await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd)
At least the printUI does show up, but i got some problems with my dispatcher now... Made my repo public, maybe someone can investigate this. Note I am a beginner.
So I was trying to print with System.Windows.Forms.PrintDialog and when I try to render a UIElement (page to print) to bitmap with RenderTargetBitmap I get the same error : Value does not fall within the expected range, in renderBitmap.GetPixelsAsync(). So I think the problem is in the WinUI UIElements. Hope this can pinpoint the problem and you guys can help solve it.
@Redp87 @NoI3ody @FreddyD-GH It looks like there is a bug in Windows 10 (internal link) that's preventing the IPrintManagerInterop solution from working. We're investigating servicing the fix downlevel, and will update here when we know more about that.