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

当使用C1DocumentViewer在物理打印机上横向打印文档,它总是保持原来的竖向打印。只能在打印对话框中手动的选择打印方向,这样改变方向。之所以会这样,主要是因为它继承于MS DocumentViewer而且当单击打印按钮弹出的打印对话框,并没有选项去自定义默认的打印对话框。然而,在XPS打印机打印的时候没有问题。本文讨论上述问题的背景。对于此类问题的解决方案,就是手动处理C1DocumentViewer 的click事件,和创建需要设置的自定义打印对话框。为此,添加一个事件给默认的打印按钮,你也可以编辑默认的模板和继承控件添加事件处理需求。

发布于 2015/01/27 00:00

ComponentOne Enterprise

当使用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

ComponentOne Enterprise | 下载试用

ComponentOne 是一套专注于企业 .NET开发、支持 .NET Core 平台,并完美集成于 Visual Studio 的第三方控件集,包含 300 多种 .NET开发控件,提供表格数据管理、数据可视化、报表和文档、日程安排、输入和编辑、导航和布局、系统提升工具等七大功能,被誉为“.NET开发的‘瑞士军刀’”。

ComponentOne 为您提供专业的产品咨询服务,并由技术支持工程师为您1对1解答。>> 发帖提问

相关产品
推荐相关案例
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态