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.
I've tested your suggested modification but the Action() is never executed; changing the priority has no effect either...
private void PrintReport(object dummy) { IGReports repPrintView = new IGReports(@".\ReportByIG.igr"); // Ohne Daten würde ein "leerer" Report angezeigt... ReportByIG_Daten.RepDaten = RepAdressen; ReportByIG_Daten.RepParameter = RepParameter; XamReportViewer repPrint = repPrintView.xamReportViewer1; repPrint.ProcessingCompleted +=
new EventHandler<Infragistics.Reports.ProcessingCompletedEventArgs>
(repPrint_ProcessingCompleted); repPrintView.Show(); } void repPrint_ProcessingCompleted(object sender, Infragistics.Reports.ProcessingCompletedEventArgs e) { XamReportViewer repPrint = sender as XamReportViewer; System.Windows.Threading.Dispatcher.CurrentDispatcher.
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,
Infragistics.Controls.Reports.PageRange.All); } }), System.Windows.Threading.DispatcherPriority.Normal, null); }
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