private void printButton_Click(object sender, EventArgs e) { PrinterSettings ps = new PrinterSettings { PrinterName = labelPrinter }; LabelTemplate labelTemplate = labelComboBox.SelectedItem as LabelTemplate; double labelWidth = labelTemplate.StockWidth; double labelHeight = labelTemplate.StockHeight; ps.Width = (int)(203 * labelHeight); ps.Length = (int)(203 * labelWidth); ps.Darkness = printDarkness; ps.PrintSpeed = printSpeed; List <byte> page = new List <byte>(); page.AddRange(ZPLCommands.ClearPrinter(ps)); Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height); labelRenderer.DrawLabel(labelTemplate, bmp, true, GetLabelParameters()); page.AddRange(ZPLCommands.GraphicStore(bmp, 'R', "img")); page.AddRange(ZPLCommands.GraphicWrite(0, 0, "img", 'R')); page.AddRange(ZPLCommands.PrintBuffer((int)copiesUpDown.Value)); new SpoolPrinter(ps).Print(page.ToArray()); }
private void labelComboBox_SelectedIndexChanged(object sender, EventArgs e) { LabelTemplate labelTemplate = labelComboBox.SelectedItem as LabelTemplate; line1Box.Tag = labelTemplate.LineLength; if (labelTemplate.InputElements.Count > 1) { line2Box.Enabled = true; line2Box.Tag = labelTemplate.LineLength; } else { line2Box.Enabled = false; } if (labelTemplate.InputElements.Count > 2) { line3Box.Enabled = true; line3Box.Tag = labelTemplate.LineLength; } else { line3Box.Enabled = false; } if (line1Box.Text.Length > labelTemplate.LineLength && line2Box.Enabled) { WrapLine(line1Box, line2Box); } UpdatePictureBox(); }
private static Font GetFillFont(Graphics g, LabelTemplate labelTemplate, LabelTextElement element, string text, string fontFamily, int fillWidth) { Rectangle boundingRect = new Rectangle(); float fontSize = 10.0f; Font font = null; while (boundingRect.Width < fillWidth) { font = new Font(fontFamily, fontSize); boundingRect = GetTextBoundingRect(g, font, text, new Point(0, 0), Alignment.CENTER); fontSize += 1; } return(new Font(fontFamily, fontSize - 1)); }
private static void DrawLines(Graphics g, LabelTemplate labelTemplate) { foreach (var line in labelTemplate.Lines) { Point p1 = labelTemplate.GetAbsolutePosition(line, line.StartPos); Point p2 = labelTemplate.GetAbsolutePosition(line, line.EndPos); Pen pen = new Pen(Brushes.Black); if (line.LineType == LineType.CutLine) { pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; pen.Width = 2; } g.DrawLine(pen, p1, p2); } }
private static Font GetFillFont(Graphics g, LabelTemplate labelTemplate, LabelTextElement element, string text, Point center, string fontFamily) { Rectangle labelBounds = labelTemplate.LabelBoundingRect; Rectangle rect = labelBounds; float fontSize = 10.0f; Font font = null; while (Rectangle.Union(rect, labelBounds).Equals(labelBounds)) { font = new Font(fontFamily, fontSize); rect = GetTextBoundingRect(g, font, text, center, Alignment.CENTER); fontSize += 1; } return(new Font(fontFamily, fontSize - 1)); }
private static void DrawMargins(Graphics g, LabelTemplate labelTemplate) { Brush marginColor = new SolidBrush(Color.FromArgb(0, 0, 0, 255)); Rectangle topMarginRect = new Rectangle(0, 0, (int)g.ClipBounds.Width, labelTemplate.TopMargin); g.FillRectangle(marginColor, topMarginRect); if (labelTemplate.RightMargin != 1000) { Rectangle rightMarginRect = new Rectangle(labelTemplate.RightMargin, 0, 10, labelTemplate.TopMargin + labelTemplate.HeightPx); g.FillRectangle(marginColor, rightMarginRect); } if (labelTemplate.BottomMargin != 1000) { Rectangle bottomMarginRect = new Rectangle(0, labelTemplate.BottomMargin, topMarginRect.Width, 10); g.FillRectangle(marginColor, bottomMarginRect); } }
private void line1Box_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '!' || e.KeyChar == '\b') { return; } TextBox tb = sender as TextBox; LabelTemplate labelTemplate = labelComboBox.SelectedItem as LabelTemplate; int rightMargin = labelTemplate.RightMargin; if (e.KeyChar >= 'a' && e.KeyChar <= 'z') { e.KeyChar = (char)(e.KeyChar - 32); } String newText = tb.Text + e.KeyChar; newText = newText.Replace("!", ""); Font descFont; int lineIndex = textBoxes.IndexOf(sender as TextBox); LabelTextElement labelLine = labelTemplate.InputElements[lineIndex]; int topMargin = labelTemplate.TopMargin; Point origin = labelTemplate.GetAbsolutePosition(labelLine, labelLine.StartPos); descFont = labelTemplate.GetFont(labelLine.Font); using (Graphics g = Graphics.FromImage(pictureBox1.Image)) { g.TextRenderingHint = TextRenderingHint.AntiAlias; StringFormat format = new StringFormat(StringFormat.GenericDefault); format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces; int width = (int)g.MeasureString(newText, descFont, origin, format).Width; if (origin.X + width > rightMargin) { e.Handled = true; SystemSounds.Beep.Play(); return; } } }
public Image DrawLabel(LabelTemplate labelTemplate, Image img, bool forPrinting, LabelParameters pars, string printText = null) { Dictionary <string, Rectangle> boundingRects = new Dictionary <string, Rectangle> { { "", new Rectangle() } }; //pictureBox1.Tag = labelTemplate.TopMargin; Bitmap bmp = img != null ? (Bitmap)img : new Bitmap(457, 254); using (Graphics g = Graphics.FromImage(bmp)) { g.FillRectangle(Brushes.White, new Rectangle(0, 0, bmp.Width, bmp.Height)); if (!forPrinting) { DrawMargins(g, labelTemplate); } g.TextRenderingHint = TextRenderingHint.AntiAlias; DrawLines(g, labelTemplate); int lineInputCount = 0; foreach (var element in labelTemplate.Elements) { Point p = labelTemplate.GetAbsolutePosition(element, element.StartPos, boundingRects[element.Anchor]); string param = element.Parameter; string paramValue = printText == null ? "" : printText; switch (param) { case "stock": paramValue = pars.StockNum; break; case "date": paramValue = pars.DateCode; break; case "store": paramValue = "Dave's Pawn LLC"; break; case "price": paramValue = FormatPrice(pars.PriceText, (LabelTextElement)element); break; default: continue; } if (element.ElementType == ElementType.Text) { Font font = labelTemplate.GetFont((element as LabelTextElement).Font); Brush textBrush = Brushes.Black; LabelTextElement textElement = element as LabelTextElement; if (textElement.Rotation == 0) { Rectangle rect = GetTextBoundingRect(g, font, paramValue, p, textElement.Alignment); if (textElement.Inverted) { Rectangle fillRect = new Rectangle(rect.X + (int)(rect.Height * 0.1), rect.Y, rect.Width - (int)(rect.Height * 0.1), (int)(rect.Height * 0.85)); g.FillRectangle(Brushes.Black, fillRect); textBrush = Brushes.White; } if (textElement.Alignment == Alignment.LEFT) { g.DrawString(paramValue, font, textBrush, p); } else { rect.Width = 500; g.DrawString(paramValue, font, textBrush, rect); } if (element.ID != "") { boundingRects.Add(element.ID, rect); } } else { if (textElement.Alignment == Alignment.FILL) { font = GetFillFont(g, labelTemplate, textElement, paramValue, font.FontFamily.Name, textElement.FillWidth); } DrawRotatedText(g, font, paramValue, textElement.Rotation, p); } } else if (element.ElementType == ElementType.Barcode) { Rectangle boundingRect = new Rectangle(); if ((element as LabelBarcodeElement).Symbology == BarcodeSymbology.Code39) { using (Font C39 = new Font(fonts.Families[0], (element as LabelBarcodeElement).Size)) { boundingRect = GetTextBoundingRect(g, C39, "*" + paramValue + "*", p, Alignment.LEFT); g.DrawString("*" + paramValue + "*", C39, Brushes.Black, p); } } else if ((element as LabelBarcodeElement).Symbology == BarcodeSymbology.Code128) { int barWeight = 1; Bitmap barcode = (Bitmap)Code128Rendering.MakeBarcodeImage(paramValue, barWeight, false); g.DrawImage(barcode, p); boundingRect = new Rectangle(p.X, p.Y, barcode.Width, barcode.Height); } if (element.ID != "") { boundingRects.Add(element.ID, boundingRect); } } } foreach (var element in labelTemplate.InputElements) { string paramValue = printText == null ? textBoxes[lineInputCount].Text : printText; if (paramValue != "") { LabelTextElement textElement = element as LabelTextElement; Rectangle rect = new Rectangle(); Font font = null; Point p = labelTemplate.GetAbsolutePosition(element, element.StartPos, boundingRects[element.Anchor]); if (textElement.Alignment != Alignment.FILL) { font = labelTemplate.GetFont(element.Font); rect = GetTextBoundingRect(g, font, paramValue.Replace("!", ""), p, (element as LabelTextElement).Alignment); g.DrawString(paramValue.Replace("!", ""), font, Brushes.Black, p); AddHighlights(g, font, new Regex("![^!]+!"), paramValue, p.Y, p.X + 3); } else { font = labelTemplate.GetFont(element.Font); font = GetFillFont(g, labelTemplate, textElement, paramValue, p, font.FontFamily.Name); rect = GetTextBoundingRect(g, font, paramValue, p, Alignment.CENTER); rect.Width = rect.Width + (int)(rect.Height * .05); Brush textBrush = Brushes.Black; if (textElement.Inverted) { textBrush = Brushes.White; g.FillRectangle(Brushes.Black, rect); } rect.Width = 500; g.DrawString(paramValue, font, textBrush, rect); } if (element.ID != "") { boundingRects.Add(element.ID, rect); } } lineInputCount++; } } return(bmp); }