zoukankan      html  css  js  c++  java
  • DevExpress GridControl使用教程:打印详解

    通过几天的学习,终于对打印有了初步的认识,并实现了打印功能,下面将详细介绍下如何使用DevExpress下的打印功能。

    首先要实现打印功能必须添加一个引用:DevExpress.XtraPrint

    实现打印需要用到的主要的类:

    1. public class PrintingSystem : Component, ISupportInitialize,IPrintingSystem,IAccessible,IExporter,IPrintingSystemContext

    该类负责提供生成报表的一些方法,还提供了对页面进行设置和打印报表的一些函数,这个类的属性包含了访问其他管理报表的对象,如Document类、BrickGraphics类(在报表上进行画图的类)、XtraPageSettings类(提供对打印页面进行设置的操作)

    2. public class PrintableComponentLink : Link

    该类的功能是对实现DevExpress.XtraPrinting.IPrintable接口的所有控件进行打印,它提供了一些基本的生成和打印报表的功能。在生成报表以后可以通过PrintingSystem将报表打印出来。

    通过以上这两个类就能完全实现各种控件的打印了,只要一个控件实现了DevExpress.XtraPrinting.IPrintable这个接口就能够对该控件进行打印,如GridControl,CharControl等等,下面给出具体的实现方法:

    public class PrintSettingController
    {
    PrintingSystem ps = null;
    string formName = null;
     
            PrintableComponentLink link = null;
     
            string _PrintHeader = null;
    /// <summary>
    /// 打印时的标题
    /// </summary>
    public string PrintHeader
    {
    set
    {
    _PrintHeader = value;
    }
    }
     
            string _PrintFooter = null;
    /// <summary>
    /// 打印时页脚
    /// </summary>
    public string PrintFooter
    {
    set
    {
    _PrintFooter = value;
    }
    }
     
            bool _landScape;
    /// <summary>
    /// 页面横纵向
    /// </summary>
    public bool LandScape
    {
    set { _landScape = value; }
    }
     
            System.Drawing.Printing.PaperKind _paperKind;
    /// <summary>
    /// 纸型
    /// </summary>
    public System.Drawing.Printing.PaperKind PaperKind
    {
    set { _paperKind = value; }
    }
     
            /// <summary>
    /// 打印控制器
    /// </summary>
    /// <param name="control">要打印的部件</param>
    public PrintSettingController(IPrintable control)
    {
    if (control == nullreturn;
    Control c = (Control)control;
    formName = c.FindForm().GetType().FullName + "." + c.Name;
    ps = new DevExpress.XtraPrinting.PrintingSystem();
    link = new DevExpress.XtraPrinting.PrintableComponentLink(ps);
    ps.Links.Add(link);
    link.Component = control;
    }
     
            /// <summary>
    /// 打印预览
    /// </summary>
    public void Preview()
    {
    try
    {
    if (DevExpress.XtraPrinting.PrintHelper.IsPrintingAvailable)
    {
    Cursor.Current = Cursors.AppStarting;
    if (_PrintHeader != null)
    {
    PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter;
     
                            //设置页眉
    phf.Header.Content.Clear();
    phf.Header.Content.AddRange(new string[] { "", _PrintHeader, "" });
    phf.Header.Font = new System.Drawing.Font("宋体", 14, System.Drawing.FontStyle.Bold);
    phf.Header.LineAlignment = BrickAlignment.Center;
     
                            //设置页脚
    phf.Footer.Content.Clear();
    phf.Footer.Content.AddRange(new string[] { """", _PrintFooter });
    phf.Footer.Font = new System.Drawing.Font("宋体", 9, System.Drawing.FontStyle.Regular);
    phf.Footer.LineAlignment = BrickAlignment.Center;
     
                        }
    link.PaperKind = ps.PageSettings.PaperKind ;
    link.Margins = ps.PageSettings.Margins;
    link.Landscape = ps.PageSettings.Landscape;
    link.CreateDocument();
     
    //汉化
    DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new Dxperience.LocalizationCHS.DxperienceXtraPrintingLocalizationCHS();
    ps.PreviewFormEx.Show();
     
                    }
    else
    {
    Cursor.Current = Cursors.Default;
    MessageBox.Show("打印机不可用 ""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    }
    finally
    {
    Cursor.Current = Cursors.Default;
    }
    }
     
            /// <summary>
    /// 打印
    /// </summary>
    public void Print()
    {
    try
    {
    if (DevExpress.XtraPrinting.PrintHelper.IsPrintingAvailable)
    {
    if (_PrintHeader != null)
    {
    PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter;
     
                            //设置页眉
    phf.Header.Content.Clear();
    phf.Header.Content.AddRange(new string[] { "", _PrintHeader, "" });
    phf.Header.Font = new System.Drawing.Font("宋体", 14, System.Drawing.FontStyle.Bold);
    phf.Header.LineAlignment = BrickAlignment.Center;
     
                            //设置页脚
    phf.Footer.Content.Clear();
    phf.Footer.Content.AddRange(new string[] { """", _PrintFooter });
    phf.Footer.Font = new System.Drawing.Font("宋体", 9, System.Drawing.FontStyle.Regular);
    phf.Footer.LineAlignment = BrickAlignment.Center;
     
                        }
    link.PaperKind = ps.PageSettings.PaperKind;
    link.Margins = ps.PageSettings.Margins;
    link.Landscape = ps.PageSettings.Landscape;
    link.CreateDocument();
    link.CreateDocument();
    //汉化
    DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new Dxperience.LocalizationCHS.DxperienceXtraPrintingLocalizationCHS();
    ps.Print();
    }
    else
    {
    Cursor.Current = Cursors.Default;
    MessageBox.Show("打印机不可用 ""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    }
    finally
    {
    }
    }
     
            //获取页面设置信息
    public void LoadPageSetting()
    {
     
                System.Drawing.Printing.Margins margins = new System.Drawing.Printing.Margins(60, 60, 60, 60);
    ps.PageSettings.Assign(margins, _paperKind, _landScape);
     
            }
    }
    客户端如何使用:实例化PrintSettingController,进行参数设置,包括页眉页脚横纵向纸型,当然所有这些设置信息也可以在PrintSettingControll类里进行设置了,我这里是需求的需要,所以加在了外面,PrintSystem.PageSettings属性是XtraPageSettings类型,XtraPageSettings类封装了所有的页面设置信息,你可以在LoadPageSetting()函数里面进行修改,来实现自己想要的功能,例如可以把设置信息保存成XML文件
    PrintSettingController psc = new PrintSettingController(this.gridControl1);
    //页眉
    psc.PrintHeader = this.Text;
     
    //页脚
    psc.PrintFooter = this.dateTimePicker1.Text.ToString();
     
    //横纵向
    psc.LandScape = this.rbtnHorizon.Checked;
     
    //纸型
    psc.PaperKind = System.Drawing.Printing.PaperKind.A4;
    //加载页面设置信息
    psc.LoadPageSetting();
     
    psc.Preview();
  • 相关阅读:
    Android 隐式意图激活另外一个Actitity
    Android 显示意图激活另外一个Actitity
    Android 创建一个新的Activity
    调用meitu秀秀.so文件实现美图功能
    Android C代码回调java方法
    Android 在C代码中调用logcat
    Android java传递int类型数组给C
    Android java传递string类型数据给C
    使用pywin32处理excel文件
    利用 pywin32 操作 excel
  • 原文地址:https://www.cnblogs.com/xiaoxihebei/p/10755433.html
Copyright © 2011-2022 走看看