Display the Printing status for WPF C1Reports

In this blog, I am providing a small utility code to capture whether the Printing has been completed or Cancelled while printing a WPF C1Report. With small code blocks, it is easy to understand and implement. So let us quickly run through the implementation.

Load C1Report

We begin the implementation by loading any report in C1Report control. For this blog, we need the CommonTasks.xml and C1Nwind.mdb placed on the desktop. Else you would require modifying the connection path in CommonTasks.xml as installed on your machine.

C1.C1Report.C1Report c1Rpt;  
void MainWindow_Loaded(object sender, RoutedEventArgs e)  
  string fileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\\CommonTasks.xml";  
  string reportName = "01: Alternating Background (Greenbar report)";  

  c1Rpt = new C1.C1Report.C1Report();  
  c1Rpt.Load(fileName, reportName);  

  c1Rpt.Document.PrintPage += Document_PrintPage;  
  c1Rpt.Document.EndPrint += Document_EndPrint;  

  c1DViewer.Document = c1Rpt.C1Document.FixedDocumentSequence;  

You would observe that two events PrintPage and EndPrint for PrintDocument object have been attached. These two events are the working region to trap and determine whether printing has been completed or not.

Trap and Determine Printing status

Given code blocks are easy to understand. PrintPage event fires for every page. Its event argument object has property 'HasMorePages' which determine if there are further pages waiting to be printed. This boolean value is stored in a temporary variable. So if at any point of time, if the printing is cancelled in the middle, value for this boolean varible would be true. This will determine whether printing has been cancelled or completed. Lets see the code snippets below.

bool printStatus = false;  

void Document_PrintPage(object sender, PrintPageEventArgs e)  
   printStatus = e.HasMorePages;  

void Document_EndPrint(object sender, PrintEventArgs e)  
   if (printStatus)  
     MessageBox.Show("Printing Cancelled");  
     MessageBox.Show("Printing Completed");  

That's it !!! This ends the implementation. Refer to the attached sample application for complete implementation. Download Sample


GrapeCity Developer Tools
comments powered by Disqus