How to know if the user closed Print dialog?

0 Answers 1012 Views
General Discussions
Tursunkhuja
Top achievements
Rank 2
Iron
Iron
Veteran
Tursunkhuja asked on 05 Oct 2022, 05:36 AM

Hi All,

Currently we use Telerik WinUI reportviewer and programmatically we need to print a report. We call PrintReport() method of the reportviewer control for that purpose. But, we should somehow know if the user clicked on "Cancel" button or "Print" button on the print dialog. How to subscribe to print/cancel events? or how to know if the user closed print dialog?

Any custom solution would be helpful.

Thanks,

 

Neli
Telerik team
commented on 11 Oct 2022, 09:23 AM

Hi Tursunkhuja,

Can you please provide us with more details on why you need such a functionality? What logic do you want to add? 

Note that we are working on replacing Win32 print dependencies with WinRT printing APIs in some of the upcoming releases. The latest WinUI architecture makes obsolete the usage of the DesktopBridge, so no Windows.Forms APIs will be called. Having this limitation imposed, we'll be discarding the usage of the WinForms print dialog and implement our own.

Tursunkhuja
Top achievements
Rank 2
Iron
Iron
Veteran
commented on 11 Oct 2022, 06:45 PM | edited

Hello Neli,

Before calling printing report, we open our custom modal dialog (or page) in our project. After closing the Pring dialog (after clicking Pring or Cancel button) we want to close our custom dialog (or do other logic). Can we do that programmatically?

Second question: When you expect to complete with WinRT printing?

 

 

 

Dimitar
Telerik team
commented on 14 Oct 2022, 03:49 PM

Hi Tursunkhuja,

Regarding the first question, you may use the PrintEnd event of the report viewer to execute the code that will close your dialog. However, while this event will be fired after the printing operation finishes, it will not be fired if the Print dialog is canceled.

Custom Approach - Manual

If the Cancel button also has to be supported, well it cannot be achieved with the report viewer out of the box. The only way that I can think of is to display a custom print dialog instead. You may cancel the default print operation in the PrintBegin event. This event fires as soon as the Print button on the report viewer is clicked.

After you cancel the button, you may open up your own PrintDialog over which you will have full control thus you should be able to know when it is being closed too. The closing should be the easy part in this scenario. The hard part is handling the printing. My idea is to use the ReportProcessor.PrintReport method. The PrintReport method will print the report silently so the scenario for printing should be as follows:

  1. Open the custom PrintDialog
  2. Get the printer settings
  3. Provide the settings as PrinterSetting instance to the PrintReport method

As far as I know, the WPF/WinUI PrintDialog does not have a PrinterSettings property so you will have to manually create an instance of the PrinterSettings class from the dialog's other properties or you could try to use the System.Windows.Forms.PrintDialog.

Example of how the printing could happen:

            var dialog = new System.Windows.Forms.PrintDialog();

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
         
                var proc = new ReportProcessor();
                proc.PrintReport(new TypeReportSource() { TypeName = typeof(Dashboard).AssemblyQualifiedName }, dialog.PrinterSettings);
            }

Feature Request Update

Regarding the second question, currently, we have no new information to share about migrating to the WinRT APIs, the Feature Request will get updated when there is any new news, as of right now it is "unplanned" because we are still investigating whether it will be possible or not.

Until we have an update on this, please keep using the existing printing functionality.

Tursunkhuja
Top achievements
Rank 2
Iron
Iron
Veteran
commented on 07 Feb 2023, 10:09 AM

Hi Dimitar,

I was trying to use the System.Windows.Forms.PrintDialog. In our client app, we have Telerik WinUI reportviewer and this reportviewer connects to Reporting Rest service (reporting engine). If we use ReportProcessorPrintReport() method requires ReportSource. In this case how we should set ReportSource? and where we should set Reporting service connection?

Thanks,

Dimitar
Telerik team
commented on 10 Feb 2023, 08:57 AM

Hi Tursunkhuja,

Indeed, the PrintReport(ReportSource, PrinterSettings) method does require ReportSource and PrinterSettings objects as arguments.

If you are using the approach with the PrintBegin event, the ReportSource of the report viewer can be retrieved from the sender object, for example:

        private void ReportViewer_PrintBegin(object sender, Telerik.ReportViewer.Common.PrintBeginEventArgs args)
        {
            var rv = (Telerik.ReportViewer.WinUI.ReportViewer)sender;
            var rs = rv.ReportSource;
            ...
        }

Regarding the Reporting REST Service question, the ReportProcessor does not need a reporting service to print/render a report. When you call the ReportProcessor to print the report, this operation will be performed in the application where the code is invoked, in this case, the WinUI application, not where the reporting service is.

Since the code will be executed in the WinUI application, you should make sure that any connection strings or object data source/user functions references that the reports use are available in the WinUI application's configuration file. 

Please let me know if you have any further questions.

No answers yet. Maybe you can help?

Tags
General Discussions
Asked by
Tursunkhuja
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or