private System.Drawing.Size GetPageSize(string xml) { Helper.ReadXml r = new Helper.ReadXml(xml); int w = Convert.ToInt16(r.Read("Page/Width")); int h = Convert.ToInt16(r.Read("Page/Height")); return(new System.Drawing.Size(w, h)); }
private int SmallTotal(string xml) { Helper.ReadXml r = new Helper.ReadXml(xml); foreach (Helper.ReadXml r2 in r.ReadList("Page/Column")) { if (r2.Read("ColName") == "行号") { } else { if (r2.Read("SmallTotal") == "1") { return(1); } else { } } } return(0); }
private void Draw(System.Drawing.Graphics g, int area, int pageIndex, int cur_top) { Helper.ReadXml r = new Helper.ReadXml(xml); int offset_y = 0; if (area == 1) { offset_y -= barHeight; } else if (area == 2) { offset_y -= ah + 2 * barHeight; } else if (area == 3) { offset_y -= ah + bh + barHeight * 3; } else if (area == 4) { offset_y -= ah + bh + ch + barHeight * 4; } else if (area == 5) { offset_y -= ah + bh + ch + dh + barHeight * 5; } if (area == 5) { cur_top = pageHeight - eh; } foreach (Helper.ReadXml r2 in r.ReadList("PrintObject1")) { int left = Convert.ToInt16(r2.Read("Left")); int top = Convert.ToInt16(r2.Read("Top")); top += offset_y + cur_top; int Width = Convert.ToInt16(r2.Read("Width")); int Height = Convert.ToInt16(r2.Read("Height")); string context = r2.Read("Context"); int Align = Convert.ToInt16(r2.Read("Align")); var sf = Helper.Conv.AlignToStringFormat(Align); FontConverter fc = new FontConverter(); var font = (Font)fc.ConvertFromString(r2.Read("Font")); var color = Color.FromArgb(Convert.ToInt32(r2.Read("Color"))); int Area = Convert.ToInt16(r2.Read("Area")); int _BorderLeft = Convert.ToInt16(r2.Read("BorderLeft")); int _BorderRight = Convert.ToInt16(r2.Read("BorderRight")); int _BorderTop = Convert.ToInt16(r2.Read("BorderTop")); int _BorderBottom = Convert.ToInt16(r2.Read("BorderBottom")); Rectangle rec = new Rectangle(left, top, Width, Height); if (Area == area) { g.DrawString(context, font, new SolidBrush(color), rec, sf); if (_BorderLeft == 1) { g.DrawLine(Pens.Black, left, top, left, top + Height); } if (_BorderRight == 1) { g.DrawLine(Pens.Black, left + Width, top, left + Width, top + Height); } if (_BorderTop == 1) { g.DrawLine(Pens.Black, left, top, left + Width, top); } if (_BorderBottom == 1) { g.DrawLine(Pens.Black, left, top + Height, left + Width, top + Height); } } } foreach (Helper.ReadXml r2 in r.ReadList("PrintObject2")) { int left = Convert.ToInt16(r2.Read("Left")); int top = Convert.ToInt16(r2.Read("Top")); top += offset_y + cur_top; int Width = Convert.ToInt16(r2.Read("Width")); int Height = Convert.ToInt16(r2.Read("Height")); string Field = r2.Read("Field"); string Format = r2.Read("Format"); int Align = Convert.ToInt16(r2.Read("Align")); var sf = Helper.Conv.AlignToStringFormat(Align); FontConverter fc = new FontConverter(); var font = (Font)fc.ConvertFromString(r2.Read("Font")); var color = Color.FromArgb(Convert.ToInt32(r2.Read("Color"))); int Area = Convert.ToInt16(r2.Read("Area")); int _BorderLeft = Convert.ToInt16(r2.Read("BorderLeft")); int _BorderRight = Convert.ToInt16(r2.Read("BorderRight")); int _BorderTop = Convert.ToInt16(r2.Read("BorderTop")); int _BorderBottom = Convert.ToInt16(r2.Read("BorderBottom")); Rectangle rec = new Rectangle(left, top, Width, Height); if (Area == area) { if (tbmain.Rows.Count != 0) { if (tbmain.Columns.Contains(Field) == true) { DataRow row = tbmain.Rows[0]; DataColumn col = tbmain.Columns[Field]; if (tbmain.Columns[Field].DataType == typeof(byte[])) { System.IO.MemoryStream ms = new System.IO.MemoryStream((byte[])row[Field]); Image img = Image.FromStream(ms); g.DrawImage(img, rec); continue; } string context = ""; if (Format == "") { context = row[Field].ToString(); } else if (Format == "大写金额") { context = Helper.Conv.DaXie2(row[Field].ToString()); } else if (Format == "条形码") { Helper.Barcode.CreateBarcodeImage(g, rec.X, rec.Y, "C", row[Field].ToString(), 1F, rec.Height); } else if (Format == "二维码") { Helper.QRCode code = new Helper.QRCode(); Bitmap bit = code.grant_qrcode(row[Field].ToString(), rec.Width, rec.Width); g.DrawImage(bit, rec); } else { if (col.DataType == typeof(decimal)) { context = Helper.Conv.ToDecimal(row[Field]).ToString(Format); } else if (col.DataType == typeof(Int16)) { context = Helper.Conv.ToInt16(row[Field]).ToString(Format); } else if (col.DataType == typeof(Int32)) { context = Helper.Conv.ToInt32(row[Field]).ToString(Format); } else if (col.DataType == typeof(DateTime)) { context = Helper.Conv.ToDateTime(row[Field]).ToString(Format); } else { context = row[Field].ToString(); } } if (Format == "条形码") { } else if (Format == "二维码") { } else { g.DrawString(context, font, new SolidBrush(color), rec, sf); } if (_BorderLeft == 1) { g.DrawLine(Pens.Black, left, top, left, top + Height); } if (_BorderRight == 1) { g.DrawLine(Pens.Black, left + Width, top, left + Width, top + Height); } if (_BorderTop == 1) { g.DrawLine(Pens.Black, left, top, left + Width, top); } if (_BorderBottom == 1) { g.DrawLine(Pens.Black, left, top + Height, left + Width, top + Height); } } } } } foreach (Helper.ReadXml r2 in r.ReadList("PrintObject3")) { int left = Convert.ToInt16(r2.Read("Left")); int top = Convert.ToInt16(r2.Read("Top")); top += offset_y + cur_top; int Width = Convert.ToInt16(r2.Read("Width")); int Height = Convert.ToInt16(r2.Read("Height")); string Field = r2.Read("Field"); string Format = r2.Read("Format"); int Align = Convert.ToInt16(r2.Read("Align")); var sf = Helper.Conv.AlignToStringFormat(Align); FontConverter fc = new FontConverter(); var font = (Font)fc.ConvertFromString(r2.Read("Font")); var color = Color.FromArgb(Convert.ToInt32(r2.Read("Color"))); int Area = Convert.ToInt16(r2.Read("Area")); int _BorderLeft = Convert.ToInt16(r2.Read("BorderLeft")); int _BorderRight = Convert.ToInt16(r2.Read("BorderRight")); int _BorderTop = Convert.ToInt16(r2.Read("BorderTop")); int _BorderBottom = Convert.ToInt16(r2.Read("BorderBottom")); Rectangle rec = new Rectangle(left, top, Width, Height); if (Area == area) { if (tbdetail.Rows.Count != 0) { if (tbdetail.Columns.Contains(Field) == true) { var col = tbdetail.Columns[Field]; string context = ""; decimal val = 0; foreach (DataRow row in tbdetail.Rows) { val += Helper.Conv.ToDecimal(row[Field].ToString()); } if (Format == "") { context = val.ToString(); } else if (Format == "大写金额") { context = Helper.Conv.DaXie2(val.ToString("0.00")); } else { if (col.DataType == typeof(decimal)) { context = val.ToString(Format); } else if (col.DataType == typeof(Int16)) { context = Helper.Conv.ToInt16(val).ToString(Format); } else if (col.DataType == typeof(Int32)) { context = Helper.Conv.ToInt32(val).ToString(Format); } else if (col.DataType == typeof(DateTime)) { context = Helper.Conv.ToDateTime(val).ToString(Format); } else { context = val.ToString(); } } g.DrawString(context, font, new SolidBrush(color), rec, sf); if (_BorderLeft == 1) { g.DrawLine(Pens.Black, left, top, left, top + Height); } if (_BorderRight == 1) { g.DrawLine(Pens.Black, left + Width, top, left + Width, top + Height); } if (_BorderTop == 1) { g.DrawLine(Pens.Black, left, top, left + Width, top); } if (_BorderBottom == 1) { g.DrawLine(Pens.Black, left, top + Height, left + Width, top + Height); } } } } } foreach (Helper.ReadXml r2 in r.ReadList("PrintObject4")) { int left = Convert.ToInt16(r2.Read("Left")); int top = Convert.ToInt16(r2.Read("Top")); top += offset_y + cur_top; int Width = Convert.ToInt16(r2.Read("Width")); int Height = Convert.ToInt16(r2.Read("Height")); var color = Color.FromArgb(Convert.ToInt32(r2.Read("Color"))); int Area = Convert.ToInt16(r2.Read("Area")); if (Area == area) { g.DrawLine(new Pen(color), left + Width / 2, top, left + Width / 2, top + Height); } } foreach (Helper.ReadXml r2 in r.ReadList("PrintObject5")) { int left = Convert.ToInt16(r2.Read("Left")); int top = Convert.ToInt16(r2.Read("Top")); top += offset_y + cur_top; int Width = Convert.ToInt16(r2.Read("Width")); int Height = Convert.ToInt16(r2.Read("Height")); var color = Color.FromArgb(Convert.ToInt32(r2.Read("Color"))); int Area = Convert.ToInt16(r2.Read("Area")); if (Area == area) { g.DrawLine(new Pen(color), left, top + Height / 2, left + Width, top + Height / 2); } } foreach (Helper.ReadXml r2 in r.ReadList("PrintObject6")) { int left = Convert.ToInt16(r2.Read("Left")); int top = Convert.ToInt16(r2.Read("Top")); top += offset_y + cur_top; int Width = Convert.ToInt16(r2.Read("Width")); int Height = Convert.ToInt16(r2.Read("Height")); var img = Helper.Conv.StringToImage(r2.Read("Image")); int Area = Convert.ToInt16(r2.Read("Area")); Rectangle rec = new Rectangle(left, top, Width, Height); if (Area == area) { g.DrawImage(img, rec); } } foreach (Helper.ReadXml r2 in r.ReadList("PrintObject7")) { int left = Convert.ToInt16(r2.Read("Left")); int top = Convert.ToInt16(r2.Read("Top")); top += offset_y + cur_top; int Width = Convert.ToInt16(r2.Read("Width")); int Height = Convert.ToInt16(r2.Read("Height")); int Align = Convert.ToInt16(r2.Read("Align")); var sf = Helper.Conv.AlignToStringFormat(Align); FontConverter fc = new FontConverter(); var font = (Font)fc.ConvertFromString(r2.Read("Font")); var color = Color.FromArgb(Convert.ToInt32(r2.Read("Color"))); int Area = Convert.ToInt16(r2.Read("Area")); int _BorderLeft = Convert.ToInt16(r2.Read("BorderLeft")); int _BorderRight = Convert.ToInt16(r2.Read("BorderRight")); int _BorderTop = Convert.ToInt16(r2.Read("BorderTop")); int _BorderBottom = Convert.ToInt16(r2.Read("BorderBottom")); Rectangle rec = new Rectangle(left, top, Width, Height); if (Area == area) { string context = "第N页,共M页".Replace("N", pageIndex.ToString()) .Replace("M", PageCount.ToString()); g.DrawString(context, font, new SolidBrush(color), rec, sf); if (_BorderLeft == 1) { g.DrawLine(Pens.Black, left, top, left, top + Height); } if (_BorderRight == 1) { g.DrawLine(Pens.Black, left + Width, top, left + Width, top + Height); } if (_BorderTop == 1) { g.DrawLine(Pens.Black, left, top, left + Width, top); } if (_BorderBottom == 1) { g.DrawLine(Pens.Black, left, top + Height, left + Width, top + Height); } } } foreach (Helper.ReadXml r2 in r.ReadList("PrintObject8")) { int left = Convert.ToInt16(r2.Read("Left")); int top = Convert.ToInt16(r2.Read("Top")); top += offset_y + cur_top; int Width = Convert.ToInt16(r2.Read("Width")); int Height = Convert.ToInt16(r2.Read("Height")); string format = r2.Read("Format"); int Align = Convert.ToInt16(r2.Read("Align")); var sf = Helper.Conv.AlignToStringFormat(Align); FontConverter fc = new FontConverter(); var font = (Font)fc.ConvertFromString(r2.Read("Font")); var color = Color.FromArgb(Convert.ToInt32(r2.Read("Color"))); int Area = Convert.ToInt16(r2.Read("Area")); int _BorderLeft = Convert.ToInt16(r2.Read("BorderLeft")); int _BorderRight = Convert.ToInt16(r2.Read("BorderRight")); int _BorderTop = Convert.ToInt16(r2.Read("BorderTop")); int _BorderBottom = Convert.ToInt16(r2.Read("BorderBottom")); Rectangle rec = new Rectangle(left, top, Width, Height); if (Area == area) { string context = ""; if (format == "") { context = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); } else { context = System.DateTime.Now.ToString(format); } g.DrawString(context, font, new SolidBrush(color), rec, sf); if (_BorderLeft == 1) { g.DrawLine(Pens.Black, left, top, left, top + Height); } if (_BorderRight == 1) { g.DrawLine(Pens.Black, left + Width, top, left + Width, top + Height); } if (_BorderTop == 1) { g.DrawLine(Pens.Black, left, top, left + Width, top); } if (_BorderBottom == 1) { g.DrawLine(Pens.Black, left, top + Height, left + Width, top + Height); } } } }
void IBLL.IPrint.Print(string style_id, string xml, System.Data.DataTable tbmain, System.Data.DataTable tbdetail) { try { this.style_id = style_id; this.xml = xml; this.tbmain = tbmain; this.tbdetail = tbdetail; row_count = this.tbdetail.Rows.Count; if (this.tbdetail.Columns.Contains("行号") == true) { this.tbdetail.Columns.Remove("行号"); this.tbdetail.Columns.Add("行号"); this.tbdetail.Columns["行号"].SetOrdinal(0); } else { this.tbdetail.Columns.Add("行号"); this.tbdetail.Columns["行号"].SetOrdinal(0); } // Helper.ReadXml r = new Helper.ReadXml(xml); barHeight = Convert.ToInt16(r.Read("Page/BarHeight")); pageWidth = Convert.ToInt16(r.Read("Page/Width")); pageHeight = Convert.ToInt16(r.Read("Page/Height")); ah = Convert.ToInt16(r.Read("Page/AHeight")); bh = Convert.ToInt16(r.Read("Page/BHeight")); ch = Convert.ToInt16(r.Read("Page/CHeight")); dh = Convert.ToInt16(r.Read("Page/DHeight")); eh = Convert.ToInt16(r.Read("Page/EHeight")); AppendEmptyRow = Convert.ToInt16(r.Read("Page/AppendEmptyRow")); rowHeight = ch; smallTotal = SmallTotal(xml); FontConverter fc = new FontConverter(); gridFont = (Font)fc.ConvertFromString(r.Read("Page/GridFont")); gridLeft = Convert.ToInt16(r.Read("Page/GridLeft")); // tbstyle = new DataTable(); tbstyle.Columns.Add("display"); tbstyle.Columns.Add("colname"); tbstyle.Columns.Add("colbyname"); tbstyle.Columns.Add("width", typeof(int)); tbstyle.Columns.Add("align"); tbstyle.Columns.Add("format"); tbstyle.Columns.Add("smalltotal"); // foreach (Helper.ReadXml r2 in r.ReadList("Page/Column")) { var row = tbstyle.NewRow(); tbstyle.Rows.Add(row); row["display"] = r2.Read("Display"); row["colname"] = r2.Read("ColName"); row["colbyname"] = r2.Read("ColByname"); row["width"] = Convert.ToInt16(r2.Read("Width")); row["align"] = r2.Read("Align"); row["format"] = r2.Read("Format"); row["SmallTotal"] = r2.Read("SmallTotal"); } // this.ShowDialog(); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } }
public void Print(string xml, DataTable tbmain, DataTable tbdetail) { this.xml = xml; this.tbmain = tbmain; this.tbdetail = tbdetail; row_count = tbdetail.Rows.Count; if (this.tbdetail.Columns.Contains("行号") == true) { this.tbdetail.Columns.Remove("行号"); this.tbdetail.Columns.Add("行号"); } else { this.tbdetail.Columns.Add("行号"); } PageCount = this.GetPageCount(xml); PageSize = this.GetPageSize(xml); // if (tbdetail != null) { int index = 0; foreach (DataRow row in tbdetail.Rows) { index++; row["行号"] = index; } } // Helper.ReadXml r = new Helper.ReadXml(xml); barHeight = Convert.ToInt16(r.Read("Page/BarHeight")); pageWidth = Convert.ToInt16(r.Read("Page/Width")); pageHeight = Convert.ToInt16(r.Read("Page/Height")); ah = Convert.ToInt16(r.Read("Page/AHeight")); bh = Convert.ToInt16(r.Read("Page/BHeight")); ch = Convert.ToInt16(r.Read("Page/CHeight")); dh = Convert.ToInt16(r.Read("Page/DHeight")); eh = Convert.ToInt16(r.Read("Page/EHeight")); AppendEmptyRow = Convert.ToInt16(r.Read("Page/AppendEmptyRow")); rowHeight = ch; smallTotal = SmallTotal(xml); FontConverter fc = new FontConverter(); gridFont = (Font)fc.ConvertFromString(r.Read("Page/GridFont")); gridLeft = Convert.ToInt16(r.Read("Page/GridLeft")); // tbstyle = new DataTable(); tbstyle.Columns.Add("display"); tbstyle.Columns.Add("colname"); tbstyle.Columns.Add("colbyname"); tbstyle.Columns.Add("width"); tbstyle.Columns.Add("align"); tbstyle.Columns.Add("format"); tbstyle.Columns.Add("smalltotal"); // foreach (Helper.ReadXml r2 in r.ReadList("Page/Column")) { var row = tbstyle.NewRow(); tbstyle.Rows.Add(row); row["display"] = r2.Read("Display"); row["colname"] = r2.Read("ColName"); row["colbyname"] = r2.Read("ColByname"); row["width"] = Convert.ToInt16(r2.Read("Width")); row["align"] = r2.Read("Align"); row["format"] = r2.Read("Format"); row["SmallTotal"] = r2.Read("SmallTotal"); } // // pd = new System.Drawing.Printing.PrintDocument(); pd.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("printPage", PageSize.Width, PageSize.Height); pd.PrintPage += this.pd_pagePrint; PageIndex = 1; pd.Print(); }
private void pd_pagePrint(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Graphics g = e.Graphics; Helper.ReadXml r = new Helper.ReadXml(xml); // int top = 0; Draw(g, 1, PageIndex, top); top += ah; if (PageIndex == 1) { Draw(g, 2, PageIndex, top); top += bh; } int flag = 0; if (row_count > 0) { DrawGridHeader(g, top); top += rowHeight; flag = 1; } while (row_count > 0) { if (pageHeight - top - rowHeight - smallTotal * rowHeight < eh) { if (smallTotal == 1) { DrawSmallTotalRow(g, top); top += smallTotal * rowHeight; lstrow.Clear(); } break; } row_count--; // DrawRow(g, tbdetail.Rows[row_index], top); lstrow.Add(tbdetail.Rows[row_index]); // row_index++; top += rowHeight; } if (row_count == 0) { if (pageHeight - top < dh + eh) { if (AppendEmptyRow == 1) { while (flag == 1) { if (pageHeight - top - rowHeight - smallTotal * rowHeight < eh) { if (smallTotal == 1) { DrawSmallTotalRow(g, top); top += smallTotal * rowHeight; lstrow.Clear(); } break; } DrawEmptyRow(g, top); top += rowHeight; } } else { if (smallTotal == 1) { DrawSmallTotalRow(g, top); top += smallTotal * rowHeight; lstrow.Clear(); } } Draw(g, 5, PageIndex, top); top += eh; e.HasMorePages = true; } else { if (AppendEmptyRow == 1) { while (flag == 1) { if (pageHeight - top - rowHeight - smallTotal * rowHeight < dh + eh) { if (smallTotal == 1) { DrawSmallTotalRow(g, top); top += smallTotal * rowHeight; lstrow.Clear(); } break; } DrawEmptyRow(g, top); top += rowHeight; } } else { if (smallTotal == 1) { DrawSmallTotalRow(g, top); top += smallTotal * rowHeight; lstrow.Clear(); } } Draw(g, 4, PageIndex, top); top += dh; Draw(g, 5, PageIndex, top); top += eh; e.HasMorePages = false; } } else { Draw(g, 5, PageIndex, top); top += eh; e.HasMorePages = true; } PageIndex++; }