I want to print a report without any preview or action on the screen (due to automatic printouts without any needed/wanted user actions).
I have an external defined report file (.igr) and an class which implements GetDataSource() to provide the data for the report.
I can display the report using a XamReportViewer.
I tried the following code:
void repPrint_ProcessingCompleted(object sender, Infragistics.Reports.ProcessingCompletedEventArgs e) { XamReportViewer repPrint = sender as XamReportViewer; if (repPrint != null) { LocalPrintServer localPrtSrv = new LocalPrintServer(); PrintQueue prtQueue = localPrtSrv.DefaultPrintQueue; Infragistics.Controls.Reports.PageSettings curPageSettings =
repPrint.GetCurrentPageSettings(); Infragistics.Controls.Reports.PaperSettings paperSettings = new Infragistics.Controls.Reports.PaperSettings(curPageSettings.PaperSize,
curPageSettings.PageOrientation); repPrint.Print(paperSettings, prtQueue,
Infragistics.Controls.Reports.PageRange.All);
}
But this has two problems:
- The report is shown on the screen
- There is an error when calling repPrint.Print() (threading-problem):
Infragistics.Reports.Engine.EngineException: An error occurred while
processing the report.Exception Message:Der aufrufende Thread kann nicht auf dieses Objekt zugreifen,
da sich das Objekt im Besitz eines anderen Threads befindet.Stack Trace: bei System.Windows.Threading.Dispatcher.VerifyAccess() bei System.Windows.DependencyObject.GetValue(DependencyProperty dp) bei System.Windows.VisualStateManager.GetVisualStateGroupsInternal(FrameworkElement obj) bei System.Windows.VisualStateManager.GoToStateCommon(FrameworkElement control, FrameworkElement stateGroupsRoot, String stateName, Boolean useTransitions) bei System.Windows.VisualStateManager.GoToState(FrameworkElement control, String stateName, Boolean useTransitions) bei Infragistics.Controls.Reports.XamReportViewer.SetVisualState(String state) bei Infragistics.Controls.Reports.ViewerStateMachine.ReportPrintServicePrintFinished(Object sender, PrintServiceFinishedEventArgs e) bei Infragistics.Controls.Reports.ProcessingSessionStateMachine.ReportPrintServicePrintFinished(Object sender, PrintServiceFinishedEventArgs e) bei Infragistics.Controls.Reports.ReportPrintService.RaisePrintFinishedEvent(Exception error) bei Infragistics.Controls.Reports.ReportPrintService.PrintHelper(PageSettingsData pageSettingsData, PrintTicket printTicket, PrintQueue printer, PageRange range) bei Infragistics.Controls.Reports.ReportPrintService.Print(PageSettingsData pageSettingsData, PaperSettings paperSettings, PrintQueue printer, PageRange range) bei Infragistics.Controls.Reports.ProcessingSessionStateMachine.Print(PageSettingsData currentPageSettingsData, PaperSettings paperSettings, PrintQueue printer, PageRange range) bei Infragistics.Controls.Reports.ViewerStateMachine.Print(PaperSettings paperSettings, PrintQueue printer, PageRange range) bei Infragistics.Controls.Reports.XamReportViewer.Print(PaperSettings paperSettings, PrintQueue printer, PageRange range) bei IG_Reporting.MainWindowViewModel.repPrint_ProcessingCompleted(Object sender, ProcessingCompletedEventArgs e) in D:\Projekte(Lokal)\VS2010Prj\IG-Reporting\IG-Reporting\MainWindowViewModel.cs:Zeile 59.
WHAT IS THE CORRECT WAY TO PRINT AN .IPR-REPORT
WITHOUT DISPLAYING ANYTHING???
Hello,
Thank you for your post. I have been looking into it and I suggest you use a Dispatcher in order to delay a little the Print method, so the Report can be rendered. You can do something like this:
XamReportViewer repPrint = sender as XamReportViewer; Dispatcher.BeginInvoke(new Action(() => { if (repPrint != null) { LocalPrintServer localPrtSrv = new LocalPrintServer(); PrintQueue prtQueue = localPrtSrv.DefaultPrintQueue; Infragistics.Controls.Reports.PageSettings curPageSettings = repPrint.GetCurrentPageSettings(); Infragistics.Controls.Reports.PaperSettings paperSettings = new Infragistics.Controls.Reports.PaperSettings(curPageSettings.PaperSize, curPageSettings.PageOrientation); repPrint.Print(paperSettings, prtQueue, Infragistics.Controls.Reports.PageRange.All); } }), System.Windows.Threading.DispatcherPriority.Background, null);
Also I can say that you are not able to print a report without showing it on the ReportViewer, because the Report should be rendered before it can be printed. Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.
Dear Stefan,
thanks for your suggested solution!
I didn't tried it yet since it is not really what I want...
I think you can imagine that there is a need for printing automated reports without displaying them!
There MUST be a way to do this, each solution to this problem is welcome; otherwise I have to
go back to our current reporting tool (Crystal Reports) which is a little bit outdated...
Thanks for a comment!
Bernd
Hi Bernd,
As Stefen said, the report needs to be rendered before sending it to the printer. You may hide the viewer by using its Visibility="Collapsed" property but it should be in the visual tree.
There is also another way of rendering them without having to use it at all, through the Report Exporter. In this case you can programmatically export the reports (to pdf for example) and then just send the files to the printer as would normally do with any file. Notice this method is less efficient than sending it directly and requires temp storage of these files, but depending on your particular scenario it may be considered acceptable.
Here is a topic on how to use the Report Exporter and here is another thread that might be useful, it uses the exporter to programmatically email reports. In your case you would be instantiating the printer (LocalPrintServer) instead, as shown before. Here is also a link that shows how to programmatically load and print xps files as a guide.
I hope these alternatives work better for what you are trying to achieve.
Regards,
Miguel.
Hi,
the Code from above with the CurrentDispatcher didn't work for me either.
When I use the Dispatcher of the report instead of the Current it worked.
XamReportViewer repPrint = sender as XamReportViewer; if (repPrint != null) repPrint.Dispatcher.BeginInvoke( System.Windows.Threading.DispatcherPriority.Normal, new Action( repPrint.PrintWithDialog ));
Hello Bernd,
I have created a support ticket on your behalf: CAS-80968-LLMD6D. In the CHS you can send me your sample and I will investigate it further for you.
Hi MIguel,
thanks for your suggestions!
I checked the provided links but I can't get the solution from this, sorry...
I've a really small VS2010/C# WPF-Project with no external dependencies for demonstrating my "problem".
May I ask you to have a look at this solution (I can mail a zip to you) ?
My direct email is bb(at)ggse.de
Best regards,