Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
320
print external ".igr"-Report without displaying anything with a WPF application
posted

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???
Parents
  • 138253
    Offline posted

    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.

Reply Children