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???
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,
Bernd
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); }
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.