(原)打印XtraGrid并设置表头、纸张类型

场景:
        使用XtraGrid打印各种报表,要求能显示表头、条件,设置纸张类型等。
解决办法:
      
  1using System;
  2using System.Data;
  3using DevExpress.XtraPrintingLinks;
  4using DevExpress.XtraPrinting;
  5using System.Drawing;
  6using System.Drawing.Printing;
  7
  8
  9namespace Dhthx.Report
 10{
 11    /// <summary>
 12    /// ReportCenter 的摘要说明。
 13    /// </summary>

 14    public class ReportCenter
 15    {
 16    
 17        private PrintingSystem printSystem;
 18        private string mReportName;
 19        private string mCondition;
 20
 21        public ReportCenter(IPrintable Printable)
 22        {
 23            printSystem = new PrintingSystem();
 24            mReportName = "";
 25            mCondition = "";
 26            PrintableComponentLink pcl = new PrintableComponentLink();
 27            pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
 28            pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
 29            pcl.Component = Printable;
 30            printSystem.Links.Add(pcl);
 31            pcl.CreateDocument();
 32
 33            PrinterSettingsUsing pst = new PrinterSettingsUsing();
 34            pst.UseMargins = false;
 35            pst.UsePaperKind = false;
 36            printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
 37            printSystem.PageSettings.PaperName = "A4";
 38            printSystem.PageSettings.LeftMargin = 2;
 39            printSystem.PageSettings.RightMargin = 2;
 40            printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
 41        }

 42        public ReportCenter(IPrintable Printable, string ReportName)
 43        {
 44            printSystem = new PrintingSystem();
 45            mReportName = ReportName;
 46            mCondition = "";
 47            PrintableComponentLink pcl = new PrintableComponentLink();
 48            pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
 49            pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
 50            pcl.Component = Printable;
 51            printSystem.Links.Add(pcl);
 52            pcl.CreateDocument();
 53
 54            PrinterSettingsUsing pst = new PrinterSettingsUsing();
 55            pst.UseMargins = false;
 56            pst.UsePaperKind = false;
 57            printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
 58            printSystem.PageSettings.PaperName = "A4";
 59            printSystem.PageSettings.LeftMargin = 2;
 60            printSystem.PageSettings.RightMargin = 2;
 61            printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
 62        }

 63        public ReportCenter(IPrintable Printable, string ReportName, string Condition)
 64        {
 65            printSystem = new PrintingSystem();
 66            mReportName = ReportName;
 67            mCondition = Condition;
 68            PrintableComponentLink pcl = new PrintableComponentLink();
 69            pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
 70            pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
 71            pcl.Component = Printable;
 72            printSystem.Links.Add(pcl);
 73            pcl.CreateDocument();
 74
 75            PrinterSettingsUsing pst = new PrinterSettingsUsing();
 76            pst.UseMargins = true;
 77            //pst.UsePaperKind = false;
 78            printSystem.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;
 79            printSystem.PageSettings.PaperName = "A4";
 80            printSystem.PageSettings.LeftMargin = 30;
 81            printSystem.PageSettings.RightMargin = 30;
 82            printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
 83        }

 84
 85        public ReportCenter(IPrintable Printable, PaperKind paperKind)
 86        {
 87            printSystem = new PrintingSystem();
 88            mReportName = "";
 89            mCondition = "";
 90            PrintableComponentLink pcl = new PrintableComponentLink();
 91            pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
 92            pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
 93            pcl.Component = Printable;
 94            printSystem.Links.Add(pcl);
 95            pcl.CreateDocument();
 96
 97            PrinterSettingsUsing pst = new PrinterSettingsUsing();
 98            pst.UseMargins = false;
 99            pst.UsePaperKind = false;
100            printSystem.PageSettings.PaperKind = paperKind;
101            printSystem.PageSettings.PaperName = "A4";
102            printSystem.PageSettings.LeftMargin = 2;
103            printSystem.PageSettings.RightMargin = 2;
104            printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
105        }

106        public ReportCenter(IPrintable Printable, string ReportName, PaperKind paperKind)
107        {
108            printSystem = new PrintingSystem();
109            mReportName = ReportName;
110            mCondition = "";
111            PrintableComponentLink pcl = new PrintableComponentLink();
112            pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
113            pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
114            pcl.Component = Printable;
115            printSystem.Links.Add(pcl);
116            pcl.CreateDocument();
117
118            PrinterSettingsUsing pst = new PrinterSettingsUsing();
119            pst.UseMargins = true;
120            //pst.UsePaperKind = false;
121            printSystem.PageSettings.PaperKind = paperKind;
122            printSystem.PageSettings.PaperName = "A4";
123            printSystem.PageSettings.LeftMargin = 30;
124            printSystem.PageSettings.RightMargin = 30;
125            printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
126        }

127        public ReportCenter(IPrintable Printable, string ReportName, string Condition,PaperKind paperKind)
128        {
129            printSystem = new PrintingSystem();
130            mReportName = ReportName;
131            mCondition = Condition;
132            PrintableComponentLink pcl = new PrintableComponentLink();
133            pcl.CreateMarginalHeaderArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalHeaderArea);
134            pcl.CreateMarginalFooterArea += new DevExpress.XtraPrinting.CreateAreaEventHandler(link_CreateMarginalFooterArea);
135            pcl.Component = Printable;
136            printSystem.Links.Add(pcl);
137            pcl.CreateDocument();
138
139            PrinterSettingsUsing pst = new PrinterSettingsUsing();
140            pst.UseMargins = true;
141            //pst.UsePaperKind = false;
142            printSystem.PageSettings.PaperKind = paperKind;
143            //printSystem.PageSettings.PaperName = "A4";
144            printSystem.PageSettings.LeftMargin = 30;
145            printSystem.PageSettings.RightMargin = 30;
146            printSystem.PageSettings.AssignDefaultPrinterSettings(pst);
147        }

148
149        private void link_CreateMarginalHeaderArea(object sender, DevExpress.XtraPrinting.CreateAreaEventArgs e)
150        {
151            if (mReportName != "")
152            {
153                e.Graph.Font = new Font("宋体"15, FontStyle.Bold);
154                e.Graph.BackColor = Color.Transparent;
155                RectangleF r = new RectangleF(0200, e.Graph.Font.Height + 20);
156                PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, mReportName, Color.Black, r, BorderSide.None);
157                brick.Alignment = BrickAlignment.Center;
158                brick.AutoWidth = true;
159            }

160
161            if (mCondition != "")
162            {
163                e.Graph.Font = new Font("宋体"10);
164                e.Graph.BackColor = Color.Transparent;
165                RectangleF r = new RectangleF(0500, e.Graph.Font.Height + 20);
166                PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, mCondition, Color.Black, r, BorderSide.None);
167                brick.Alignment = BrickAlignment.Center;
168                brick.AutoWidth = true;
169            }

