From d6e210aeab34f990a9e8d8511a19be7cd32926eb Mon Sep 17 00:00:00 2001 From: Kalarumeth <70245579+Kalarumeth@users.noreply.github.com> Date: Thu, 27 Oct 2022 18:53:21 +0200 Subject: [PATCH] Issue --- console_lps.sln | 25 +++++ console_lps/Program.cs | 51 +++++++++ console_lps/Repository/labelItem.cs | 17 +++ console_lps/Services/LabelPrinterService.cs | 118 ++++++++++++++++++++ console_lps/Services/Utils.cs | 38 +++++++ console_lps/console_lps.csproj | 21 ++++ 6 files changed, 270 insertions(+) create mode 100644 console_lps.sln create mode 100644 console_lps/Program.cs create mode 100644 console_lps/Repository/labelItem.cs create mode 100644 console_lps/Services/LabelPrinterService.cs create mode 100644 console_lps/Services/Utils.cs create mode 100644 console_lps/console_lps.csproj diff --git a/console_lps.sln b/console_lps.sln new file mode 100644 index 0000000..04789c7 --- /dev/null +++ b/console_lps.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "console_lps", "console_lps\console_lps.csproj", "{35ADC8E8-A92B-42D3-9B04-CFF7B5A3F9D6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {35ADC8E8-A92B-42D3-9B04-CFF7B5A3F9D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {35ADC8E8-A92B-42D3-9B04-CFF7B5A3F9D6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {35ADC8E8-A92B-42D3-9B04-CFF7B5A3F9D6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {35ADC8E8-A92B-42D3-9B04-CFF7B5A3F9D6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {3AF84959-0FD9-499B-8D10-69A934AC2ABF} + EndGlobalSection +EndGlobal diff --git a/console_lps/Program.cs b/console_lps/Program.cs new file mode 100644 index 0000000..58faa68 --- /dev/null +++ b/console_lps/Program.cs @@ -0,0 +1,51 @@ +using System.ComponentModel; +using console_lps.Services; +using System.Drawing.Printing; +using System.Globalization; +using DocumentFormat.OpenXml.Spreadsheet; +using console_lps.Repository; + +namespace console_lps; + +public class Program +{ + private List liToPrintList; + private int itemCount; + + public static void Main() + { + Program p = new Program(); + p.Print(); + } + + public void Print() + { + string[] dataToPrint = + { + "HELLO WORLD!", + "I WRITE SOME TEXT IN THIS SPACE TO CHECK HOW IT WAS PRINTED" + }; + + //DateTime delDate = new DateTime(); + //DateTime.TryParse(dataToPrint[10], out delDate); + + itemCount += 1; + + liToPrintList = new List(); + + liToPrintList.Add(new LabelItem() + { + ItemId = dataToPrint[0], + ItemDescription = dataToPrint[1], + }); + + // Definisce dove viene stampata l'etichetta + var printer = "\\\\PALAPPS03.PAL.LOCAL\\ETM03"; + + using (PrintDocument pdLabel = LabelPrinterService.PreparePrintDocument(printer, liToPrintList[0].TotalLabels)) + { + pdLabel.PrintPage += (sender, evt) => LabelPrinterService.label_PrintWmsItems(sender, evt, itemCount, liToPrintList[0]); + pdLabel.Print(); + } + } +} diff --git a/console_lps/Repository/labelItem.cs b/console_lps/Repository/labelItem.cs new file mode 100644 index 0000000..4dd1e6a --- /dev/null +++ b/console_lps/Repository/labelItem.cs @@ -0,0 +1,17 @@ +using DocumentFormat.OpenXml.Drawing; + +namespace console_lps.Repository; + +public class LabelItem +{ + public short TotalLabels { get; set; } + public string ItemId { get; set; } + public string ItemDescription { get; set; } + List QrValues { get; set; } +} + +public class QrValue +{ + public string Value { get; set; } + public string Item { get; set; } +} \ No newline at end of file diff --git a/console_lps/Services/LabelPrinterService.cs b/console_lps/Services/LabelPrinterService.cs new file mode 100644 index 0000000..37818a1 --- /dev/null +++ b/console_lps/Services/LabelPrinterService.cs @@ -0,0 +1,118 @@ +using System.Diagnostics; +using System.Drawing; +using System.Drawing.Printing; +using System.Globalization; +using console_lps.Repository; + +namespace console_lps.Services; + +public class LabelPrinterService +{ + //private string printer = "ETM03"; + //private List wliToPrintList; + //private int itemCount; + + + internal static PrintDocument PreparePrintDocument(string printer, short numOfCopies = 1) + { + PaperSize paperSize = new PaperSize("Custom Size", 410, 158); + + PrintDocument labelToPrint = new PrintDocument(); + labelToPrint.DefaultPageSettings.PaperSize = paperSize; + labelToPrint.PrinterSettings.PrinterName = printer; + labelToPrint.PrinterSettings.DefaultPageSettings.PaperSize = paperSize; + labelToPrint.PrinterSettings.Copies = numOfCopies; + + + return labelToPrint; + } + + public static void label_PrintWmsItems(object sender, PrintPageEventArgs e, int itemCount, LabelItem item) + { + try + { + int offsetx = 8; + int offsety = 8; + + + offsety += -19; + + SolidBrush brush = new SolidBrush(Color.Black); + + /*Nuova etichetta con in QR*/ + Font fnt = new Font("Bitstream Vera Sans Mono", 8.0F, FontStyle.Regular); + Font fntBold = new Font("Bitstream Vera Sans Mono", 13.0F, FontStyle.Bold); + + // Codice QR + //e.Graphics.DrawImage(Utils.GenerateQRCode_Image(JsonConvert.SerializeObject(new QrValue() + //{ + // Value = "Hello World! I'm a fucking QR CODE", + // Item = "" + //}), + //135, 135), offsetx, offsety); + + offsetx += 137; + + Font fntItemId = new Font("Bitstream Vera Sans Mono", 20.0F, FontStyle.Bold); + + if (item.ItemId.Length > 14) + fntItemId = new Font("Bitstream Vera Sans Mono", 14.0F, FontStyle.Bold); + + // Item Id + offsety += 10; + e.Graphics.DrawString(item.ItemId, fntItemId, brush, offsetx, offsety); + + ////Batch + //if (!string.IsNullOrEmpty(item.InventBatchId)) + //{ + // Font fntBatchId = new Font("Bitstream Vera Sans Mono", 14.0F, FontStyle.Regular); + // offsety += 24; + // e.Graphics.DrawString(item.InventBatchId, fntBatchId, brush, offsetx, offsety); + //} + + offsetx += 4; + offsety += 31; + string format = CultureInfo.GetCultureInfo("IT-it").DateTimeFormat.ShortDatePattern; + + e.Graphics.DrawString((item.ItemDescription.Length > 36 ? item.ItemDescription.Substring(0, 36) + "\r\n" + item.ItemDescription.Substring(36, item.ItemDescription.Length - 36) : item.ItemDescription + "\r\n") + , fnt + , brush + , offsetx + , offsety); + + //e.Graphics.DrawString("\r\n" + "\r\n" + "\r\n" + item.UoM + " " + Utils.ConvertDecimalInString_TwoDecimal(item.InventQty) + // , fnt + // , brush + // , offsetx + 226 + // , offsety + // , new StringFormat() { Alignment = StringAlignment.Far }); + + //offsety += 50; + //e.Graphics.DrawString(item.PbmProjectInventId + // , fntBold + // , brush + // , offsetx + // , offsety); + + //e.Graphics.DrawString(item.WmsLocationId + // , fntBold + // , brush + // , offsetx + 226 + // , offsety + // , new StringFormat() { Alignment = StringAlignment.Far }); + + ///*Nuova etichetta con in QR*/ + //if (itemCount != wliToPrintList.Count - 1) + //{ + // itemCount += 1; + // e.HasMorePages = true; + //} + Thread.Sleep(2000); + } + catch (Exception exc) + { + StackFrame callStack = new StackFrame(0, true); + Utils.TryCatchMessage(exc, callStack); + } + } +} diff --git a/console_lps/Services/Utils.cs b/console_lps/Services/Utils.cs new file mode 100644 index 0000000..57e305e --- /dev/null +++ b/console_lps/Services/Utils.cs @@ -0,0 +1,38 @@ +using System.Diagnostics; +using System.Drawing; +using Serilog; +using ZXing.QrCode; + +namespace console_lps; + +public class Utils +{ + internal static Image GenerateQRCode_Image(string data, int width, int height, int x = 0, int y = 0, int croppedHeight = 0, int croppedWidth = 0) + { + QRCodeWriter qr = new QRCodeWriter(); + var matrix = qr.encode(data, ZXing.BarcodeFormat.QR_CODE, width, height); + ZXing.BarcodeWriter w = new ZXing.BarcodeWriter { Format = ZXing.BarcodeFormat.QR_CODE }; + System.Drawing.Bitmap img = w.Write(matrix); + + if (x != 0 || y != 0) + { + System.Drawing.Bitmap croppedImage = img.Clone(new System.Drawing.Rectangle(x, y, croppedHeight, croppedWidth), img.PixelFormat); + return croppedImage; + } + else + return img; + } + + internal static string ConvertDecimalInString_TwoDecimal(decimal dec, string culture = "it-IT") + { + string ret = ""; + ret = dec.ToString("#,0.00", System.Globalization.CultureInfo.GetCultureInfo(culture)); + return ret; + } + + internal static void TryCatchMessage(Exception exc, StackFrame callStack) + { + var message = "ERRORE => " + exc.Message + "\r\n -> alla linea di codice " + callStack.GetFileLineNumber() + "\r\n -> nel metodo " + callStack.GetMethod() + "\r\n -> del file " + callStack.GetFileName() + "\r\n -> in data " + System.DateTime.Now + "\r\n"; + Log.Logger.Error(message); + } +} \ No newline at end of file diff --git a/console_lps/console_lps.csproj b/console_lps/console_lps.csproj new file mode 100644 index 0000000..2e82547 --- /dev/null +++ b/console_lps/console_lps.csproj @@ -0,0 +1,21 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + + + +