WPF平台的C1DocumentViewer自定义打印对话框

发布时间:2015/01/27 00:01 发布者:alice

返回博客中心

当使用C1DocumentViewer在物理打印机上横向打印文档,它总是保持原来的竖向打印。只能在打印对话框中手动的选择打印方向,这样改变方向。之所以会这样,主要是因为它继承于MS DocumentViewer而且当单击打印按钮弹出的打印对话框,并没有选项去自定义默认的打印对话框。然而,在XPS打印机打印的时候没有问题。

本文讨论上述问题的背景。对于此类问题的解决方案,就是手动处理C1DocumentViewer 的click事件,和创建需要设置的自定义打印对话框。为此,添加一个事件给默认的打印按钮,你也可以编辑默认的模板和继承控件添加事件处理需求。

下面的代码片断创建了自定义C1DocumentViewer 和暴露一个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;
  }
}

 

你可以添加一个click 事件在默认的Print按钮,然后在这里创建你的自定义打印对话框:

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();
      }
   };
 };

 

本文的示例可以在如下附件获取:

WpfDVPrinting.zip (9.67 kb)

 

如果你对ComponentOne感兴趣,请到我们的官网下载最新版本:/download/?pid=6

如果你有疑问,可以到GCDN论坛获得技术支持:http://gcdn.grapecity.com.cn/showforum-138.html


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网