170        }

171        private void link_CreateMarginalFooterArea(object sender, CreateAreaEventArgs e)
172        {
173            string format = "第{0}页 共{1}页";
174            e.Graph.Font = new Font("宋体"10);
175            e.Graph.BackColor = Color.Transparent;
176
177            RectangleF r = new RectangleF(050, e.Graph.Font.Height + 20);
178
179            PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, format, Color.Black, r, BorderSide.None);
180            brick.Alignment = BrickAlignment.Far;
181            brick.AutoWidth = true;
182
183            brick = e.Graph.DrawPageInfo(PageInfo.DateTime, "打印时间:" + DateTime.Today.ToLongDateString(), Color.Black, r, BorderSide.None);
184            brick.Alignment = BrickAlignment.Near;
185            brick.AutoWidth = true;
186        }
public void Print()
187        {
188            printSystem.Print();
189        }

190        
191        public void Preview()
192        {
193            FrmPreview frm = new FrmPreview(printSystem);
194            frm.Show();
195        }

196        public void Designe()
197        {}
198
199        
200    }

201}

202

1//使用方法
2ReportCenter rc = new ReportCenter(gControl, lblTitle.Text, strDate);
3                rc.Preview();
posted @ 2007-09-21 09:12  禹过天晴  阅读(1433)  评论(4编辑  收藏  举报