DevExpress组件之——ChartControl组件打印

        由于项目中需要将ChartControl中的图表打印出来,自带的只有个预览方法ShowPrintPreview(),经过资料查找,并对DevExpress打印进行了一些整理,现把具体思路贴供大家参考。

 

实现打印需要用到的主要类有PrintingSystem、XtraPageSettings、PrintableComponentLink 。
1、PrintingSystem类

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

2、XtraPageSettings类(提供对打印页面进行设置的操作)

3、PrintableComponentLink类

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

 4、代码实现: 

打印类代码
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Windows.Forms;
  6 using DevExpress.XtraPrinting;
  7 
  8 namespace Teleware.GeologicDisaster.ForecastApp
  9 {
 10     public class PrintClass
 11     {
 12         #region 私有字段
 13 
 14         private PrintingSystem ps = null;
 15         private string formName = null;
 16         private PrintableComponentLink link = null;
 17 
 18         #endregion
 19 
 20         #region 属性
 21         string _PrintHeader = null;  // 设置打印时的标题
 22         private string _PrintFooter = null// 设置打印时页脚显示内容
 23         private bool _landScape;  // 设置页面横纵向
 24         private System.Drawing.Printing.PaperKind _paperKind;  // 设置纸张类型(A1、A2、A3、A4)
 25 
 26         /// <summary>
 27         /// 设置打印时的标题
 28         /// </summary> 
 29         public string PrintHeader
 30         {
 31             set
 32             {
 33                 _PrintHeader = value;
 34             }
 35         }
 36 
 37         /// <summary>
 38         /// 设置打印时页脚显示内容
 39         /// </summary> 
 40         public string PrintFooter
 41         {
 42             set
 43             {
 44                 _PrintFooter = value;
 45             }
 46         }
 47 
 48         /// <summary>
 49         /// 设置页面横纵向
 50         /// </summary> 
 51         public bool LandScape
 52         {
 53             set { _landScape = value; }
 54         }
 55 
 56         /// <summary>
 57         /// 设置纸张类型(A1、A2、A3、A4)
 58         /// </summary>        
 59         public System.Drawing.Printing.PaperKind PaperKind
 60         {
 61             set { _paperKind = value; }
 62         }
 63         #endregion
 64 
 65         #region 构造函数
 66         /// <summary>
 67         /// 打印控制器
 68         /// </summary>
 69         /// <param name="control">要打印的部件</param>
 70         public PrintClass(IPrintable control)
 71         {
 72             if (control == null)
 73             {
 74                 return;
 75             }
 76             Control c = (Control)control;
 77             formName = c.FindForm().GetType().FullName + "." + c.Name;
 78             ps = new DevExpress.XtraPrinting.PrintingSystem();
 79             link = new DevExpress.XtraPrinting.PrintableComponentLink(ps);
 80             ps.Links.Add(link);
 81             link.Component = control;
 82         }
 83         #endregion
 84 
 85         #region 打印预览
 86         /// <summary>
 87         /// 打印预览
 88         /// </summary>
 89         public void Preview()
 90         {
 91             try
 92             {
 93                 if (DevExpress.XtraPrinting.PrintHelper.IsPrintingAvailable)
 94                 {
 95                     Cursor.Current = Cursors.AppStarting;
 96                     if (_PrintHeader != null)
 97                     {
 98                         PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter;
 99 
100                         //设置页眉
101                         phf.Header.Content.Clear();
102                         phf.Header.Content.AddRange(new string[] { "", _PrintHeader, "" });
103                         phf.Header.Font = new System.Drawing.Font("宋体"14, System.Drawing.FontStyle.Bold);
104                         phf.Header.LineAlignment = BrickAlignment.Center;
105 
106                         //设置页脚
107                         phf.Footer.Content.Clear();
108                         phf.Footer.Content.AddRange(new string[] { """", _PrintFooter });
109                         phf.Footer.Font = new System.Drawing.Font("宋体"9, System.Drawing.FontStyle.Regular);
110                         phf.Footer.LineAlignment = BrickAlignment.Center;
111 
112                     }
113                     link.PaperKind = ps.PageSettings.PaperKind;
114                     link.Margins = ps.PageSettings.Margins;
115                     link.Landscape = ps.PageSettings.Landscape;
116                     link.CreateDocument();
117 
118                     //汉化
119                     //DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new Dxperience.LocalizationCHS.DxperienceXtraPrintingLocalizationCHS();
120                     ps.PreviewFormEx.Show();
121 
122                 }
123                 else
124                 {
125                     Cursor.Current = Cursors.Default;
126                     MessageBox.Show("打印机不可用 ""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
127                 }
128             }
129             finally
130             {
131                 Cursor.Current = Cursors.Default;
132             }
133         }
134         #endregion
135 
136         #region 打印
137 
138         /// <summary>
139         /// 打印
140         /// </summary>
141         public void Print()
142         {
143             try
144             {
145                 if (DevExpress.XtraPrinting.PrintHelper.IsPrintingAvailable)
146                 {
147                     if (_PrintHeader != null)
148                     {
149                         PageHeaderFooter phf = link.PageHeaderFooter as PageHeaderFooter;
150 
151                         //设置页眉
152                         phf.Header.Content.Clear();
153                         phf.Header.Content.AddRange(new string[] { "", _PrintHeader, "" });
154                         phf.Header.Font = new System.Drawing.Font("宋体"14, System.Drawing.FontStyle.Bold);
155                         phf.Header.LineAlignment = BrickAlignment.Center;
156 
157                         //设置页脚
158                         phf.Footer.Content.Clear();
159                         phf.Footer.Content.AddRange(new string[] { """", _PrintFooter });
160                         phf.Footer.Font = new System.Drawing.Font("宋体"9, System.Drawing.FontStyle.Regular);
161                         phf.Footer.LineAlignment = BrickAlignment.Center;
162 
163                     }
164                     link.PaperKind = ps.PageSettings.PaperKind;
165                     link.Margins = ps.PageSettings.Margins;
166                     link.Landscape = ps.PageSettings.Landscape;
167                     link.CreateDocument();
168                     link.CreateDocument();
169                     //汉化
170                     //  DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new Dxperience.LocalizationCHS.DxperienceXtraPrintingLocalizationCHS();
171                     ps.Print();
172                 }
173                 else
174                 {
175                     Cursor.Current = Cursors.Default;
176                     MessageBox.Show("打印机不可用 ""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
177                 }
178             }
179             finally
180             {
181             }
182         }
183 
184         #endregion
185 
186         #region 获取页面设置信息
187 
188         /// <summary>
189         /// 获取页面设置信息
190         /// </summary>
191         public void LoadPageSetting()
192         {
193             System.Drawing.Printing.Margins margins = new System.Drawing.Printing.Margins(60606060);
194             ps.PageSettings.Assign(margins, _paperKind, _landScape);
195         }
196         #endregion
197     }
198 }
199 

 

调用代码
 1 private void 打印预览ToolStripMenuItem_Click(object sender, EventArgs e)
 2 {
 3     PrintClass printClass = new PrintClass(this.chartControl1);
 4 
 5     printClass.LandScape = true;
 6 
 7     printClass.PaperKind = System.Drawing.Printing.PaperKind.A4;
 8 
 9     printClass.LoadPageSetting();
10 
11     printClass.Preview();
12 }

 5、通过以上这两个类就能完全实现各种控件的打印了,只要一个控件实现了DevExpress.XtraPrinting.IPrintable这个接口就能够对该控件进行打印,如GridControl,CharControl等。

 

posted @ 2010-07-23 17:09  yangan-wu2008  阅读(3678)  评论(0编辑  收藏  举报