// 设置卷滚区域 void SetAutoScroll(LabelParam label_param) { double nWidth = 0; double nHeight = 0; bool bPrinter = true; // 是否要考虑打印机的纸张尺寸 if (label_param != null && string.IsNullOrEmpty(label_param.DefaultPrinter) == true) bPrinter = false; if (label_param != null) { if (label_param.RotateDegree == 90 || label_param.RotateDegree == 270) { nWidth = label_param.PageHeight; nHeight = label_param.PageWidth; } else { nWidth = label_param.PageWidth; nHeight = label_param.PageHeight; } } if (this._document != null && bPrinter == true) nWidth = Math.Max(nWidth, this._document.DefaultPageSettings.Bounds.Width); // this.document.DefaultPageSettings.PaperSize.Width if (this._document != null && bPrinter == true) nHeight = Math.Max(nHeight, this._document.DefaultPageSettings.Bounds.Height); // this.document.DefaultPageSettings.PaperSize.Height #if NO bool bLandscape = false; if (label_param != null) bLandscape = label_param.Landscape; else #endif #if NO if (this.document != null) bLandscape = this.document.DefaultPageSettings.Landscape; if (bLandscape == false) this.AutoScrollMinSize = new Size((int)nWidth, (int)nHeight); else this.AutoScrollMinSize = new Size((int)nHeight, (int)nWidth); #endif this.AutoScrollMinSize = new Size((int)nWidth, (int)nHeight); }
public string DefaultPrinter = ""; // 缺省的打印机参数。包括打印机名和纸张名。如果没有定义页面尺寸,则用纸张的尺寸作为页面尺寸 public static int Build(string strLabelDefFilename, out LabelParam label_param, out string strError) { label_param = null; strError = ""; //int nRet = 0; XmlDocument dom = new XmlDocument(); try { dom.Load(strLabelDefFilename); } catch (Exception ex) { strError = "装载文件 '" + strLabelDefFilename + "' 到XMLDOM时发生错误: " + ex.Message; return(-1); } return(Build(dom, out label_param, out strError)); }
public string DefaultPrinter = ""; // 缺省的打印机参数。包括打印机名和纸张名。如果没有定义页面尺寸,则用纸张的尺寸作为页面尺寸 public static int Build(string strLabelDefFilename, out LabelParam label_param, out string strError) { label_param = null; strError = ""; int nRet = 0; XmlDocument dom = new XmlDocument(); try { dom.Load(strLabelDefFilename); } catch (Exception ex) { strError = "装载文件 '" + strLabelDefFilename + "' 到XMLDOM时发生错误: " + ex.Message; return -1; } return Build(dom, out label_param, out strError); }
public static int Build(XmlDocument dom, out LabelParam label_param, out string strError) { strError = ""; int nRet = 0; label_param = new LabelParam(); XmlNode label = dom.DocumentElement.SelectSingleNode("label"); if (label != null) { // int nValue = 0; double fValue = 0; // width nRet = DomUtil.GetDoubleParam(label, "width", 0, out fValue, out strError); if (nRet == -1) { return(-1); } label_param.LabelWidth = fValue; // height nRet = DomUtil.GetDoubleParam(label, "height", 0, out fValue, out strError); if (nRet == -1) { return(-1); } label_param.LabelHeight = fValue; // lineSep nRet = DomUtil.GetDoubleParam(label, "lineSep", 0, out fValue, out strError); if (nRet == -1) { return(-1); } label_param.LineSep = fValue; { DecimalPadding margins; string strPaddings = DomUtil.GetAttr(label, "paddings"); nRet = GetMarginValue( strPaddings, out margins, out strError); if (nRet == -1) { strError = "<label>元素paddings属性值格式错误: " + strError; return(-1); } label_param.LabelPaddings = margins; } #if NO try { string strPaddings = DomUtil.GetAttr(label, "paddings"); string[] parts = strPaddings.Split(new char[] { ',' }); if (parts.Length > 0) { label_param.LabelPaddings.Left = Convert.ToInt32(parts[0]); } if (parts.Length > 1) { label_param.LabelPaddings.Top = Convert.ToInt32(parts[1]); } if (parts.Length > 2) { label_param.LabelPaddings.Right = Convert.ToInt32(parts[2]); } if (parts.Length > 3) { label_param.LabelPaddings.Bottom = Convert.ToInt32(parts[3]); } } catch (Exception ex) { strError = "<label>元素paddings属性值格式错误: " + ex.Message; return(-1); } #endif string strFont = DomUtil.GetAttr(label, "font"); if (String.IsNullOrEmpty(strFont) == false) { if (Global.IsVirtualBarcodeFont(ref strFont) == true) { label_param.IsBarcodeFont = true; } try { label_param.Font = Global.BuildFont(strFont); } catch (Exception ex) { strError = "<label>元素 font 属性值格式错误: " + ex.Message; return(-1); } } } XmlNode page = dom.DocumentElement.SelectSingleNode("page"); if (page != null) { { // int nValue = 0; double fValue = 0; // width nRet = DomUtil.GetDoubleParam(page, "width", 0, out fValue, out strError); if (nRet == -1) { return(-1); } label_param.PageWidth = fValue; // height nRet = DomUtil.GetDoubleParam(page, "height", 0, out fValue, out strError); if (nRet == -1) { return(-1); } label_param.PageHeight = fValue; } { DecimalPadding margins; string strMargins = DomUtil.GetAttr(page, "margins"); nRet = GetMarginValue( strMargins, out margins, out strError); if (nRet == -1) { strError = "<page>元素margins属性值格式错误: " + strError; return(-1); } label_param.PageMargins = margins; } #if NO try { string strMargins = DomUtil.GetAttr(page, "margins"); string[] parts = strMargins.Split(new char[] { ',' }); if (parts.Length > 0) { label_param.PageMargins.Left = Convert.ToInt32(parts[0]); } if (parts.Length > 1) { label_param.PageMargins.Top = Convert.ToInt32(parts[1]); } if (parts.Length > 2) { label_param.PageMargins.Right = Convert.ToInt32(parts[2]); } if (parts.Length > 3) { label_param.PageMargins.Bottom = Convert.ToInt32(parts[3]); } } catch (Exception ex) { strError = "<page>元素margins属性值格式错误: " + ex.Message; return(-1); } #endif label_param.DefaultPrinter = DomUtil.GetAttr(page, "defaultPrinter"); #if NO bool bValue = false; nRet = DomUtil.GetBooleanParam(page, "landscape", false, out bValue, out strError); if (nRet == -1) { strError = "<page> 元素的 landscape 属性错误: " + strError; return(-1); } label_param.Landscape = bValue; #endif int nValue = 0; DomUtil.GetIntegerParam(page, "rotate", 0, out nValue, out strError); if (nRet == -1) { strError = "<page> 元素的 rotate 属性错误: " + strError; return(-1); } label_param.RotateDegree = nValue; } XmlNodeList nodes = dom.DocumentElement.SelectNodes("lineFormats/line"); label_param.LineFormats = new List <LineFormat>(); for (int i = 0; i < nodes.Count; i++) { XmlNode node = nodes[i]; string strFont = DomUtil.GetAttr(node, "font"); LineFormat format = new LineFormat(); if (Global.IsVirtualBarcodeFont(ref strFont) == true) { format.IsBarcodeFont = true; } if (string.IsNullOrEmpty(strFont) == false) { try { format.Font = Global.BuildFont(strFont); } catch (Exception ex) { strError = "<line>元素font属性值格式错误: " + ex.Message; return(-1); } } else { format.Font = null; // 继承页面的字体 } format.Align = DomUtil.GetAttr(node, "align"); string strOffset = DomUtil.GetAttr(node, "offset"); if (string.IsNullOrEmpty(strOffset) == false) { try { double left = 0; double right = 0; ParsetTwoDouble(strOffset, false, out left, out right); format.OffsetX = left; format.OffsetY = right; } catch (Exception ex) { strError = "<line>元素offset属性值格式错误: " + ex.Message; return(-1); } } string strStart = DomUtil.GetAttr(node, "start"); if (string.IsNullOrEmpty(strStart) == false) { try { double left = double.NaN; double right = double.NaN; ParsetTwoDouble(strStart, true, out left, out right); format.StartX = left; format.StartY = right; } catch (Exception ex) { strError = "<line>元素start属性值格式错误: " + ex.Message; return(-1); } } string strSize = DomUtil.GetAttr(node, "size"); if (string.IsNullOrEmpty(strSize) == false) { try { double left = double.NaN; double right = double.NaN; ParsetTwoDouble(strSize, true, out left, out right); format.Width = left; format.Height = right; } catch (Exception ex) { strError = "<line>元素size属性值格式错误: " + ex.Message; return(-1); } } format.ForeColor = DomUtil.GetAttr(node, "foreColor"); format.BackColor = DomUtil.GetAttr(node, "backColor"); label_param.LineFormats.Add(format); } return(0); }
void SetLabelParam(LabelParam param) { this.numericUpDown_pageWidth.UniverseValue = (decimal)param.PageWidth; this.numericUpDown_pageHeight.UniverseValue = (decimal)param.PageHeight; // this.checkBox_landscape.Checked = param.Landscape; this.rotateControl1.RotateDegree = param.RotateDegree; this.textBox_pagePadding.UniverseText = param.PageMargins.Left.ToString() + "," + param.PageMargins.Top.ToString() + "," + param.PageMargins.Right.ToString() + "," + param.PageMargins.Bottom.ToString(); this.numericUpDown_labelWidth.UniverseValue = (decimal)param.LabelWidth; this.numericUpDown_labelHeight.UniverseValue = (decimal)param.LabelHeight; this.textBox_labelPadding.UniverseText = param.LabelPaddings.Left.ToString() + "," + param.LabelPaddings.Top.ToString() + "," + param.LabelPaddings.Right.ToString() + "," + param.LabelPaddings.Bottom.ToString(); if (param.IsBarcodeFont == true) this.textBox_labelFont.Text = Global.GetBarcodeFontString(param.Font); else this.textBox_labelFont.Text = FontUtil.GetFontString(param.Font); this.listView_lineFormats.Items.Clear(); foreach (LineFormat line in param.LineFormats) { ListViewItem item = new ListViewItem(); string strFontString = ""; if (line.Font != null) { if (line.IsBarcodeFont == true) strFontString = Global.GetBarcodeFontString(line.Font); else strFontString = FontUtil.GetFontString(line.Font); } ListViewUtil.ChangeItemText(item, COLUMN_FONT, strFontString); ListViewUtil.ChangeItemText(item, COLUMN_ALIGN, line.Align); string strStart = GetStartString(this._currentUnit, line.StartX, line.StartY); ListViewUtil.ChangeItemText(item, COLUMN_START, strStart); string strOffset = GetOffsetString(this._currentUnit, line.OffsetX, line.OffsetY); ListViewUtil.ChangeItemText(item, COLUMN_OFFSET, strOffset); LineStore store = new LineStore(); store.UniversalStart = GetStartString(GraphicsUnit.Display, line.StartX, line.StartY); store.UniversalOffset = GetOffsetString(GraphicsUnit.Display, line.OffsetX, line.OffsetY); item.Tag = store; ListViewUtil.ChangeItemText(item, COLUMN_FORECOLOR, line.ForeColor); ListViewUtil.ChangeItemText(item, COLUMN_BACKCOLOR, line.BackColor); this.listView_lineFormats.Items.Add(item); } this.numericUpDown_lineSep.UniverseValue = (decimal)param.LineSep; this.textBox_printerInfo.Text = param.DefaultPrinter; }
LabelParam GetLabelParam() { LabelParam param = new LabelParam(); param.PageWidth = (double)this.numericUpDown_pageWidth.UniverseValue; param.PageHeight = (double)this.numericUpDown_pageHeight.UniverseValue; // param.Landscape = this.checkBox_landscape.Checked; param.RotateDegree = this.rotateControl1.RotateDegree; try { // 可能会抛出 ArgumentException 异常 DecimalPadding padding = PaddingDialog.ParsePaddingString(this.textBox_pagePadding.UniverseText); #if NO param.PageMargins = new System.Drawing.Printing.Margins(padding.Left, padding.Right, padding.Top, padding.Bottom); #endif param.PageMargins = padding; } catch { } param.LabelWidth = (double)this.numericUpDown_labelWidth.UniverseValue; param.LabelHeight = (double)this.numericUpDown_labelHeight.UniverseValue; try { // 可能会抛出 ArgumentException 异常 DecimalPadding padding = PaddingDialog.ParsePaddingString(this.textBox_labelPadding.UniverseText); #if NO param.LabelPaddings = new System.Drawing.Printing.Margins(padding.Left, padding.Right, padding.Top, padding.Bottom); #endif param.LabelPaddings = padding; } catch { } #if NO Font font = null; if (String.IsNullOrEmpty(this.textBox_labelFont.Text) == false) { // Create the FontConverter. System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Font)); font = (Font)converter.ConvertFromString(this.textBox_labelFont.Text); } else { font = Control.DefaultFont; } #endif string strFontString = this.textBox_labelFont.Text; if (Global.IsVirtualBarcodeFont(ref strFontString) == true) param.IsBarcodeFont = true; else param.IsBarcodeFont = false; param.Font = Global.BuildFont(strFontString); param.LineFormats.Clear(); foreach (ListViewItem item in this.listView_lineFormats.Items) { LineStore store = item.Tag as LineStore; Debug.Assert(store != null, ""); LineFormat format = new LineFormat(); strFontString = ListViewUtil.GetItemText(item, COLUMN_FONT); if (string.IsNullOrEmpty(strFontString) == false) { if (Global.IsVirtualBarcodeFont(ref strFontString) == true) format.IsBarcodeFont = true; else format.IsBarcodeFont = false; format.Font = Global.BuildFont(strFontString); } else format.Font = null; format.Align = ListViewUtil.GetItemText(item, COLUMN_ALIGN); SetFormatStart(format, store.UniversalStart); SetFormatOffset(format, store.UniversalOffset); format.ForeColor = ListViewUtil.GetItemText(item, COLUMN_FORECOLOR); format.BackColor = ListViewUtil.GetItemText(item, COLUMN_BACKCOLOR); param.LineFormats.Add(format); } param.LineSep = (double)this.numericUpDown_lineSep.UniverseValue; param.DefaultPrinter = this.textBox_printerInfo.Text; return param; }
/// <summary> /// 设置缺省的参数 /// </summary> /// <param name="bSetText">是否要同时设置标签文字内容</param> public void SetDefaultValue(bool bSetText = true) { LabelParam param = new LabelParam(); param.PageWidth = 640; param.PageHeight = 540; param.PageMargins = new DecimalPadding(20, 20, 20, 20); param.LabelWidth = 100; param.LabelHeight = 50; param.LabelPaddings = new DecimalPadding(5, 5, 5, 5); SetLabelParam(param); this.labelDesignControl1.LabelParam = param; if (bSetText) { string strText = BuildSampleLabelText(); using (Stream stream = new MemoryStream(Encoding.Default.GetBytes(strText))) using (StreamReader sr = new StreamReader(stream, Encoding.Default)) { string strError = ""; this.labelDesignControl1.SetLabelFile(sr, out strError); } } this.labelDesignControl1.Invalidate(); _panelVersion++; SetChanged(); }
internal void DoPrintPage( IWin32Window owner, LabelParam label_param, string strStyle, PrintPageEventArgs e) { string strError = ""; int nRet = 0; if (e.Cancel == true) return; bool bTestingGrid = false; if (StringUtil.IsInList("TestingGrid", strStyle) == true) bTestingGrid = true; int nYCount = 0; int nXCount = 0; double PageWidth = label_param.PageWidth; double PageHeight = label_param.PageHeight; #if NO if (label_param.Landscape == true) { double nTemp = PageHeight; PageHeight = PageWidth; PageWidth = nTemp; } #endif #if NO if (e.PageSettings.Landscape == true) { double nTemp = PageHeight; PageHeight = PageWidth; PageWidth = nTemp; } #endif int nPageWidth = e.PageBounds.Width; // PageBounds 中已经是按照 Landscape 处理过的方向了 if (PageWidth != 0) nPageWidth = (int)PageWidth; int nPageHeight = e.PageBounds.Height; if (PageHeight != 0) nPageHeight = (int)PageHeight; DecimalPadding PageMargins = RotatePadding(label_param.PageMargins, e.PageSettings.Landscape); // label_param.Landscape #if NO // 垂直方向的个数 nYCount = (e.PageBounds.Height - label_param.PageMargins.Top - label_param.PageMargins.Bottom) / label_param.Height; // 水平方向的个数 nXCount = (e.PageBounds.Width - label_param.PageMargins.Left - label_param.PageMargins.Right) / label_param.Width; #endif // 垂直方向的个数 nYCount = (int) ( (double)(nPageHeight - PageMargins.Top - PageMargins.Bottom) / (double)label_param.LabelHeight ); // 水平方向的个数 nXCount = (int) ( (double)(nPageWidth - PageMargins.Left - PageMargins.Right) / (double)label_param.LabelWidth ); int from = 0; int to = 0; bool bOutput = true; // 如果 e.PageSettings.PrinterSettings.FromPage == 0,会被当作打印第一页 if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages && e.PageSettings.PrinterSettings.FromPage >= 1) { from = e.PageSettings.PrinterSettings.FromPage; to = e.PageSettings.PrinterSettings.ToPage; // 交换,保证from为小 if (from > to) { int temp = to; to = from; from = temp; } if (this.m_nPageNo == 0) { this.m_nPageNo = from; Debug.Assert(this.m_nPageNo >= 1, ""); long nLabelCount = (nXCount * nYCount) * (this.m_nPageNo - 1); // 从文件中跳过这么多label的内容行 for (int i = 0; i < nLabelCount; i++) { List<string> lines = null; nRet = this.GetLabelLines(out lines, out strError); if (nRet == -1) goto ERROR1; if (nRet == 1) { e.Cancel = true; return; } } } /* if (this.m_nPageNo >= from && this.m_nPageNo <= to) { bOutput = true; } else { bOutput = false; } * */ } else { if (this.m_nPageNo == 0) this.m_nPageNo = 1; // 一般性的初始化 } // 加快运行速度 float nXDelta = e.PageSettings.PrintableArea.Left; float nYDelta = e.PageSettings.PrintableArea.Top; /* if (this.PrintController.IsPreview == true) { nXDelta = 0; nYDelta = 0; } * */ if (this.OriginAtMargins == true || this.PreviewMode == true) // false { // true 如果图形起始于页面边距。否则起始于可打印区域 nXDelta = 0; nYDelta = 0; } if (this.OriginPoint != null) { nXDelta -= this.OriginPoint.X; nYDelta -= this.OriginPoint.Y; } #if NO float nPrintableWidth = e.PageSettings.PrintableArea.Width; float nPrintableHeight = e.PageSettings.PrintableArea.Height; #endif if (this.IsDesignMode) { #if NO Pen pen = new Pen(Color.Blue, (float)1); e.Graphics.DrawRectangle(pen, 0 + 1 - nXDelta, 0 + 1 - nYDelta, e.PageBounds.Width - 2, e.PageBounds.Height - 2); pen.Dispose(); #endif // 绘制整个纸张背景 白色 using (Brush brushBack = new SolidBrush(Color.White)) { RectangleF rectPaper = new RectangleF(0 + 1 - nXDelta, 0 + 1 - nYDelta, e.PageBounds.Width - 2, e.PageBounds.Height - 2); e.Graphics.FillRectangle(brushBack, rectPaper); } #if NO // 绘制配置文件的页面区域 if (PageHeight > 0 && PageWidth > 0) { using (Brush brushBack = new SolidBrush(Color.FromArgb(128, Color.LightYellow))) { RectangleF rectPaper = new RectangleF(0 - nXDelta, 0 - nYDelta, (float)PageWidth, (float)PageHeight); e.Graphics.FillRectangle(brushBack, rectPaper); } } #endif } // 绘制可打印区域 // 鲜红色 if (bTestingGrid == true && bOutput == true) { float nXOffs = 0; float nYOffs = 0; // 如果为正式打印,左上角(0,0)已经就是可以打印区域的左上角 // 如果为preview模式,则左上角要向右向下移动,才能模拟出显示效果 #if NO if (this.OriginAtMargins == true || this.PreviewMode == true) { nXOffs = e.PageSettings.PrintableArea.Left; nYOffs = e.PageSettings.PrintableArea.Top; } #endif if (this.OriginPoint != null) { nXOffs += this.OriginPoint.X; nYOffs += this.OriginPoint.Y; } RectangleF rect = RotateRectangle(e.PageSettings.PrintableArea, e.PageSettings.Landscape); // label_param.Landscape if (this.OriginAtMargins == true || this.PreviewMode == true) { } else { rect.X = 0; rect.Y = 0; } rect.Offset(nXOffs, nYOffs); using (Pen pen = new Pen(Color.Red, (float)1)) { DrawFourAngel( e.Graphics, pen, rect, 50); // 半英寸 } } // 加入变换 e.Graphics.TranslateTransform(-nXDelta, -nYDelta); nXDelta = 0; nYDelta = 0; if (label_param.RotateDegree != 0) { float x_offs, y_offs; CenterMove(label_param.RotateDegree, (float)label_param.PageWidth, // e.PageBounds.Width, (float)label_param.PageHeight, // e.PageBounds.Height, out x_offs, out y_offs); e.Graphics.TranslateTransform(x_offs, y_offs); e.Graphics.RotateTransform((float)label_param.RotateDegree); } if (this.IsDesignMode) { // 绘制配置文件的页面区域 if (PageHeight > 0 && PageWidth > 0) { using (Brush brushBack = new SolidBrush(Color.FromArgb(128, Color.LightYellow))) { RectangleF rectPaper = new RectangleF(0 - nXDelta, 0 - nYDelta, (float)PageWidth, (float)PageHeight); e.Graphics.FillRectangle(brushBack, rectPaper); } } } // 绘制内容区域边界(也就是排除了页面边空的中间部分) // 淡绿色 if (bTestingGrid == true && bOutput == true) { using (Pen pen = new Pen(Color.FromArgb(0, 100, 0), (float)2)) // 3 { #if NO e.Graphics.DrawRectangle(pen, PageMargins.Left - nXDelta, PageMargins.Top - nYDelta, e.PageBounds.Width - PageMargins.Left - PageMargins.Right, e.PageBounds.Height - PageMargins.Top - PageMargins.Bottom); #endif e.Graphics.DrawRectangle(pen, (float)PageMargins.Left - nXDelta, (float)PageMargins.Top - nYDelta, (float)nPageWidth - (float)PageMargins.Left - (float)PageMargins.Right, (float)nPageHeight - (float)PageMargins.Top - (float)PageMargins.Bottom); } } bool bEOF = false; float y = (float)PageMargins.Top; // 每一行的循环 for (int i = 0; i < nYCount; i++) { bool bDisplay = true; if (this.IsDesignMode == true) { RectangleF rectLine = new RectangleF( (float)0 - nXDelta, (float)y - nYDelta, (float)label_param.LabelWidth * nXCount, (float)label_param.LabelHeight); if (rectLine.Top > e.Graphics.ClipBounds.Bottom) { // Debug.WriteLine("break line loop at " + i.ToString()); break; // 当前行在剪裁区域的下方,可以中断循环了 } if (rectLine.IntersectsWith(e.Graphics.ClipBounds) == false) { // Debug.WriteLine("skip line " + i.ToString()); bDisplay = false; } } float x = (float)PageMargins.Left; // 每一列的循环 for (int j = 0; j < nXCount; j++) { List<string> lines = null; nRet = this.GetLabelLines(out lines, out strError); if (nRet == -1) goto ERROR1; if (nRet == 1) bEOF = true; if (bOutput == true && bDisplay == true) { // 标签 RectangleF rectLabel = new RectangleF( (float)x - nXDelta, (float)y - nYDelta, (float)label_param.LabelWidth, (float)label_param.LabelHeight); if (rectLabel.Left > e.Graphics.ClipBounds.Right) { // Debug.WriteLine("break label loop at i=" + i.ToString() + " j=" + j.ToString()); // 当前标签在剪裁区域的右方,可以不要显示后面的标签了 bDisplay = false; } if (this.IsDesignMode == false || rectLabel.IntersectsWith(e.Graphics.ClipBounds) == true) { // Debug.WriteLine("i="+i.ToString()+" j="+j.ToString()+" rectLabel = "+rectLabel.ToString()+", clipbounds " + e.Graphics.ClipBounds.ToString()); // 标签内容区域 RectangleF rectContent = new RectangleF( (float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right - 1, (float)label_param.LabelHeight - (float)label_param.LabelPaddings.Top - (float)label_param.LabelPaddings.Bottom - 1); // 绘制标签边界 // 灰色 if (bTestingGrid == true) { // 标签白色背景 if (this.IsDesignMode == true) { using (Brush brushBack = new SolidBrush(Color.FromArgb(200, Color.White))) { e.Graphics.FillRectangle(brushBack, rectLabel); } } // 标签边界 using (Pen pen = new Pen(Color.FromArgb(200, 200, 200), this.IsDesignMode ? (float)0.5 : (float)1)) { e.Graphics.DrawRectangle(pen, rectLabel.X, rectLabel.Y, rectLabel.Width, rectLabel.Height); #if NO e.Graphics.DrawRectangle(pen, x - nXDelta, y - nYDelta, (float)label_param.LabelWidth, (float)label_param.LabelHeight); #endif } // 绘制标签内部文字区域边界 // 淡红色 using (Pen pen = new Pen(Color.FromArgb(255, 200, 200), this.IsDesignMode ? (float)0.5 : (float)1)) { e.Graphics.DrawRectangle(pen, rectContent.X, rectContent.Y, rectContent.Width, rectContent.Height); #if NO e.Graphics.DrawRectangle(pen, (float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right - 1, (float)label_param.LabelHeight - (float)label_param.LabelPaddings.Top - (float)label_param.LabelPaddings.Bottom - 1); #endif } } #if NO RectangleF clip = new RectangleF((float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right, (float)label_param.LabelHeight - (float)label_param.LabelPaddings.Top - (float)label_param.LabelPaddings.Bottom); #endif using (Region old_clip = e.Graphics.Clip) { e.Graphics.IntersectClip(rectContent); float y0 = 0; for (int k = 0; k < lines.Count; k++) { string strText = lines[k]; LineFormat format = null; if (k < label_param.LineFormats.Count) format = label_param.LineFormats[k]; Font this_font = null; bool bIsBarcodeFont = false; if (format != null && format.Font != null) { this_font = format.Font; bIsBarcodeFont = format.IsBarcodeFont; } else { this_font = label_param.Font; bIsBarcodeFont = label_param.IsBarcodeFont; } if (bIsBarcodeFont == true && string.IsNullOrEmpty(strText) == false) strText = "*" + strText + "*"; float nLineHeight = this_font.GetHeight(e.Graphics); RectangleF rect = new RectangleF((float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top + y0 - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right, nLineHeight); bool bAbsLocation = false; // 行格式的 start 和 offset if (format != null) { if (double.IsNaN(format.StartX) == false) rect.X = (float)format.StartX; if (double.IsNaN(format.StartY) == false) { rect.Y = (float)format.StartY; bAbsLocation = true; // Y 绝对定位后,行高度不参与累计 } rect.Offset((float)format.OffsetX, (float)format.OffsetY); y0 += (float)format.OffsetY; // Y 偏移后,累计值也跟着调整 } StringFormat s_format = new StringFormat(); if (format != null) { if (format.Align == "right") s_format.Alignment = StringAlignment.Far; else if (format.Align == "center") s_format.Alignment = StringAlignment.Center; else s_format.Alignment = StringAlignment.Near; s_format.Trimming = StringTrimming.EllipsisCharacter; // s_format.LineAlignment = StringAlignment.Center; } if (format != null && string.IsNullOrEmpty(format.BackColor) == false) { using (Brush brush = new SolidBrush(GetColor(format.BackColor))) { e.Graphics.FillRectangle(brush, rect); } } { Brush brushText = null; if (format != null && string.IsNullOrEmpty(format.ForeColor) == false) { brushText = new SolidBrush(GetColor(format.ForeColor)); } else brushText = System.Drawing.Brushes.Black; e.Graphics.DrawString(strText, this_font, brushText, rect, s_format); if (brushText != System.Drawing.Brushes.Black) brushText.Dispose(); } // 文字行区域边界 // 黑色点 if (bTestingGrid == true && label_param.LineSep > 0) { using (Pen pen = new Pen(Color.Black, (float)1)) { // pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom; #if NO e.Graphics.DrawRectangle(pen, rect.Left, rect.Top, rect.Width, rect.Height); #endif pen.DashPattern = new float[] { 1F, 3F, 1F, 3F }; e.Graphics.DrawLine(pen, new PointF(rect.Left, rect.Top), new PointF(rect.Right, rect.Top) ); e.Graphics.DrawLine(pen, new PointF(rect.Left + 2, rect.Bottom), new PointF(rect.Right, rect.Bottom) ); } } if (bAbsLocation == false) y0 += nLineHeight + (float)label_param.LineSep; } e.Graphics.Clip = old_clip; } // end of using clip } // end if IntersectsWith } // end if bOutput == true x += (float)label_param.LabelWidth; } CONTINUE_LINE: y += (float)label_param.LabelHeight; } // If more lines exist, print another page. if (bEOF == false) { if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages) { if (this.m_nPageNo >= to) { e.HasMorePages = false; return; } } } else { e.HasMorePages = false; return; } this.m_nPageNo++; e.HasMorePages = true; return; ERROR1: MessageBox.Show(owner, strError); }
// 设置卷滚区域 void SetAutoScroll(LabelParam label_param) { double nWidth = 0; double nHeight = 0; bool bPrinter = true; // 是否要考虑打印机的纸张尺寸 if (label_param != null && string.IsNullOrEmpty(label_param.DefaultPrinter) == true) { bPrinter = false; } if (label_param != null) { if (label_param.RotateDegree == 90 || label_param.RotateDegree == 270) { nWidth = label_param.PageHeight; nHeight = label_param.PageWidth; } else { nWidth = label_param.PageWidth; nHeight = label_param.PageHeight; } } if (this._document != null && bPrinter == true) { nWidth = Math.Max(nWidth, this._document.DefaultPageSettings.Bounds.Width); // this.document.DefaultPageSettings.PaperSize.Width } if (this._document != null && bPrinter == true) { nHeight = Math.Max(nHeight, this._document.DefaultPageSettings.Bounds.Height); // this.document.DefaultPageSettings.PaperSize.Height } #if NO bool bLandscape = false; if (label_param != null) { bLandscape = label_param.Landscape; } else #endif #if NO if (this.document != null) { bLandscape = this.document.DefaultPageSettings.Landscape; } if (bLandscape == false) { this.AutoScrollMinSize = new Size((int)nWidth, (int)nHeight); } else { this.AutoScrollMinSize = new Size((int)nHeight, (int)nWidth); } #endif this.AutoScrollMinSize = new Size((int)nWidth, (int)nHeight); }
public static int Build(XmlDocument dom, out LabelParam label_param, out string strError) { strError = ""; int nRet = 0; label_param = new LabelParam(); XmlNode label = dom.DocumentElement.SelectSingleNode("label"); if (label != null) { // int nValue = 0; double fValue = 0; // width nRet = DomUtil.GetDoubleParam(label, "width", 0, out fValue, out strError); if (nRet == -1) return -1; label_param.LabelWidth = fValue; // height nRet = DomUtil.GetDoubleParam(label, "height", 0, out fValue, out strError); if (nRet == -1) return -1; label_param.LabelHeight = fValue; // lineSep nRet = DomUtil.GetDoubleParam(label, "lineSep", 0, out fValue, out strError); if (nRet == -1) return -1; label_param.LineSep = fValue; { DecimalPadding margins; string strPaddings = DomUtil.GetAttr(label, "paddings"); nRet = GetMarginValue( strPaddings, out margins, out strError); if (nRet == -1) { strError = "<label>元素paddings属性值格式错误: " + strError; return -1; } label_param.LabelPaddings = margins; } #if NO try { string strPaddings = DomUtil.GetAttr(label, "paddings"); string[] parts = strPaddings.Split(new char[] { ',' }); if (parts.Length > 0) label_param.LabelPaddings.Left = Convert.ToInt32(parts[0]); if (parts.Length > 1) label_param.LabelPaddings.Top = Convert.ToInt32(parts[1]); if (parts.Length > 2) label_param.LabelPaddings.Right = Convert.ToInt32(parts[2]); if (parts.Length > 3) label_param.LabelPaddings.Bottom = Convert.ToInt32(parts[3]); } catch (Exception ex) { strError = "<label>元素paddings属性值格式错误: " + ex.Message; return -1; } #endif string strFont = DomUtil.GetAttr(label, "font"); if (String.IsNullOrEmpty(strFont) == false) { if (Global.IsVirtualBarcodeFont(ref strFont) == true) label_param.IsBarcodeFont = true; try { label_param.Font = Global.BuildFont(strFont); } catch (Exception ex) { strError = "<label>元素 font 属性值格式错误: " + ex.Message; return -1; } } } XmlNode page = dom.DocumentElement.SelectSingleNode("page"); if (page != null) { { // int nValue = 0; double fValue = 0; // width nRet = DomUtil.GetDoubleParam(page, "width", 0, out fValue, out strError); if (nRet == -1) return -1; label_param.PageWidth = fValue; // height nRet = DomUtil.GetDoubleParam(page, "height", 0, out fValue, out strError); if (nRet == -1) return -1; label_param.PageHeight = fValue; } { DecimalPadding margins; string strMargins = DomUtil.GetAttr(page, "margins"); nRet = GetMarginValue( strMargins, out margins, out strError); if (nRet == -1) { strError = "<page>元素margins属性值格式错误: " + strError; return -1; } label_param.PageMargins = margins; } #if NO try { string strMargins = DomUtil.GetAttr(page, "margins"); string[] parts = strMargins.Split(new char[] { ',' }); if (parts.Length > 0) label_param.PageMargins.Left = Convert.ToInt32(parts[0]); if (parts.Length > 1) label_param.PageMargins.Top = Convert.ToInt32(parts[1]); if (parts.Length > 2) label_param.PageMargins.Right = Convert.ToInt32(parts[2]); if (parts.Length > 3) label_param.PageMargins.Bottom = Convert.ToInt32(parts[3]); } catch (Exception ex) { strError = "<page>元素margins属性值格式错误: " + ex.Message; return -1; } #endif label_param.DefaultPrinter = DomUtil.GetAttr(page, "defaultPrinter"); #if NO bool bValue = false; nRet = DomUtil.GetBooleanParam(page, "landscape", false, out bValue, out strError); if (nRet == -1) { strError = "<page> 元素的 landscape 属性错误: " + strError; return -1; } label_param.Landscape = bValue; #endif int nValue = 0; DomUtil.GetIntegerParam(page, "rotate", 0, out nValue, out strError); if (nRet == -1) { strError = "<page> 元素的 rotate 属性错误: " + strError; return -1; } label_param.RotateDegree = nValue; } XmlNodeList nodes = dom.DocumentElement.SelectNodes("lineFormats/line"); label_param.LineFormats = new List<LineFormat>(); for (int i = 0; i < nodes.Count; i++) { XmlNode node = nodes[i]; string strFont = DomUtil.GetAttr(node, "font"); LineFormat format = new LineFormat(); if (Global.IsVirtualBarcodeFont(ref strFont) == true) format.IsBarcodeFont = true; if (string.IsNullOrEmpty(strFont) == false) { try { format.Font = Global.BuildFont(strFont); } catch (Exception ex) { strError = "<line>元素font属性值格式错误: " + ex.Message; return -1; } } else format.Font = null; // 继承页面的字体 format.Align = DomUtil.GetAttr(node, "align"); string strOffset = DomUtil.GetAttr(node, "offset"); if (string.IsNullOrEmpty(strOffset) == false) { try { double left = 0; double right = 0; ParsetTwoDouble(strOffset, false, out left, out right); format.OffsetX = left; format.OffsetY = right; } catch (Exception ex) { strError = "<line>元素offset属性值格式错误: " + ex.Message; return -1; } } string strStart = DomUtil.GetAttr(node, "start"); if (string.IsNullOrEmpty(strStart) == false) { try { double left = double.NaN; double right = double.NaN; ParsetTwoDouble(strStart, true, out left, out right); format.StartX = left; format.StartY = right; } catch (Exception ex) { strError = "<line>元素start属性值格式错误: " + ex.Message; return -1; } } format.ForeColor = DomUtil.GetAttr(node, "foreColor"); format.BackColor = DomUtil.GetAttr(node, "backColor"); label_param.LineFormats.Add(format); } return 0; }
// 设置卷滚区域 void SetAutoScroll(LabelParam label_param) { double nWidth = 0; double nHeight = 0; bool bPrinter = true; // 是否要考虑打印机的纸张尺寸 if (label_param != null && string.IsNullOrEmpty(label_param.DefaultPrinter) == true) { bPrinter = false; } if (label_param != null) { if (label_param.RotateDegree == 90 || label_param.RotateDegree == 270) { nWidth = label_param.PageHeight; nHeight = label_param.PageWidth; } else { nWidth = label_param.PageWidth; nHeight = label_param.PageHeight; } } // 2017/4/12 PageSettings setting = null; if (this._document != null && this._document.DefaultPageSettings != null) { try { setting = this._document.DefaultPageSettings; int nTemp = setting.Bounds.Width; } catch (InvalidPrinterException) { setting = null; // setting = new PageSettings(this._document.PrinterSettings); } } if (setting != null && bPrinter == true) { nWidth = Math.Max(nWidth, setting.Bounds.Width); // this.document.DefaultPageSettings.PaperSize.Width } if (setting != null && bPrinter == true) { nHeight = Math.Max(nHeight, setting.Bounds.Height); // this.document.DefaultPageSettings.PaperSize.Height } #if NO bool bLandscape = false; if (label_param != null) { bLandscape = label_param.Landscape; } else #endif #if NO if (this.document != null) { bLandscape = this.document.DefaultPageSettings.Landscape; } if (bLandscape == false) { this.AutoScrollMinSize = new Size((int)nWidth, (int)nHeight); } else { this.AutoScrollMinSize = new Size((int)nHeight, (int)nWidth); } #endif this.AutoScrollMinSize = new Size((int)nWidth, (int)nHeight); }
// parameters: // strLabelFilename 标签文件名 // strDefFilename 定义文件名 int BeginPrint( string strLabelFilename, string strDefFilename, out string strError) { strError = ""; if (String.IsNullOrEmpty(strDefFilename) == true) { strError = "尚未指定标签定义文件名"; return -1; } if (String.IsNullOrEmpty(strLabelFilename) == true) { strError = "尚未指定标签文件名"; return -1; } LabelParam label_param = null; int nRet = LabelParam.Build(strDefFilename, out label_param, out strError); if (nRet == -1) return -1; this.label_param = label_param; if (this.document != null) { this.document.Close(); this.document = null; } this.document = new PrintLabelDocument(); nRet = this.document.Open(strLabelFilename, out strError); if (nRet == -1) return -1; this.document.PrintPage -= new System.Drawing.Printing.PrintPageEventHandler(document_PrintPage); this.document.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(document_PrintPage); if (this.checkBox_testingGrid.Checked == true) this.m_strPrintStyle = "TestingGrid"; else this.m_strPrintStyle = ""; return 0; }
// 绘制一个标签的全部文字 // parameters: // rectContent 整个文字区域的矩形 void PaintLabelContent(Graphics g, RectangleF rectContent, List <string> lines, LabelParam label_param, float x, float y, float nXDelta, float nYDelta, bool bTestingGrid) { using (Region old_clip = g.Clip) { g.IntersectClip(rectContent); float y0 = 0; for (int k = 0; k < lines.Count; k++) { string strText = lines[k]; LineFormat format = null; if (k < label_param.LineFormats.Count) { format = label_param.LineFormats[k]; } Font this_font = null; bool bIsBarcodeFont = false; if (format != null && format.Font != null) { this_font = format.Font; bIsBarcodeFont = format.IsBarcodeFont; } else { this_font = label_param.Font; bIsBarcodeFont = label_param.IsBarcodeFont; } //if (bIsBarcodeFont == true && string.IsNullOrEmpty(strText) == false) // strText = "*" + strText + "*"; Font save_font = this_font; if (this.DesignMode) { this_font = ReCreateFont(this_font, g); } try { float nLineHeight = this_font.GetHeight(g); RectangleF rect = new RectangleF((float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top + y0 - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right, nLineHeight); bool bAbsLocation = false; // 行格式的 start 和 offset if (format != null) { if (double.IsNaN(format.StartX) == false) { rect.X = (float)format.StartX; } if (double.IsNaN(format.StartY) == false) { rect.Y = (float)format.StartY; bAbsLocation = true; // Y 绝对定位后,行高度不参与累计 } rect.Offset((float)format.OffsetX, (float)format.OffsetY); y0 += (float)format.OffsetY; // Y 偏移后,累计值也跟着调整 if (double.IsNaN(format.Width) == false) { rect.Width = (float)format.Width; } if (double.IsNaN(format.Height) == false) { rect.Height = (float)format.Height; } } StringFormat s_format = new StringFormat(); if (format != null) { if (format.Align == "right") { s_format.Alignment = StringAlignment.Far; } else if (format.Align == "center") { s_format.Alignment = StringAlignment.Center; } else { s_format.Alignment = StringAlignment.Near; } s_format.Trimming = StringTrimming.EllipsisCharacter; // s_format.LineAlignment = StringAlignment.Center; } if (format != null && string.IsNullOrEmpty(format.BackColor) == false) { using (Brush brush = new SolidBrush(GetColor(format.BackColor))) { g.FillRectangle(brush, rect); } } if (bIsBarcodeFont) { // strText = strText.Trim(new char[] { '*' }); // 应该是 1/100 inch 单位 float textHeight = MeasureOcrTextHeight( g, rect, strText); RectangleF target = new RectangleF(rect.X, rect.Y, rect.Width, rect.Height - textHeight); GraphicsUnit u = g.PageUnit; Hashtable param = new Hashtable(); param["type"] = "39"; param["code"] = strText; param["width"] = ((int)(target.Width * 2)).ToString(); param["height"] = ((int)(target.Height * 2)).ToString(); param["margin"] = "0"; using (Image image = BuildQrCodeImage(param)) { RectangleF source = new RectangleF(0, 0, image.Width, image.Height); g.DrawImage(image, target, source, GraphicsUnit.Pixel); } RectangleF rectText = new RectangleF(rect.X, rect.Y + target.Height, rect.Width, textHeight); PaintOcrFont( g, rectText, strText); } else { Brush brushText = null; try { if (format != null && string.IsNullOrEmpty(format.ForeColor) == false) { brushText = new SolidBrush(GetColor(format.ForeColor)); } else { brushText = System.Drawing.Brushes.Black; } g.DrawString(strText, this_font, brushText, rect, s_format); } finally { if (brushText != System.Drawing.Brushes.Black) { brushText.Dispose(); } } } // 文字行区域边界 // 黑色点 if (bTestingGrid == true && label_param.LineSep > 0) { using (Pen pen = new Pen(Color.Black, (float)1)) { pen.DashPattern = new float[] { 1F, 3F, 1F, 3F }; g.DrawLine(pen, new PointF(rect.Left, rect.Top), new PointF(rect.Right, rect.Top) ); g.DrawLine(pen, new PointF(rect.Left + 2, rect.Bottom), new PointF(rect.Right, rect.Bottom) ); } } if (bAbsLocation == false) { y0 += // nLineHeight rect.Height + (float)label_param.LineSep; } } finally { if (this_font != save_font) { this_font.Dispose(); } } } g.Clip = old_clip; } // end of using clip }
internal void DoPrintPage( IWin32Window owner, LabelParam label_param, string strStyle, PrintPageEventArgs e) { string strError = ""; int nRet = 0; if (e.Cancel == true) { return; } bool bTestingGrid = false; if (StringUtil.IsInList("TestingGrid", strStyle) == true) { bTestingGrid = true; } int nYCount = 0; int nXCount = 0; double PageWidth = label_param.PageWidth; double PageHeight = label_param.PageHeight; int nPageWidth = e.PageBounds.Width; // PageBounds 中已经是按照 Landscape 处理过的方向了 if (PageWidth != 0) { nPageWidth = (int)PageWidth; } int nPageHeight = e.PageBounds.Height; if (PageHeight != 0) { nPageHeight = (int)PageHeight; } DecimalPadding PageMargins = RotatePadding(label_param.PageMargins, e.PageSettings.Landscape); // label_param.Landscape #if NO // 垂直方向的个数 nYCount = (e.PageBounds.Height - label_param.PageMargins.Top - label_param.PageMargins.Bottom) / label_param.Height; // 水平方向的个数 nXCount = (e.PageBounds.Width - label_param.PageMargins.Left - label_param.PageMargins.Right) / label_param.Width; #endif // 垂直方向的个数 nYCount = (int) ( (double)(nPageHeight - PageMargins.Top - PageMargins.Bottom) / (double)label_param.LabelHeight ); // 水平方向的个数 nXCount = (int) ( (double)(nPageWidth - PageMargins.Left - PageMargins.Right) / (double)label_param.LabelWidth ); int from = 0; int to = 0; bool bOutput = true; // 如果 e.PageSettings.PrinterSettings.FromPage == 0,会被当作打印第一页 if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages && e.PageSettings.PrinterSettings.FromPage >= 1) { from = e.PageSettings.PrinterSettings.FromPage; to = e.PageSettings.PrinterSettings.ToPage; // 交换,保证from为小 if (from > to) { int temp = to; to = from; from = temp; } if (this.m_nPageNo == 0) { this.m_nPageNo = from; Debug.Assert(this.m_nPageNo >= 1, ""); long nLabelCount = (nXCount * nYCount) * (this.m_nPageNo - 1); // 从文件中跳过这么多label的内容行 for (int i = 0; i < nLabelCount; i++) { List <string> lines = null; nRet = this.GetLabelLines(out lines, out strError); if (nRet == -1) { goto ERROR1; } if (nRet == 1) { e.Cancel = true; return; } } } /* * if (this.m_nPageNo >= from * && this.m_nPageNo <= to) * { * bOutput = true; * } * else * { * bOutput = false; * } * */ } else { if (this.m_nPageNo == 0) { this.m_nPageNo = 1; // 一般性的初始化 } } // 加快运行速度 float nXDelta = e.PageSettings.PrintableArea.Left; float nYDelta = e.PageSettings.PrintableArea.Top; /* * if (this.PrintController.IsPreview == true) * { * nXDelta = 0; * nYDelta = 0; * } * */ if (this.OriginAtMargins == true || this.PreviewMode == true) // false { // true 如果图形起始于页面边距。否则起始于可打印区域 nXDelta = 0; nYDelta = 0; } if (this.OriginPoint != null) { nXDelta -= this.OriginPoint.X; nYDelta -= this.OriginPoint.Y; } #if NO float nPrintableWidth = e.PageSettings.PrintableArea.Width; float nPrintableHeight = e.PageSettings.PrintableArea.Height; #endif if (this.IsDesignMode) { // 绘制整个纸张背景 白色 using (Brush brushBack = new SolidBrush(Color.White)) { RectangleF rectPaper = new RectangleF(0 + 1 - nXDelta, 0 + 1 - nYDelta, e.PageBounds.Width - 2, e.PageBounds.Height - 2); e.Graphics.FillRectangle(brushBack, rectPaper); } } // 绘制可打印区域 // 鲜红色 if (bTestingGrid == true && bOutput == true) { float nXOffs = 0; float nYOffs = 0; // 如果为正式打印,左上角(0,0)已经就是可以打印区域的左上角 // 如果为preview模式,则左上角要向右向下移动,才能模拟出显示效果 #if NO if (this.OriginAtMargins == true || this.PreviewMode == true) { nXOffs = e.PageSettings.PrintableArea.Left; nYOffs = e.PageSettings.PrintableArea.Top; } #endif if (this.OriginPoint != null) { nXOffs += this.OriginPoint.X; nYOffs += this.OriginPoint.Y; } RectangleF rect = RotateRectangle(e.PageSettings.PrintableArea, e.PageSettings.Landscape); // label_param.Landscape if (this.OriginAtMargins == true || this.PreviewMode == true) { } else { rect.X = 0; rect.Y = 0; } rect.Offset(nXOffs, nYOffs); using (Pen pen = new Pen(Color.Red, (float)1)) { DrawFourAngel( e.Graphics, pen, rect, 50); // 半英寸 } } // 加入变换 e.Graphics.TranslateTransform(-nXDelta, -nYDelta); nXDelta = 0; nYDelta = 0; if (label_param.RotateDegree != 0) { float x_offs, y_offs; CenterMove(label_param.RotateDegree, (float)label_param.PageWidth, // e.PageBounds.Width, (float)label_param.PageHeight, // e.PageBounds.Height, out x_offs, out y_offs); e.Graphics.TranslateTransform(x_offs, y_offs); e.Graphics.RotateTransform((float)label_param.RotateDegree); } if (this.IsDesignMode) { // 绘制配置文件的页面区域 if (PageHeight > 0 && PageWidth > 0) { using (Brush brushBack = new SolidBrush(Color.FromArgb(128, Color.LightYellow))) { RectangleF rectPaper = new RectangleF(0 - nXDelta, 0 - nYDelta, (float)PageWidth, (float)PageHeight); e.Graphics.FillRectangle(brushBack, rectPaper); } } } // 绘制内容区域边界(也就是排除了页面边空的中间部分) // 淡绿色 if (bTestingGrid == true && bOutput == true) { using (Pen pen = new Pen(Color.FromArgb(0, 100, 0), (float)2)) // 3 { #if NO e.Graphics.DrawRectangle(pen, PageMargins.Left - nXDelta, PageMargins.Top - nYDelta, e.PageBounds.Width - PageMargins.Left - PageMargins.Right, e.PageBounds.Height - PageMargins.Top - PageMargins.Bottom); #endif e.Graphics.DrawRectangle(pen, (float)PageMargins.Left - nXDelta, (float)PageMargins.Top - nYDelta, (float)nPageWidth - (float)PageMargins.Left - (float)PageMargins.Right, (float)nPageHeight - (float)PageMargins.Top - (float)PageMargins.Bottom); } } bool bEOF = false; float y = (float)PageMargins.Top; // 每一行的循环 for (int i = 0; i < nYCount; i++) { bool bDisplay = true; if (this.IsDesignMode == true) { RectangleF rectLine = new RectangleF( (float)0 - nXDelta, (float)y - nYDelta, (float)label_param.LabelWidth * nXCount, (float)label_param.LabelHeight); if (rectLine.Top > e.Graphics.ClipBounds.Bottom) { // Debug.WriteLine("break line loop at " + i.ToString()); break; // 当前行在剪裁区域的下方,可以中断循环了 } if (rectLine.IntersectsWith(e.Graphics.ClipBounds) == false) { // Debug.WriteLine("skip line " + i.ToString()); bDisplay = false; } } float x = (float)PageMargins.Left; // 每一列的循环 for (int j = 0; j < nXCount; j++) { List <string> lines = null; nRet = this.GetLabelLines(out lines, out strError); if (nRet == -1) { goto ERROR1; } if (nRet == 1) { bEOF = true; } if (bOutput == true && bDisplay == true) { // 标签 RectangleF rectLabel = new RectangleF( (float)x - nXDelta, (float)y - nYDelta, (float)label_param.LabelWidth, (float)label_param.LabelHeight); if (rectLabel.Left > e.Graphics.ClipBounds.Right) { // Debug.WriteLine("break label loop at i=" + i.ToString() + " j=" + j.ToString()); // 当前标签在剪裁区域的右方,可以不要显示后面的标签了 bDisplay = false; } if (this.IsDesignMode == false || rectLabel.IntersectsWith(e.Graphics.ClipBounds) == true) { // Debug.WriteLine("i="+i.ToString()+" j="+j.ToString()+" rectLabel = "+rectLabel.ToString()+", clipbounds " + e.Graphics.ClipBounds.ToString()); // 标签内容区域 RectangleF rectContent = new RectangleF( (float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right - 1, (float)label_param.LabelHeight - (float)label_param.LabelPaddings.Top - (float)label_param.LabelPaddings.Bottom - 1); // 绘制标签边界 // 灰色 if (bTestingGrid == true) { // 标签白色背景 if (this.IsDesignMode == true) { using (Brush brushBack = new SolidBrush(Color.FromArgb(200, Color.White))) { e.Graphics.FillRectangle(brushBack, rectLabel); } } // 标签边界 using (Pen pen = new Pen(Color.FromArgb(200, 200, 200), this.IsDesignMode ? (float)0.5 : (float)1)) { e.Graphics.DrawRectangle(pen, rectLabel.X, rectLabel.Y, rectLabel.Width, rectLabel.Height); } // 绘制标签内部文字区域边界 // 淡红色 using (Pen pen = new Pen(Color.FromArgb(255, 200, 200), this.IsDesignMode ? (float)0.5 : (float)1)) { e.Graphics.DrawRectangle(pen, rectContent.X, rectContent.Y, rectContent.Width, rectContent.Height); } } // 绘制一个标签的全部文字 PaintLabelContent(e.Graphics, rectContent, lines, label_param, x, y, nXDelta, nYDelta, bTestingGrid); } // end if IntersectsWith } // end if bOutput == true x += (float)label_param.LabelWidth; } //CONTINUE_LINE: y += (float)label_param.LabelHeight; } // If more lines exist, print another page. if (bEOF == false) { if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages) { if (this.m_nPageNo >= to) { e.HasMorePages = false; return; } } } else { e.HasMorePages = false; return; } this.m_nPageNo++; e.HasMorePages = true; return; ERROR1: MessageBox.Show(owner, strError); }
internal void DoPrintPage( IWin32Window owner, LabelParam label_param, string strStyle, PrintPageEventArgs e) { string strError = ""; int nRet = 0; if (e.Cancel == true) { return; } bool bTestingGrid = false; if (StringUtil.IsInList("TestingGrid", strStyle) == true) { bTestingGrid = true; } int nYCount = 0; int nXCount = 0; double PageWidth = label_param.PageWidth; double PageHeight = label_param.PageHeight; #if NO if (label_param.Landscape == true) { double nTemp = PageHeight; PageHeight = PageWidth; PageWidth = nTemp; } #endif #if NO if (e.PageSettings.Landscape == true) { double nTemp = PageHeight; PageHeight = PageWidth; PageWidth = nTemp; } #endif int nPageWidth = e.PageBounds.Width; // PageBounds 中已经是按照 Landscape 处理过的方向了 if (PageWidth != 0) { nPageWidth = (int)PageWidth; } int nPageHeight = e.PageBounds.Height; if (PageHeight != 0) { nPageHeight = (int)PageHeight; } DecimalPadding PageMargins = RotatePadding(label_param.PageMargins, e.PageSettings.Landscape); // label_param.Landscape #if NO // 垂直方向的个数 nYCount = (e.PageBounds.Height - label_param.PageMargins.Top - label_param.PageMargins.Bottom) / label_param.Height; // 水平方向的个数 nXCount = (e.PageBounds.Width - label_param.PageMargins.Left - label_param.PageMargins.Right) / label_param.Width; #endif // 垂直方向的个数 nYCount = (int) ( (double)(nPageHeight - PageMargins.Top - PageMargins.Bottom) / (double)label_param.LabelHeight ); // 水平方向的个数 nXCount = (int) ( (double)(nPageWidth - PageMargins.Left - PageMargins.Right) / (double)label_param.LabelWidth ); int from = 0; int to = 0; bool bOutput = true; // 如果 e.PageSettings.PrinterSettings.FromPage == 0,会被当作打印第一页 if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages && e.PageSettings.PrinterSettings.FromPage >= 1) { from = e.PageSettings.PrinterSettings.FromPage; to = e.PageSettings.PrinterSettings.ToPage; // 交换,保证from为小 if (from > to) { int temp = to; to = from; from = temp; } if (this.m_nPageNo == 0) { this.m_nPageNo = from; Debug.Assert(this.m_nPageNo >= 1, ""); long nLabelCount = (nXCount * nYCount) * (this.m_nPageNo - 1); // 从文件中跳过这么多label的内容行 for (int i = 0; i < nLabelCount; i++) { List <string> lines = null; nRet = this.GetLabelLines(out lines, out strError); if (nRet == -1) { goto ERROR1; } if (nRet == 1) { e.Cancel = true; return; } } } /* * if (this.m_nPageNo >= from * && this.m_nPageNo <= to) * { * bOutput = true; * } * else * { * bOutput = false; * } * */ } else { if (this.m_nPageNo == 0) { this.m_nPageNo = 1; // 一般性的初始化 } } // 加快运行速度 float nXDelta = e.PageSettings.PrintableArea.Left; float nYDelta = e.PageSettings.PrintableArea.Top; /* * if (this.PrintController.IsPreview == true) * { * nXDelta = 0; * nYDelta = 0; * } * */ if (this.OriginAtMargins == true || this.PreviewMode == true) // false { // true 如果图形起始于页面边距。否则起始于可打印区域 nXDelta = 0; nYDelta = 0; } if (this.OriginPoint != null) { nXDelta -= this.OriginPoint.X; nYDelta -= this.OriginPoint.Y; } #if NO float nPrintableWidth = e.PageSettings.PrintableArea.Width; float nPrintableHeight = e.PageSettings.PrintableArea.Height; #endif if (this.IsDesignMode) { // 绘制整个纸张背景 白色 using (Brush brushBack = new SolidBrush(Color.White)) { RectangleF rectPaper = new RectangleF(0 + 1 - nXDelta, 0 + 1 - nYDelta, e.PageBounds.Width - 2, e.PageBounds.Height - 2); e.Graphics.FillRectangle(brushBack, rectPaper); } } // 绘制可打印区域 // 鲜红色 if (bTestingGrid == true && bOutput == true) { float nXOffs = 0; float nYOffs = 0; // 如果为正式打印,左上角(0,0)已经就是可以打印区域的左上角 // 如果为preview模式,则左上角要向右向下移动,才能模拟出显示效果 #if NO if (this.OriginAtMargins == true || this.PreviewMode == true) { nXOffs = e.PageSettings.PrintableArea.Left; nYOffs = e.PageSettings.PrintableArea.Top; } #endif if (this.OriginPoint != null) { nXOffs += this.OriginPoint.X; nYOffs += this.OriginPoint.Y; } RectangleF rect = RotateRectangle(e.PageSettings.PrintableArea, e.PageSettings.Landscape); // label_param.Landscape if (this.OriginAtMargins == true || this.PreviewMode == true) { } else { rect.X = 0; rect.Y = 0; } rect.Offset(nXOffs, nYOffs); using (Pen pen = new Pen(Color.Red, (float)1)) { DrawFourAngel( e.Graphics, pen, rect, 50); // 半英寸 } } // 加入变换 e.Graphics.TranslateTransform(-nXDelta, -nYDelta); nXDelta = 0; nYDelta = 0; if (label_param.RotateDegree != 0) { float x_offs, y_offs; CenterMove(label_param.RotateDegree, (float)label_param.PageWidth, // e.PageBounds.Width, (float)label_param.PageHeight, // e.PageBounds.Height, out x_offs, out y_offs); e.Graphics.TranslateTransform(x_offs, y_offs); e.Graphics.RotateTransform((float)label_param.RotateDegree); } if (this.IsDesignMode) { // 绘制配置文件的页面区域 if (PageHeight > 0 && PageWidth > 0) { using (Brush brushBack = new SolidBrush(Color.FromArgb(128, Color.LightYellow))) { RectangleF rectPaper = new RectangleF(0 - nXDelta, 0 - nYDelta, (float)PageWidth, (float)PageHeight); e.Graphics.FillRectangle(brushBack, rectPaper); } } } // 绘制内容区域边界(也就是排除了页面边空的中间部分) // 淡绿色 if (bTestingGrid == true && bOutput == true) { using (Pen pen = new Pen(Color.FromArgb(0, 100, 0), (float)2)) // 3 { #if NO e.Graphics.DrawRectangle(pen, PageMargins.Left - nXDelta, PageMargins.Top - nYDelta, e.PageBounds.Width - PageMargins.Left - PageMargins.Right, e.PageBounds.Height - PageMargins.Top - PageMargins.Bottom); #endif e.Graphics.DrawRectangle(pen, (float)PageMargins.Left - nXDelta, (float)PageMargins.Top - nYDelta, (float)nPageWidth - (float)PageMargins.Left - (float)PageMargins.Right, (float)nPageHeight - (float)PageMargins.Top - (float)PageMargins.Bottom); } } bool bEOF = false; float y = (float)PageMargins.Top; // 每一行的循环 for (int i = 0; i < nYCount; i++) { bool bDisplay = true; if (this.IsDesignMode == true) { RectangleF rectLine = new RectangleF( (float)0 - nXDelta, (float)y - nYDelta, (float)label_param.LabelWidth * nXCount, (float)label_param.LabelHeight); if (rectLine.Top > e.Graphics.ClipBounds.Bottom) { // Debug.WriteLine("break line loop at " + i.ToString()); break; // 当前行在剪裁区域的下方,可以中断循环了 } if (rectLine.IntersectsWith(e.Graphics.ClipBounds) == false) { // Debug.WriteLine("skip line " + i.ToString()); bDisplay = false; } } float x = (float)PageMargins.Left; // 每一列的循环 for (int j = 0; j < nXCount; j++) { List <string> lines = null; nRet = this.GetLabelLines(out lines, out strError); if (nRet == -1) { goto ERROR1; } if (nRet == 1) { bEOF = true; } if (bOutput == true && bDisplay == true) { // 标签 RectangleF rectLabel = new RectangleF( (float)x - nXDelta, (float)y - nYDelta, (float)label_param.LabelWidth, (float)label_param.LabelHeight); if (rectLabel.Left > e.Graphics.ClipBounds.Right) { // Debug.WriteLine("break label loop at i=" + i.ToString() + " j=" + j.ToString()); // 当前标签在剪裁区域的右方,可以不要显示后面的标签了 bDisplay = false; } if (this.IsDesignMode == false || rectLabel.IntersectsWith(e.Graphics.ClipBounds) == true) { // Debug.WriteLine("i="+i.ToString()+" j="+j.ToString()+" rectLabel = "+rectLabel.ToString()+", clipbounds " + e.Graphics.ClipBounds.ToString()); // 标签内容区域 RectangleF rectContent = new RectangleF( (float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right - 1, (float)label_param.LabelHeight - (float)label_param.LabelPaddings.Top - (float)label_param.LabelPaddings.Bottom - 1); // 绘制标签边界 // 灰色 if (bTestingGrid == true) { // 标签白色背景 if (this.IsDesignMode == true) { using (Brush brushBack = new SolidBrush(Color.FromArgb(200, Color.White))) { e.Graphics.FillRectangle(brushBack, rectLabel); } } // 标签边界 using (Pen pen = new Pen(Color.FromArgb(200, 200, 200), this.IsDesignMode ? (float)0.5 : (float)1)) { e.Graphics.DrawRectangle(pen, rectLabel.X, rectLabel.Y, rectLabel.Width, rectLabel.Height); #if NO e.Graphics.DrawRectangle(pen, x - nXDelta, y - nYDelta, (float)label_param.LabelWidth, (float)label_param.LabelHeight); #endif } // 绘制标签内部文字区域边界 // 淡红色 using (Pen pen = new Pen(Color.FromArgb(255, 200, 200), this.IsDesignMode ? (float)0.5 : (float)1)) { e.Graphics.DrawRectangle(pen, rectContent.X, rectContent.Y, rectContent.Width, rectContent.Height); #if NO e.Graphics.DrawRectangle(pen, (float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right - 1, (float)label_param.LabelHeight - (float)label_param.LabelPaddings.Top - (float)label_param.LabelPaddings.Bottom - 1); #endif } } #if NO RectangleF clip = new RectangleF((float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right, (float)label_param.LabelHeight - (float)label_param.LabelPaddings.Top - (float)label_param.LabelPaddings.Bottom); #endif using (Region old_clip = e.Graphics.Clip) { e.Graphics.IntersectClip(rectContent); float y0 = 0; for (int k = 0; k < lines.Count; k++) { string strText = lines[k]; LineFormat format = null; if (k < label_param.LineFormats.Count) { format = label_param.LineFormats[k]; } Font this_font = null; bool bIsBarcodeFont = false; if (format != null && format.Font != null) { this_font = format.Font; bIsBarcodeFont = format.IsBarcodeFont; } else { this_font = label_param.Font; bIsBarcodeFont = label_param.IsBarcodeFont; } if (bIsBarcodeFont == true && string.IsNullOrEmpty(strText) == false) { strText = "*" + strText + "*"; } float nLineHeight = this_font.GetHeight(e.Graphics); RectangleF rect = new RectangleF((float)x + (float)label_param.LabelPaddings.Left - nXDelta, (float)y + (float)label_param.LabelPaddings.Top + y0 - nYDelta, (float)label_param.LabelWidth - (float)label_param.LabelPaddings.Left - (float)label_param.LabelPaddings.Right, nLineHeight); bool bAbsLocation = false; // 行格式的 start 和 offset if (format != null) { if (double.IsNaN(format.StartX) == false) { rect.X = (float)format.StartX; } if (double.IsNaN(format.StartY) == false) { rect.Y = (float)format.StartY; bAbsLocation = true; // Y 绝对定位后,行高度不参与累计 } rect.Offset((float)format.OffsetX, (float)format.OffsetY); y0 += (float)format.OffsetY; // Y 偏移后,累计值也跟着调整 } StringFormat s_format = new StringFormat(); if (format != null) { if (format.Align == "right") { s_format.Alignment = StringAlignment.Far; } else if (format.Align == "center") { s_format.Alignment = StringAlignment.Center; } else { s_format.Alignment = StringAlignment.Near; } s_format.Trimming = StringTrimming.EllipsisCharacter; // s_format.LineAlignment = StringAlignment.Center; } if (format != null && string.IsNullOrEmpty(format.BackColor) == false) { using (Brush brush = new SolidBrush(GetColor(format.BackColor))) { e.Graphics.FillRectangle(brush, rect); } } { Brush brushText = null; try { if (format != null && string.IsNullOrEmpty(format.ForeColor) == false) { brushText = new SolidBrush(GetColor(format.ForeColor)); } else { brushText = System.Drawing.Brushes.Black; } e.Graphics.DrawString(strText, this_font, brushText, rect, s_format); } finally { if (brushText != System.Drawing.Brushes.Black) { brushText.Dispose(); } } } // 文字行区域边界 // 黑色点 if (bTestingGrid == true && label_param.LineSep > 0) { using (Pen pen = new Pen(Color.Black, (float)1)) { // pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Custom; #if NO e.Graphics.DrawRectangle(pen, rect.Left, rect.Top, rect.Width, rect.Height); #endif pen.DashPattern = new float[] { 1F, 3F, 1F, 3F }; e.Graphics.DrawLine(pen, new PointF(rect.Left, rect.Top), new PointF(rect.Right, rect.Top) ); e.Graphics.DrawLine(pen, new PointF(rect.Left + 2, rect.Bottom), new PointF(rect.Right, rect.Bottom) ); } } if (bAbsLocation == false) { y0 += nLineHeight + (float)label_param.LineSep; } } e.Graphics.Clip = old_clip; } // end of using clip } // end if IntersectsWith } // end if bOutput == true x += (float)label_param.LabelWidth; } //CONTINUE_LINE: y += (float)label_param.LabelHeight; } // If more lines exist, print another page. if (bEOF == false) { if (e.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages) { if (this.m_nPageNo >= to) { e.HasMorePages = false; return; } } } else { e.HasMorePages = false; return; } this.m_nPageNo++; e.HasMorePages = true; return; ERROR1: MessageBox.Show(owner, strError); }