Skip to main content Skip to footer

Custom PrintDialog in WPF C1DocumentViewer

When document with landscape layout is printed on a physical printer using the C1DocumentViewer, it is always printed as portrait. The orientation can only be changed manually when user selects the print orientation as Landscape in the printdialog. The issue is mainly because of the inherent MS DocumentViewer and there aren't a lot of options to customize the default printdialog that pops up when print button is clicked. However, no issue is observed while printing on XPS printer. This article discusses the workaround for the above issue. The solution is to handle the click event of the C1DocumentViewer manually and create a custom printdialog with required settings there. For adding an event to the default 'Print' button, you can either edit the default template and add and event there or inherit the control and do the required stuff there itself. The next code snippets creates a custom C1DocumentViewer and exposes the PrintButton :


public class MyDocumentViewer : C1.WPF.C1Report.C1DocumentViewer  
 {  
 private Button printbtn;  
 public Button PrintButton  
  {get { return printbtn;}}  

 public override void OnApplyTemplate()  
  {  
   base.OnApplyTemplate();  
   var cc = (((VisualTreeHelper.GetChild(this, 0) as Border).Child as Grid).Children[0] as ContentControl);  
   cc.ApplyTemplate();  
   printbtn = ((VisualTreeHelper.GetChild(cc, 0) as ToolBar).Items[0] as Button);  
   printbtn.Command = null;  
  }  
}

You can now add a click event to the default Print button and create your custom print dialog there :

c1dv.Loaded += (s, e) =>  
 {  
  c1dv.ApplyTemplate();  
  c1dv.PrintButton.Click += (s1, e1) =>  
   {  
     System.Windows.Forms.PrintDialog pd = new System.Windows.Forms.PrintDialog();  
     pd.Document = rpt.Document;  
     PrinterSettings myPrinterSettings = new PrinterSettings();  
     myPrinterSettings.DefaultPageSettings.Landscape = true;  

     if ((rpt.Layout.Orientation == OrientationEnum.Landscape))  
      {  
        pd.PrinterSettings.DefaultPageSettings.Landscape = true;  
      }  
      else  
      {  
        pd.PrinterSettings.DefaultPageSettings.Landscape = false;  
      }  

      if ((pd.ShowDialog() == System.Windows.Forms.DialogResult.OK))  
      {  
        pd.Document.Print();  
      }  
   };  
 };

Download the attached sample with complete implementation. Download Sample

MESCIUS inc.

comments powered by Disqus