当使用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(); } }; };
本文的示例可以在如下附件获取:
如果你对ComponentOne感兴趣,请到我们的官网下载最新版本:/download/?pid=6
如果你有疑问,可以到GCDN论坛获得技术支持:http://gcdn.grapecity.com.cn/showforum-138.html