using System; using System.Data; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; namespace GoldPrinter { public class MisGoldPrinter : IDisposable { private const int CON_SPACE_TITLE_CAPTION = 5; private const int CON_SPACE_CAPTION_TOP = 20; private const int CON_SPACE_HEADER_BODY = 5; private const int CON_SPACE_BODY_FOOTER = 5; private int X; private int Y; private int Width; private float Scale = 1f; private int mCurrentPageIndex; private int mCurrentRowIndex; private Graphics mGraphics; private Printer mPrinter; private PrintDocument mPrintDocument; private PrinterMargins mPrinterMargins; private Sewing _sewing; private bool _isOnlySingleColor = true; public Color BackColor = Color.White; private int _rowsPerPage = -1; private bool _isSubTotalPerPage = false; private string _subTotalColsList = ""; private bool _isSewingLine = false; private bool _isPrinterMargins = false; private GridBorderFlag _gridBorder = GridBorderFlag.Double; private Title _title; private Caption _caption; private Top _top; private Header _header; private MultiHeader _multiHeader; private Body _body; protected Footer _footer; private Bottom _bottom; public Sewing Sewing { get { return this._sewing; } set { if (value != null) { this._sewing = value; } else { this._sewing.Margin = 0; } } } public string DocumentName { get { return this.mPrintDocument.DocumentName; } set { this.mPrintDocument.DocumentName = value; } } public int RowsPerPage { get { return this._rowsPerPage; } set { this._rowsPerPage = value; } } public bool IsSubTotalPerPage { get { return this._isSubTotalPerPage; } set { this._isSubTotalPerPage = value; } } public string SubTotalColsList { get { return this._subTotalColsList; } set { this._subTotalColsList = value; } } public bool IsSewingLine { get { return this._isSewingLine; } set { this._isSewingLine = value; } } public bool IsPrinterMargins { get { return this._isPrinterMargins; } set { this._isPrinterMargins = value; } } public GridBorderFlag GridBorder { get { return this._gridBorder; } set { this._gridBorder = value; } } public object Title { get { return this._title; } set { if (value != null) { if (value.GetType().ToString() == "System.String") { if (this._title == null) { this._title = new Title(); } this._title.Text = (string)value; } else if (value.GetType().ToString() == "GoldPrinter.Title") { this._title = (Title)value; } } } } public object Caption { get { return this._caption; } set { if (value != null) { if (value.GetType().ToString() == "System.String") { if (this._caption == null) { this._caption = new Caption(); } this._caption.Text = (string)value; } else if (value.GetType().ToString() == "GoldPrinter.Caption") { this._caption = (Caption)value; } } } } public object Top { get { return this._top; } set { if (value != null) { if (value.GetType().ToString() == "System.String" || value.GetType().ToString() == "System.String[]") { if (this._top == null) { this._top = new Top(); } this._top.DataSource = value; } else if (value.GetType().ToString() == "GoldPrinter.Top") { this._top = (Top)value; } } } } public object Bottom { get { return this._bottom; } set { if (value != null) { if (value.GetType().ToString() == "System.String" || value.GetType().ToString() == "System.String[]") { if (this._bottom == null) { this._bottom = new Bottom(); } this._bottom.DataSource = (string)value; } else if (value.GetType().ToString() == "GoldPrinter.Bottom") { this._bottom = (Bottom)value; } } } } public object Header { get { return this._header; } set { if (value != null) { if (value.GetType().ToString() == "System.String[]" || value.GetType().ToString() == "System.String[,]" || value.GetType().ToString() == "System.Data.DataTable") { if (this._header == null) { this._header = new Header(); } this._header.DataSource = value; } else if (value.GetType().ToString() == "GoldPrinter.Header") { this._header = (Header)value; } } } } public object Footer { get { return this._footer; } set { if (value != null) { if (value.GetType().ToString() == "System.String[]" || value.GetType().ToString() == "System.String[,]" || value.GetType().ToString() == "System.Data.DataTable") { if (this._footer == null) { this._footer = new Footer(); } this._footer.DataSource = value; } else if (value.GetType().ToString() == "GoldPrinter.Footer") { this._footer = (Footer)value; } } } } public object MultiHeader { get { return this._multiHeader; } set { if (value != null) { if (value.GetType().ToString() == "System.String[]" || value.GetType().ToString() == "System.String[,]" || value.GetType().ToString() == "System.Data.DataTable") { if (this._multiHeader == null) { this._multiHeader = new MultiHeader(); } this._multiHeader.DataSource = value; } else if (value.GetType().ToString() == "GoldPrinter.MultiHeader") { this._multiHeader = (MultiHeader)value; } } } } public object Body { get { return this._body; } set { if (value != null) { if (value.GetType().ToString() == "System.String[]" || value.GetType().ToString() == "System.String[,]" || value.GetType().ToString() == "System.Data.DataTable") { if (this._body == null) { this._body = new Body(); } this._body.DataSource = value; } else if (value.GetType().ToString() == "GoldPrinter.Body") { this._body = (Body)value; } } } } public object DataSource { get { return this._body.DataSource; } set { this._body.DataSource = value; } } public MisGoldPrinter() : this(false) { } public MisGoldPrinter(bool p_IsLandscape) { PrinterSingleton.Reset(); this.mCurrentPageIndex = 1; this.mCurrentRowIndex = 0; this.mPrintDocument = PrinterSingleton.PrintDocument; this.mPrintDocument.DefaultPageSettings.Landscape = p_IsLandscape; this.mPrinterMargins = PrinterSingleton.PrinterMargins; this.mPrintDocument.DocumentName = "MIS金质打印通,欢迎使用!"; this._sewing = new Sewing(30, SewingDirectionFlag.Left); this.mPrinter = new Printer(); this._body = new Body(); } public virtual void Dispose() { try { this.mGraphics.Dispose(); this.mPrintDocument.Dispose(); } catch { } } public PageSettings PageSetup() { PrinterPageSetting printerPageSetting = new PrinterPageSetting(this.mPrintDocument); printerPageSetting.PrintPage += new PrintPageDelegate(this.PrintPageEventHandler); PageSettings defaultPageSettings = this.mPrintDocument.DefaultPageSettings; PageSettings pageSettings = printerPageSetting.ShowPageSetupDialog(); if (defaultPageSettings != pageSettings) { PrinterSingleton.PrintDocument = this.mPrintDocument; this.mPrinterMargins = new PrinterMargins(this.mPrintDocument); PrinterSingleton.PrinterMargins = this.mPrinterMargins; } return pageSettings; } public PrinterSettings Print() { this.mCurrentPageIndex = 1; this.mCurrentRowIndex = 0; PrinterPageSetting printerPageSetting = new PrinterPageSetting(this.mPrintDocument); printerPageSetting.PrintPage += new PrintPageDelegate(this.PrintPageEventHandler); return printerPageSetting.ShowPrintSetupDialog(); } public void Preview() { this.mCurrentPageIndex = 1; this.mCurrentRowIndex = 0; PrinterPageSetting printerPageSetting = new PrinterPageSetting(this.mPrintDocument); printerPageSetting.PrintPage += new PrintPageDelegate(this.PrintPageEventHandler); printerPageSetting.ImportExcelValue = new ImportExcelDelegate(this.ImportExcelMethodHandler); printerPageSetting.ShowPrintPreviewDialog(); } public void ImportExcelMethodHandler(object obj, ImportExcelArgs ev) { ExcelAccess excelAccess = new ExcelAccess(); excelAccess.Open(); excelAccess.MergeCells(1, 1, 1, this._body.Cols); excelAccess.SetFont(1, 1, 1, this._body.Cols, this._title.Font); excelAccess.SetCellText(1, 1, 1, this._body.Cols, this._title.Text); excelAccess.SetCellText((DataTable)this.DataSource, 3, 1, true); FileDialog fileDialog = new SaveFileDialog(); fileDialog.AddExtension = true; fileDialog.DefaultExt = ".xls"; fileDialog.Title = "保存到Excel文件"; fileDialog.Filter = "Microsoft Office Excel 工作簿(*.xls)|*.xls|模板(*.xlt)|*.xlt"; if (fileDialog.ShowDialog() == DialogResult.OK) { if (excelAccess.SaveAs(fileDialog.FileName, true)) { MessageBox.Show("数据成功保存到Excel文件!", "GoldPrinter", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } fileDialog.Dispose(); excelAccess.Close(); } private void PrintPageEventHandler(object obj, PrintPageEventArgs ev) { Graphics graphics = ev.Graphics; this.mGraphics = graphics; try { bool flag = this.Draw(graphics); if (flag) { ev.HasMorePages = true; this.mCurrentPageIndex++; } else { ev.HasMorePages = false; this.mCurrentPageIndex = 1; this.mCurrentRowIndex = 0; } } catch (Exception var_2_55) { } } private void OutObject(Printer outer) { if (outer != null) { outer.Graphics = this.mGraphics; outer.Rectangle = new Rectangle(this.X, this.Y, this.Width, outer.Height); if (this._isOnlySingleColor) { outer.Pen = Pens.Black; outer.Brush = Brushes.Black; } outer.Draw(); this.Y += outer.Rectangle.Height; } } private bool Draw(Graphics g) { bool flag = false; if (this._body.Rows < 0) { throw new Exception("打印主要网格不能为空,请用Body设置!"); } this.mPrinter.Graphics = g; this.mPrinter.PrintDocument = this.mPrintDocument; this.mPrinter.Sewing = this.Sewing; this.mPrinter.PrinterMargins = this.mPrinterMargins; this.Y = this.mPrinter.PrinterMargins.Top; this.X = this.mPrinter.PrinterMargins.Left; this.Width = this.mPrinter.PrinterMargins.Width; this.DrawPrinterMargins(this.mPrinter); this.DrawSewing(this.mPrinter); if (this._title != null && (this.mCurrentPageIndex == 1 || this._title.IsDrawAllPage)) { this._title.PrinterMargins = this.mPrinterMargins; this.OutObject(this._title); } if (this._caption != null && (this.mCurrentPageIndex == 1 || this._caption.IsDrawAllPage)) { this._caption.MoveY = 0; if (this._title != null && (this.mCurrentPageIndex == 1 || this._title.IsDrawAllPage)) { this._caption.MoveY = this._title.Height + 5; } this._caption.PrinterMargins = this.mPrinterMargins; this.OutObject(this._caption); } if (this._title != null || this._caption != null) { this.Y += 20; } int num = 0; int i; if (!this._body.IsAverageColsWidth) { for (i = 0; i < this._body.ColsWidth.Length; i++) { num += this._body.ColsWidth[i]; } if (num > this.mPrinterMargins.Width) { this.Scale = (float)(this.mPrinterMargins.Width / num); } else { this.Width = num; this.X += (this.mPrinterMargins.Width - this.Width) / 2; } } if (this._top != null && (this.mCurrentPageIndex == 1 || this._top.IsDrawAllPage)) { this.OutObject(this._top); } if (this._header != null && (this.mCurrentPageIndex == 1 || this._header.IsDrawAllPage)) { this.OutObject(this._header); } if ((this._top != null || this._header != null) && (this.mCurrentPageIndex == 1 || (this._top != null && this._top.IsDrawAllPage) || (this._header != null && this._header.IsDrawAllPage))) { this.Y += 5; } if (this._multiHeader != null && (this.mCurrentPageIndex == 1 || this._multiHeader.IsDrawAllPage)) { this.OutObject(this._multiHeader); } float num2 = (float)(this.mPrinter.PrinterMargins.Height - (this.Y - this.mPrinter.PrinterMargins.Top)); if (this._footer != null && this._footer.IsDrawAllPage) { num2 -= (float)this._footer.Height; } if (this._bottom != null && this._bottom.IsDrawAllPage) { num2 -= (float)this._bottom.Height; } if (num2 < 0f) { throw new Exception("预留给打印主要网格的空间太小,请适当调整!"); } int num3 = (int)(num2 / (float)this._body.RowHeight); if (this.RowsPerPage > 0 && this.RowsPerPage < num3) { num3 = this.RowsPerPage; } if (this.IsSubTotalPerPage) { num3--; } Body body; if (this.RowsPerPage > 0 && this.RowsPerPage < num3) { body = new Body(num3, this._body.Cols); } else { if (num3 > this._body.Rows - this.mCurrentRowIndex) { num3 = this._body.Rows - this.mCurrentRowIndex; } body = new Body(num3, this._body.Cols); } string[,] array = new string[num3, this._body.Cols]; i = 0; while (i < num3 && this.mCurrentRowIndex < this._body.Rows) { for (int j = 0; j < this._body.Cols; j++) { array[i, j] = this._body.GetText(this.mCurrentRowIndex, j); } this.mCurrentRowIndex++; i++; } body.GridText = array; body.ColsAlignString = this._body.ColsAlignString; body.ColsWidth = this._body.ColsWidth; body.IsAverageColsWidth = this._body.IsAverageColsWidth; body.Font = (Font)this._body.Font.Clone(); this.OutObject(body); if (this.mCurrentRowIndex < this._body.Rows) { flag = true; } if (this._isSubTotalPerPage && this._subTotalColsList != "") { try { MultiHeader multiHeader = new MultiHeader(1, this._body.Cols); multiHeader.ColsWidth = this._body.ColsWidth; multiHeader.Graphics = g; multiHeader.PrintDocument = this.mPrintDocument; multiHeader.Sewing = this._sewing; multiHeader.Rectangle = new Rectangle(this.X, this.Y, this.Width, multiHeader.Height); multiHeader.SetText(0, 0, "本页小计"); multiHeader.SetText(0, 1, "本页小计"); string[] array2 = this._subTotalColsList.Split(new char[] { ';' }); double num4 = 0.0; for (i = 0; i < array2.Length; i++) { int num5 = int.Parse(array2[i]); for (int j = 0; j < array.GetLength(0); j++) { num4 += double.Parse(array[j, num5]); } multiHeader.SetText(0, num5, num4.ToString()); num4 = 0.0; } multiHeader.Draw(); this.Y += multiHeader.Height; } catch { } } if ((this._footer != null || this._bottom != null) && (this.mCurrentPageIndex == 1 || (this._top != null && this._top.IsDrawAllPage) || (this._header != null && this._header.IsDrawAllPage))) { this.Y += 5; } if (this._footer != null) { if (!flag || this._footer.IsDrawAllPage) { if (this._footer.IsDrawAllPage) { this.OutObject(this._footer); } } } if (this._bottom != null) { if (!flag || this._bottom.IsDrawAllPage) { if (this._bottom.IsDrawAllPage) { this.OutObject(this._bottom); } else { num2 = (float)(this.mPrinter.PrinterMargins.Height - (this.Y - this.mPrinter.PrinterMargins.Top)); if (num2 < (float)this._bottom.Height) { flag = true; } else { this.OutObject(this._bottom); } } } } this.DrawBorder(g, this._multiHeader, body); body.Dispose(); body = null; return flag; } private void DrawPrinterMargins(Printer printer) { if (this.IsPrinterMargins) { printer.DrawPrinterMargins(); } } private void DrawSewing(Printer printer) { if (this.IsSewingLine && this.Sewing.Margin > 0) { if (this.Sewing.LineLen <= 0) { if (this.Sewing.SewingDirection == SewingDirectionFlag.Left) { this.Sewing.LineLen = printer.PageHeight; } else if (this.Sewing.SewingDirection == SewingDirectionFlag.Top) { this.Sewing.LineLen = printer.PageWidth; } } printer.Sewing = this.Sewing; printer.DrawSewing(); } } private void DrawBorder(Graphics g, MultiHeader multiHeader, Body body) { int width = body.Rectangle.Width; int num = body.Rectangle.Height; int x; int y; if (multiHeader != null) { x = multiHeader.Rectangle.X; y = multiHeader.Rectangle.Y; num += multiHeader.Rectangle.Height; } else { x = body.Rectangle.X; y = body.Rectangle.Y; } if (this.IsSubTotalPerPage) { MultiHeader multiHeader2 = new MultiHeader(1, 1); num += multiHeader2.RowHeight; } Rectangle rectangle = new Rectangle(x, y, width, num); Pen pen = new Pen(Color.Black, 1f); DrawRectangle drawRectangle = new DrawRectangle(); drawRectangle.Graphics = g; drawRectangle.Rectangle = rectangle; drawRectangle.Pen = pen; switch (this.GridBorder) { case GridBorderFlag.Single: drawRectangle.Draw(); break; case GridBorderFlag.SingleBold: drawRectangle.Pen.Width = 2f; drawRectangle.Draw(); if (multiHeader != null) { drawRectangle.Rectangle = body.Rectangle; drawRectangle.DrawTopLine(); } break; case GridBorderFlag.Double: drawRectangle.Draw(); rectangle = new Rectangle(x - 2, y - 2, width + 4, num + 4); drawRectangle.Rectangle = rectangle; drawRectangle.Draw(); break; case GridBorderFlag.DoubleBold: drawRectangle.Draw(); rectangle = new Rectangle(x - 2, y - 2, width + 4, num + 4); drawRectangle.Rectangle = rectangle; drawRectangle.Pen.Width = 2f; drawRectangle.Draw(); break; } } private void AddSewingNonePrintArea() { if (this.Sewing.SewingDirection == SewingDirectionFlag.Left) { this.mPrinterMargins.Left += this.Sewing.Margin; this.mPrinterMargins.Width -= this.Sewing.Margin; } else if (this.Sewing.SewingDirection == SewingDirectionFlag.Top) { this.mPrinterMargins.Top += this.Sewing.Margin; this.mPrinterMargins.Height -= this.Sewing.Margin; } } private void WriteMetricsToConsole(PrintPageEventArgs ev) { Graphics graphics = ev.Graphics; Console.WriteLine("*****Information about the printer*****"); Console.WriteLine("纸张的大小 ev.PageSettings.PaperSize:" + ev.PageSettings.PaperSize); Console.WriteLine("打印分辨率 ev.PageSettings.PrinterResolution:" + ev.PageSettings.PrinterResolution); Console.WriteLine("旋转的角度 ev.PageSettings.PrinterSettings.LandscapeAngle" + ev.PageSettings.PrinterSettings.LandscapeAngle); Console.WriteLine(""); Console.WriteLine("*****Information about the page*****"); Console.WriteLine("页面的大小 ev.PageSettings.Bounds:" + ev.PageSettings.Bounds); Console.WriteLine("页面(同上) ev.PageBounds:" + ev.PageBounds); Console.WriteLine("页面的边距 ev.PageSettings.Margins.:" + ev.PageSettings.Margins); Console.WriteLine("页面的边距 ev.MarginBounds:" + ev.MarginBounds); Console.WriteLine("水平分辨率 ev.Graphics.DpiX:" + ev.Graphics.DpiX); Console.WriteLine("垂直分辨率 ev.Graphics.DpiY:" + ev.Graphics.DpiY); ev.Graphics.SetClip(ev.PageBounds); Console.WriteLine("ev.Graphics.VisibleClipBounds:" + ev.Graphics.VisibleClipBounds); SizeF sizeF = new SizeF(ev.Graphics.VisibleClipBounds.Width * ev.Graphics.DpiX / 100f, ev.Graphics.VisibleClipBounds.Height * ev.Graphics.DpiY / 100f); Console.WriteLine("drawing Surface Size in Pixels" + sizeF); } } }