|
using System; using System.Xml; using System.Drawing;
namespace RemotePrint { public class Table : PrintElement { private XmlNode table; public static int count = 0, pc = 1;
public Table(XmlNode Table) { table = Table; }
public override bool Draw(Graphics g) { file://表格坐标 int tableX = int.Parse(table.Attributes["x"].InnerText); int tableY = int.Parse(table.Attributes["y"].InnerText); int x = tableX, y = tableY; DrawTopLine(g, table);//画表格顶线 Pen pen = new Pen(Color.FromName(table.Attributes["bordercolor"].InnerText), float.Parse(table.Attributes["border"].InnerText)); int trheight = 0; file://表头 foreach(XmlNode tr in table["tablehead"].ChildNodes) { trheight = int.Parse(tr.Attributes["height"].InnerText); DrawTR(x, y, tr, pen, g); y += trheight; } file://表项 for(int i = 0; i < int.Parse(table.Attributes["maxlines"].InnerText); i++) { XmlNode tr = table["tablebody"].ChildNodes[count]; trheight = int.Parse(tr.Attributes["height"].InnerText); DrawTR(x, y, tr, pen, g); y += trheight; count++; if(count == table["tablebody"].ChildNodes.Count) break; } x = tableX; file://表底 foreach(XmlNode tr in table["tablefoot"].ChildNodes) { trheight = int.Parse(tr.Attributes["height"].InnerText); DrawTR(x, y, tr, pen, g); y += trheight; } int currentpage = pc; pc++; bool hasPage = false;
if(count < table["tablebody"].ChildNodes.Count - 1) { hasPage = true;//需要继续打印 } else { count = 0; pc = 1; hasPage = false;//表格打印完毕 } return hasPage; }
private void DrawTopLine(Graphics g, XmlNode table) { Pen pen = new Pen(Color.FromName(table.Attributes["bordercolor"].InnerText), float.Parse(table.Attributes["border"].InnerText)); int width = 0; foreach(XmlNode td in table.FirstChild.FirstChild) { width += int.Parse(td.Attributes["width"].InnerText); } int x = int.Parse(table.Attributes["x"].InnerText); int y = int.Parse(table.Attributes["y"].InnerText); g.DrawLine(pen, x, y, x + width, y); }
file://画表格行 private void DrawTR(int x, int y, XmlNode tr, Pen pen, Graphics g) { int height = int.Parse(tr.Attributes["height"].InnerText); int width; g.DrawLine(pen, x, y, x, y + height);//画左端线条 foreach(XmlNode td in tr) { width = int.Parse(td.Attributes["width"].InnerText); DrawTD(x, y, width, height, td, g); g.DrawLine(pen, x + width, y, x + width, y + height);//右线 g.DrawLine(pen, x, y + height, x + width, y + height);//底线 x += width; } }
file://画单元格 private void DrawTD(int x, int y, int width, int height, XmlNode td, Graphics g) { Brush brush = new SolidBrush(Color.FromName(td.Attributes["bgcolor"].InnerText)); g.FillRectangle(brush, x, y, width, height); FontStyle style = FontStyle.Regular; file://设置字体样式 if(td.Attributes["b"].InnerText == "true") style |= FontStyle.Bold; if(td.Attributes["i"].InnerText == "true") style |= FontStyle.Italic; if(td.Attributes["u"].InnerText == "true") style |= FontStyle.Underline; Font font = new Font(td.Attributes["fontname"].InnerText, float.Parse(td.Attributes["fontsize"].InnerText), style); brush = new SolidBrush(Color.FromName(td.Attributes["fontcolor"].InnerText)); StringFormat sf = new StringFormat(); file://设置对齐方式 switch(td.Attributes["align"].InnerText) { case "center": sf.Alignment = StringAlignment.Center; break; case "right": sf.Alignment = StringAlignment.Near; break; default: sf.Alignment = StringAlignment.Far; break; } sf.LineAlignment = StringAlignment.Center; RectangleF rect = new RectangleF( (float)x, (float)y, (float)width, (float)height); g.DrawString(td.InnerText, font, brush, rect, sf); } } } |