Advertisement
Librarydll

Untitled

May 25th, 2022
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.69 KB | None | 0 0
  1.  public class KitchenPrintService : IKitchenPrintService
  2.     {
  3.         public const int COUNT_WIDTH = 30;
  4.         private Font _printFont;
  5.         private Font _boldFont;
  6.         private Font _printProductFont;
  7.         private Brush _brush;
  8.         private Font _orderNumberFont;
  9.         public Guid Id { get; set; }
  10.         private readonly ILogger<KitchenPrintService> _logger;
  11.  
  12.         const int LengthForWrap = 16;
  13.         const int RecHeight = 45;
  14.         public KitchenPrintService(ILogger<KitchenPrintService> logger)
  15.         {
  16.             _logger = logger;
  17.             _printFont = new Font("Arial", 12);
  18.             _boldFont = new Font("Arial", 17,FontStyle.Bold);
  19.             _printProductFont = new Font("Arial", 16, FontStyle.Bold);
  20.             _brush = Brushes.Black;
  21.             _orderNumberFont = new Font("Arial", 13, FontStyle.Bold);
  22.             Id = Guid.NewGuid();
  23.         }
  24.  
  25.         public async Task Print(KitchenPrintModel model)
  26.         {
  27.             var grouped = model.Models.GroupBy(x => x.IpAddress);
  28.             List<Task> printTasks = new List<Task>();
  29.  
  30.             foreach (var item in grouped)
  31.             {
  32.                 var collection = item.Select(x => x).ToList();
  33.                 var task = Task.Run(() =>
  34.                 PrintInner(new KitchenPrintModel
  35.                 {
  36.                     CashierFIO = model.CashierFIO,
  37.                     Models = collection,
  38.                     Created = model.Created,
  39.                     OrderNumber = model.OrderNumber,
  40.                     OrderType = model.OrderType,
  41.                     PrinterName = item.Key,
  42.                     Comment = model.Comment
  43.                 }));
  44.                 printTasks.Add(task);
  45.             }
  46.             _logger.LogInformation($"Begin Print kitchen", Id);
  47.             await Task.WhenAll(printTasks);
  48.             _logger.LogInformation("Finish Print kitchen", Id);
  49.         }
  50.  
  51.         public void PrintInner(KitchenPrintModel kitchenPrintModel)
  52.         {
  53.             PrintDocument pd = new();
  54.             pd.PrinterSettings.PrinterName = kitchenPrintModel.PrinterName;
  55.             pd.PrintPage += (sendr, e) => Draw(kitchenPrintModel, e);
  56.             pd.Print();
  57.         }
  58.         private void Draw(KitchenPrintModel model, PrintPageEventArgs e)
  59.         {
  60.             try
  61.             {
  62.                 int count = 0;
  63.                 float leftMargin = 10;
  64.                 float rigthMargin = 10;
  65.                 float topMargin = 5;
  66.                 var paperWidth = e.PageBounds.Width;
  67.                 float yPos = topMargin + (count * _printFont.GetHeight(e.Graphics));
  68.                 var textSize = e.Graphics.MeasureString(model.OrderType, _boldFont);
  69.                 var center = paperWidth / 2 - (textSize.Width / 2);
  70.                 e.Graphics.DrawString(model.OrderType, _boldFont, _brush, center, yPos, new StringFormat());
  71.                 count++;
  72.                 yPos = topMargin + (count * _printFont.GetHeight(e.Graphics));
  73.                 var number = $"N# {model.OrderNumber}";
  74.                 e.Graphics.DrawString(number, _orderNumberFont, _brush, leftMargin, yPos, new StringFormat());
  75.                 count++;
  76.                 yPos = topMargin + (count * _printFont.GetHeight(e.Graphics));
  77.                 e.Graphics.DrawString(model.CashierFIO, _printFont, _brush, leftMargin, yPos, new StringFormat());
  78.                 count++;
  79.                 yPos = topMargin + (count * _printFont.GetHeight(e.Graphics));
  80.                 var now = model.Created.ToString("dd.MM.yyyy | HH:mm");
  81.                 e.Graphics.DrawString(now, _printFont, _brush, leftMargin, yPos, new StringFormat());
  82.                 count++;
  83.                 yPos = topMargin + (count * _printFont.GetHeight(e.Graphics));
  84.                 e.Graphics.DrawLine(new Pen(_brush), 0, yPos, paperWidth, yPos);
  85.                 int rec = 0;
  86.                 foreach (var item in model.Models)
  87.                 {
  88.                     var line = item.Title;
  89.                     var size = e.Graphics.MeasureString(line, _printProductFont);
  90.                     var currentYPos = topMargin + (count * _printProductFont.GetHeight(e.Graphics));
  91.                     yPos = topMargin + (count * _printProductFont.GetHeight(e.Graphics)) + rec;
  92.  
  93.                     e.Graphics.DrawRectangle(new Pen(Brushes.Transparent), leftMargin, yPos, paperWidth, 200);
  94.                     int wrap = 30;
  95.                     var recF = new RectangleF(leftMargin, yPos, 250, wrap);
  96.                     if(line.Length > LengthForWrap)
  97.                     {
  98.                         recF = new RectangleF(leftMargin, yPos, 250, RecHeight);
  99.                         rec = RecHeight /2;
  100.                     }
  101.                     e.Graphics.DrawString(line, _printProductFont, _brush, recF, new StringFormat());
  102.  
  103.                     string countStr = item.Count.ToString();
  104.  
  105.                     e.Graphics.DrawString(countStr, _printProductFont, _brush, paperWidth - leftMargin - rigthMargin - 30, yPos, new StringFormat());
  106.                     count++;
  107.                 }
  108.                 yPos = topMargin + (count * _printProductFont.GetHeight(e.Graphics)) + rec;
  109.  
  110.                 e.Graphics.DrawLine(new Pen(_brush), 0, yPos, paperWidth, yPos);
  111.                 count++;
  112.  
  113.                 //draw rectangle
  114.                 if (!string.IsNullOrWhiteSpace(model.Comment))
  115.                 {
  116.                     var rect = new RectangleF(leftMargin, yPos, paperWidth, 100);
  117.                     e.Graphics.DrawString(model.Comment, _printProductFont, _brush, rect);
  118.                 }
  119.             }
  120.             catch (Exception ex)
  121.             {
  122.                 _logger.LogError($"Error while printing {ex.Message}");
  123.             }
  124.         }
  125.  
  126.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement