/// /// Copyright (c) 2021 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Printing; using System.IO; using Common; using GenCode128; using Gma.QrCodeNet.Encoding; using SharedDatabase.Entities; using OrderManagement.Resources; namespace OrderManagement { public class OrderPrintDocument : Common.Printers.PrintersCommon { static QrEncoder encoder = new QrEncoder(); // Paper size const bool IsLandscape = false; const int PaperHeight = 1400; const int PaperWidth = 900; /// Texts readonly Font font = new Font("Arial", 12, FontStyle.Bold); const int Top = 100; const int Column1left = 100; const int Column2left = 300; const int Spacing = 35; /// Barcode const bool IsQRCode = false; const int BarcodeLeft = 550; const int BarcodeTop = 100; const int BarcodeWidth = 200; const int BarcodeHeight = 75; /// Data to print OrderInfo order; /// /// Constructor /// /// Text to be printed public OrderPrintDocument(OrderInfo order) { DocumentName = order.POName; this.order = order; /// Set print area size and margins (in dots using 100 dpi) DefaultPageSettings.Landscape = IsLandscape; } /// /// Override the default onbeginPrint method of the PrintDocument Object /// /// /// protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e) { base.OnBeginPrint(e); } /// /// Override the default OnPrintPage method of the PrintDocument. /// /// /// This provides the print logic for our document protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e) { /// Run base code base.OnPrintPage(e); string[] texts = new string[] { Strings.Order + " :", order.POName, Strings.Pieces_count + " :", order.PiecesCount.ToString(), Strings.First_serial_number + " :", string.Format("{0}{1}{2}", (order.SNPrefix != null) ? order.SNPrefix : string.Empty, order.SNDigitsCount != 0 ? order.SNFirst.ToString(string.Format("D{0}", order.SNDigitsCount)) : string.Empty, (order.SNSuffix != null) ? order.SNSuffix : string.Empty), Strings.Last_serial_number + " :", string.Format("{0}{1}{2}", (order.SNPrefix != null) ? order.SNPrefix : string.Empty, order.SNDigitsCount != 0 ? (order.SNFirst + order.PiecesCount - 1).ToString(string.Format("D{0}", order.SNDigitsCount)) : string.Empty, (order.SNSuffix != null) ? order.SNSuffix : string.Empty), Strings.First_radio_address + " :", string.Format("{0}{1}{2}", (order.RAPrefix != null) ? order.RAPrefix : string.Empty, order.RAFirst.ToString(SharedDatabase.Const.RAFormat), (order.RASuffix != null) ? order.RASuffix : string.Empty), Strings.Last_radio_address + " :", string.Format("{0}{1}{2}", (order.RAPrefix != null) ? order.RAPrefix : string.Empty, (order.RAFirst + order.PiecesCount - 1).ToString(SharedDatabase.Const.RAFormat), (order.RASuffix != null) ? order.RASuffix : string.Empty), Strings.Workflow + " :", (order.Workflow != null) ? order.Workflow : string.Empty, Strings.Test_procedure + " :", (order.TestProcedure != null) ? order.TestProcedure : string.Empty, Strings.Variant_code + " :", (order.VariantCode != null) ? order.VariantCode : string.Empty, Strings.Laser_marking + " :", (order.LaserMarking != null) ? order.LaserMarking : string.Empty, Strings.Remark + " :", (order.Remark != null) ? order.Remark : string.Empty, }; int lineTop = Top; for (int i = 0; i < texts.Length; i += 2) { e.Graphics.DrawString(texts[i], font, Brushes.Black, new RectangleF(Column1left, lineTop, PaperWidth, PaperHeight)); e.Graphics.DrawString(texts[i + 1], font, Brushes.Black, new RectangleF(Column2left, lineTop, PaperWidth, PaperHeight)); lineTop += Spacing; } try { if (IsQRCode) { QrCode qrCode = encoder.Encode(order.POName); DrawQR(e.Graphics, qrCode, BarcodeLeft, BarcodeTop, BarcodeWidth, BarcodeHeight); } else { Image img = Code128Rendering.MakeBarcodeImage(order.POName, 1, true); #if VERTICAL_BARCODE img.RotateFlip(RotateFlipType.Rotate90FlipNone); #endif e.Graphics.DrawImage(img, BarcodeLeft, BarcodeTop, BarcodeWidth, BarcodeHeight); } } catch (Exception) { } } public static void DrawQR(Graphics g, QrCode qrCode, float left, float top, float width, float height) { /// Assuming 100 dpi (printer) g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; float qrPixWidth = width / qrCode.Matrix.Width; float qrPixHeight = height / qrCode.Matrix.Height; for (int i = 0; i < qrCode.Matrix.Height; i++) { for (int j = 0; j < qrCode.Matrix.Width; j++) { if (qrCode.Matrix[j, i]) { g.FillRectangle(Brushes.Black, new RectangleF(left + j * qrPixWidth, top + i * qrPixHeight, qrPixWidth, qrPixHeight)); } } } } } }