/// /// Copyright (c) 2018 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 Gma.QrCodeNet.Encoding; using Config.Entities; using Results.Resources; namespace Results.Output.Printers.Label { public class LabelPrintDocument : PrintersCommon { static QrEncoder encoder = new QrEncoder(); /// Size of the printed area without margins, after taking into account 'pageOrientation' readonly int printHeight; readonly int printWidth; /// Selected font readonly Font font; /// Data to print Results.Entities.Batch batch; LabelPrinterCfg cfg; Results.Entities.WaterMeter wm; /// Items to print IList itemsToPrint; /// /// Constructor /// /// Text to be printed public LabelPrintDocument(Results.Entities.Batch batch, LabelPrinterCfg cfg, Results.Entities.WaterMeter wm, string documentName) { DocumentName = documentName; this.batch = batch; this.cfg = cfg; this.wm = wm; this.itemsToPrint = Results.WMeterRsltItemSpec.FromStrArray(cfg.ItemsToPrint); /// Initialize fonts font = new Font((cfg.FontFamily != null ? cfg.FontFamily : "Arial"), (cfg.FontSize > 0 ? cfg.FontSize : 10), (FontStyle)cfg.FontStyle); /// Set print area size and margins (in dots using 100 dpi) DefaultPageSettings.Landscape = (cfg.PageOrientation == PageOrientation.Landscape); /// printHeight = cfg.PaperHeight; printWidth = cfg.PaperWidth; } /// /// 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); if (!string.IsNullOrEmpty(cfg.Template)) { string templatePath = Path.Combine("C:\\TBF\\Templates\\", cfg.Template); if (File.Exists(templatePath)) { e.Graphics.DrawImage(new Bitmap(Image.FromFile(templatePath), printWidth, printHeight), new Rectangle(0, 0, printWidth, printHeight)); } } foreach (var item in itemsToPrint) { string[] coords = item.Caption.Split(new char[] { ';' }); int x, y; if (coords.Length >= 2 && int.TryParse(coords[0].Trim(new char[] { ' ' }), out x) && int.TryParse(coords[1].Trim(new char[] { ' ' }), out y)) { RectangleF printArea = new RectangleF(x, y, printWidth, printHeight); e.Graphics.DrawString(item.Print(wm), this.font, Brushes.Black, printArea); } } if (cfg.DrawSNasBarcode) { if (cfg.IsQR) { QrCode qrCode = encoder.Encode(wm.SerialNr); DrawQR(e.Graphics, qrCode, cfg.BarcodeLeft, cfg.BarcodeTop, cfg.BarcodeWidth, cfg.BarcodeHeight); } else { /// TODO } } } 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)); } } } } } }