Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class KitchenPrintService : IKitchenPrintService
- {
- public const int COUNT_WIDTH = 30;
- private Font _printFont;
- private Font _boldFont;
- private Font _printProductFont;
- private Brush _brush;
- private Font _orderNumberFont;
- public Guid Id { get; set; }
- private readonly ILogger<KitchenPrintService> _logger;
- const int LengthForWrap = 16;
- const int RecHeight = 45;
- public KitchenPrintService(ILogger<KitchenPrintService> logger)
- {
- _logger = logger;
- _printFont = new Font("Arial", 12);
- _boldFont = new Font("Arial", 17,FontStyle.Bold);
- _printProductFont = new Font("Arial", 16, FontStyle.Bold);
- _brush = Brushes.Black;
- _orderNumberFont = new Font("Arial", 13, FontStyle.Bold);
- Id = Guid.NewGuid();
- }
- public async Task Print(KitchenPrintModel model)
- {
- var grouped = model.Models.GroupBy(x => x.IpAddress);
- List<Task> printTasks = new List<Task>();
- foreach (var item in grouped)
- {
- var collection = item.Select(x => x).ToList();
- var task = Task.Run(() =>
- PrintInner(new KitchenPrintModel
- {
- CashierFIO = model.CashierFIO,
- Models = collection,
- Created = model.Created,
- OrderNumber = model.OrderNumber,
- OrderType = model.OrderType,
- PrinterName = item.Key,
- Comment = model.Comment
- }));
- printTasks.Add(task);
- }
- _logger.LogInformation($"Begin Print kitchen", Id);
- await Task.WhenAll(printTasks);
- _logger.LogInformation("Finish Print kitchen", Id);
- }
- public void PrintInner(KitchenPrintModel kitchenPrintModel)
- {
- PrintDocument pd = new();
- pd.PrinterSettings.PrinterName = kitchenPrintModel.PrinterName;
- pd.PrintPage += (sendr, e) => Draw(kitchenPrintModel, e);
- pd.Print();
- }
- private void Draw(KitchenPrintModel model, PrintPageEventArgs e)
- {
- try
- {
- int count = 0;
- float leftMargin = 10;
- float rigthMargin = 10;
- float topMargin = 5;
- var paperWidth = e.PageBounds.Width;
- float yPos = topMargin + (count * _printFont.GetHeight(e.Graphics));
- var textSize = e.Graphics.MeasureString(model.OrderType, _boldFont);
- var center = paperWidth / 2 - (textSize.Width / 2);
- e.Graphics.DrawString(model.OrderType, _boldFont, _brush, center, yPos, new StringFormat());
- count++;
- yPos = topMargin + (count * _printFont.GetHeight(e.Graphics));
- var number = $"N# {model.OrderNumber}";
- e.Graphics.DrawString(number, _orderNumberFont, _brush, leftMargin, yPos, new StringFormat());
- count++;
- yPos = topMargin + (count * _printFont.GetHeight(e.Graphics));
- e.Graphics.DrawString(model.CashierFIO, _printFont, _brush, leftMargin, yPos, new StringFormat());
- count++;
- yPos = topMargin + (count * _printFont.GetHeight(e.Graphics));
- var now = model.Created.ToString("dd.MM.yyyy | HH:mm");
- e.Graphics.DrawString(now, _printFont, _brush, leftMargin, yPos, new StringFormat());
- count++;
- yPos = topMargin + (count * _printFont.GetHeight(e.Graphics));
- e.Graphics.DrawLine(new Pen(_brush), 0, yPos, paperWidth, yPos);
- int rec = 0;
- foreach (var item in model.Models)
- {
- var line = item.Title;
- var size = e.Graphics.MeasureString(line, _printProductFont);
- var currentYPos = topMargin + (count * _printProductFont.GetHeight(e.Graphics));
- yPos = topMargin + (count * _printProductFont.GetHeight(e.Graphics)) + rec;
- e.Graphics.DrawRectangle(new Pen(Brushes.Transparent), leftMargin, yPos, paperWidth, 200);
- int wrap = 30;
- var recF = new RectangleF(leftMargin, yPos, 250, wrap);
- if(line.Length > LengthForWrap)
- {
- recF = new RectangleF(leftMargin, yPos, 250, RecHeight);
- rec = RecHeight /2;
- }
- e.Graphics.DrawString(line, _printProductFont, _brush, recF, new StringFormat());
- string countStr = item.Count.ToString();
- e.Graphics.DrawString(countStr, _printProductFont, _brush, paperWidth - leftMargin - rigthMargin - 30, yPos, new StringFormat());
- count++;
- }
- yPos = topMargin + (count * _printProductFont.GetHeight(e.Graphics)) + rec;
- e.Graphics.DrawLine(new Pen(_brush), 0, yPos, paperWidth, yPos);
- count++;
- //draw rectangle
- if (!string.IsNullOrWhiteSpace(model.Comment))
- {
- var rect = new RectangleF(leftMargin, yPos, paperWidth, 100);
- e.Graphics.DrawString(model.Comment, _printProductFont, _brush, rect);
- }
- }
- catch (Exception ex)
- {
- _logger.LogError($"Error while printing {ex.Message}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement