Open bridgesquared opened 3 years ago
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.
And following @krschau comments - Given the OS service updates process, I don't expect we can make it before September 2022.
@marb2000 Thanks, that info is very helpful for planning.
Hi, Any updates @marb2000 @krschau ?
Hi, Any updates @marb2000 @krschau ?
I ended up going with the method of creating an HTML document template that resembles a word document then modifying it using HTML agility pack and then opening it in Webview2 and calling the print function. The best part about this approach is that it also works on .net MAUI.
Any Updates?
Hi. Any solutions ? Thanks
@marb2000 & @krschau we are planning to integrate the below approach for printing in our application. Is the approach provided here stable?
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
printMan = PrintManagerInterop.GetForWindow(hWnd);
await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd)
Sounds like this might be (finally) coming to Windows 10 in 19045.2788, which is currently in preview.
I have not tested printing there yet, but it is mentioned in the release notes and also here
This update affects applications that use the Windows UI Library in the Windows App SDK (WinUI 3). It makes printing for them possible on Windows 10 devices.
-- Note: I do not work for Microsoft and I am not affiliated with the WinUI / WinAppSDK in any way.
I tested in windows10(19045.2846,22H2). It show print preview dialog.
Is this issue still open? Any updates on this?
@marb2000 @krschau With the provided solution (or workaround), is it possible to handle Print dialog close event? Because I want to run some code when user presses print & cancel buttons of print dialog.
@sethuramkumar
This is still an issue in the latest WinAppSDK 1.3.23x NOTE: On my home, non-domain machine I do see the print dialog. However on my work machine, which is joined to a domain, I get the below exception. I do have the proper rights/permissions to print.
Printing doesn't work with EntryPoint=Windows.PartialTrustApplication
and without the <rescap:Capability Name="runFullTrust" />
.
I'm working with an old UWP app that needs converting, and I cannot add the runFullTrust
capability.
I'm trying to look if it's a capability missing, but no clue as of now because no exception or error are happening.
Edit I am getting the exception
Exception thrown at 0x00007FF8E3034B2C (KernelBase.dll) in App.exe: WinRT originate error - 0x80010115 : 'OLE a envoyé une demande et attend la réponse. (0x80010115)'.
I get the same error. Summarising the previous comment - it looks like printing is not possible in WinUI 3 desktop apps today unless they are also declare runFullTrust
. That makes them less secure and more difficult to publish to the Microsoft Store (very difficult?). Like the previous commenter, I'm trying to port a UWP Microsoft Store app to WinUI3. Thought I was nearly there! Maybe this can be re-categorised as an issue not a question?
I realize that few people still print to paper, but many of us need the ability to print to PDF or the like. I wish someone would fix this, not sure how this feature was even tested properly at MS?
I get the same error. Summarising the previous comment - it looks like printing is not possible in WinUI 3 desktop apps today unless they are also declare
runFullTrust
. That makes them less secure and more difficult to publish to the Microsoft Store (very difficult?). Like the previous commenter, I'm trying to port a UWP Microsoft Store app to WinUI3. Thought I was nearly there! Maybe this can be re-categorised as an issue not a question?
@DaveAurionix I wouldn't consider it a workaround but an overall better solution than to use the built in printer is to use WebView2 and HTMLAgilityPack to make HTML printouts, they're instant to load and work cross platform on any app framework.
I cannot believe this is an issue, especially for this long. WinUI will never be able to compete with WinForms for business applications if you cannot print a SfDataGrid, for example. Our progress on our application just came to a complete stop thanks to this.
I cannot believe this is an issue, especially for this long. WinUI will never be able to compete with WinForms for business applications if you cannot print a SfDataGrid, for example. Our progress on our application just came to a complete stop thanks to this.
In desktop apps, any control can be printed (PrintManager or Win32 methods (GDI, GDI+, Direct2D))
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 aSystem.Runtime.InteropServices.COMException: 'Interface not registered (0x80040155)'
I put this down to issues with getting theWindows.Graphics.Printing.PrintManager
as reported in #4375 and #4363.So I suppose I'm trying to answer a few questions:
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)