Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Printing in WinUI 3 Desktop #4419

Open
bridgesquared opened this issue Mar 4, 2021 · 43 comments
Open

Question: Printing in WinUI 3 Desktop #4419

bridgesquared opened this issue Mar 4, 2021 · 43 comments
Labels
appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps product-winui3 WinUI 3 issues question

Comments

@bridgesquared
Copy link

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:

  1. Is the PrintManager + PrintDocument combination going to be the preferred printing technique for WinUI 3 Desktop (as per UWP)
  2. If not what is plan for WinUI 3 Desktop?
  3. Is there a vision of when the capability will be available? there's nothing Print specific on the roadmap at the moment
  4. 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)

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Mar 4, 2021
@FreddyDgh
Copy link

I agree that at least adding printing APIs to the roadmap would be very helpful to plan for WinUI 3.0 adoption.

@StephenLPeters
Copy link
Contributor

@MikeHillberg and @codendone do you have any insights here?

@StephenLPeters StephenLPeters added appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps product-winui3 WinUI 3 issues and removed needs-triage Issue needs to be triaged by the area owners labels Mar 5, 2021
@XamDR
Copy link

XamDR commented Mar 5, 2021

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.

@JohnnyWestlake
Copy link

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.

@MikeHillberg
Copy link
Contributor

MikeHillberg commented Mar 18, 2021

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.

@bridgesquared
Copy link
Author

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

@alexdi220
Copy link
Contributor

Hi, any updates on this, or is there any workaround? At now we try to find a way to print documents in our application

@BarcodeResource
Copy link

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.

@MartinRothschink
Copy link

Windows App SDK is now 1.0 stable and this bug still prevents printing (and using PrintHelper from Community Toolkit).
Any news?

@kmgallahan
Copy link
Contributor

@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

@MartinRothschink
Copy link

Thanks, voted.

@NoI3ody
Copy link

NoI3ody commented Feb 5, 2022

Still no updates?

@Wandtket
Copy link

Working with WinUI and still waiting on PrintDocument to be updated to WinUI.

@ezexe
Copy link

ezexe commented Apr 13, 2022

same

@castorix
Copy link

I just tested (Windows 10 21H1) and PrintDocument works :

WinUI_Printing

@NoI3ody
Copy link

NoI3ody commented Apr 18, 2022

I just tested (Windows 10 21H1) and PrintDocument works :

WinUI_Printing

can you maybe share your code for your testproject, so we can investigate what we maybe do wrong?

@castorix
Copy link

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)

WinUI3_Print

@hgkujawa
Copy link

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.
image

@castorix
Copy link

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"

@NoI3ody
Copy link

NoI3ody commented Apr 27, 2022

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

@hgkujawa
Copy link

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.

@castorix
Copy link

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 :

PrintPreview_Custom

@krschau
Copy link
Contributor

krschau commented Jun 15, 2022

@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.

@Redp87
Copy link

Redp87 commented Jun 16, 2022

Getting System.ArgumentException: 'Value does not fall within the expected range.'
On this line :
await PrintManagerInterop.ShowPrintUIForWindowAsync(hWnd)

@FreddyDgh
Copy link

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?

@Redp87
Copy link

Redp87 commented Jun 16, 2022

Yes I'm on Windows 10 when I get this error.

@marb2000
Copy link
Contributor

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.

@NoI3ody
Copy link

NoI3ody commented Jun 18, 2022

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.

@Redp87
Copy link

Redp87 commented Jun 21, 2022

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.

@krschau
Copy link
Contributor

krschau commented Jun 22, 2022

@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.

@marb2000
Copy link
Contributor

And following @krschau comments - Given the OS service updates process, I don't expect we can make it before September 2022.

@FreddyDgh
Copy link

@marb2000 Thanks, that info is very helpful for planning.

@Tursunkhuja
Copy link

Hi,
Any updates @marb2000 @krschau ?

@Wandtket
Copy link

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.

@KieferAtkin
Copy link

Any Updates?

@Armorost
Copy link

Hi. Any solutions ? Thanks

@sethuramkumar
Copy link

@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)

@Marv51
Copy link
Contributor

Marv51 commented Mar 22, 2023

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.

@rirufa
Copy link

rirufa commented May 8, 2023

I tested in windows10(19045.2846,22H2). It show print preview dialog.

@Tursunkhuja
Copy link

Is this issue still open? Any updates on this?

@Tursunkhuja
Copy link

Tursunkhuja commented Jun 7, 2023

@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

@GuildOfCalamity
Copy link

GuildOfCalamity commented Jun 30, 2023

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.
image

@emyfreya
Copy link

emyfreya commented Jul 27, 2023

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 KapseoPlus.App.exe: WinRT originate error - 0x80010115 : 'OLE a envoyé une demande et attend la réponse. (0x80010115)'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
appModel-win32 Exclusive to WinUI 3 Win32 Desktop apps product-winui3 WinUI 3 issues question
Projects
None yet
Development

No branches or pull requests