2019-01-13 11:30:02 +00:00
|
|
|
|
///
|
2020-02-25 10:26:55 +00:00
|
|
|
|
/// Copyright (c) 2020 Sensus Slovensko a.s.
|
2019-01-13 11:30:02 +00:00
|
|
|
|
///
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Drawing;
|
2020-02-10 14:38:05 +00:00
|
|
|
|
using System.Drawing.Drawing2D;
|
2019-01-13 11:30:02 +00:00
|
|
|
|
using System.Drawing.Printing;
|
|
|
|
|
|
using System.IO;
|
2020-02-10 14:38:05 +00:00
|
|
|
|
using Gma.QrCodeNet.Encoding;
|
2021-09-23 09:46:40 +00:00
|
|
|
|
using Common;
|
2019-01-13 11:30:02 +00:00
|
|
|
|
using Results.Resources;
|
2020-02-25 10:26:55 +00:00
|
|
|
|
using GenCode128;
|
2019-01-13 11:30:02 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Results.Output.Printers.Label
|
|
|
|
|
|
{
|
2022-01-23 18:56:15 +00:00
|
|
|
|
public class LabelPrintDocument : Common.Printers.PrintersCommon
|
2019-01-13 11:30:02 +00:00
|
|
|
|
{
|
2020-02-10 14:38:05 +00:00
|
|
|
|
static QrEncoder encoder = new QrEncoder();
|
|
|
|
|
|
|
|
|
|
|
|
/// Size of the printed area without margins, after taking into account 'pageOrientation'
|
2019-01-13 11:30:02 +00:00
|
|
|
|
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<WMeterRsltItemSpec> itemsToPrint;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="textToPrint">Text to be printed</param>
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Override the default onbeginPrint method of the PrintDocument Object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name=e></param>
|
|
|
|
|
|
/// <remarks></remarks>
|
|
|
|
|
|
protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnBeginPrint(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Override the default OnPrintPage method of the PrintDocument.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name=e></param>
|
|
|
|
|
|
/// <remarks>This provides the print logic for our document</remarks>
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-02-10 14:38:05 +00:00
|
|
|
|
|
2020-02-25 10:26:55 +00:00
|
|
|
|
if (cfg.BarcodeType != BarcodeType.None && cfg.BarcodeType < BarcodeType.Count)
|
2020-02-10 14:38:05 +00:00
|
|
|
|
{
|
2020-02-25 10:26:55 +00:00
|
|
|
|
try
|
2020-02-10 14:38:05 +00:00
|
|
|
|
{
|
2020-02-25 10:26:55 +00:00
|
|
|
|
if (cfg.BarcodeType == BarcodeType.QR_Code)
|
|
|
|
|
|
{
|
|
|
|
|
|
QrCode qrCode = encoder.Encode(wm.SerialNr);
|
|
|
|
|
|
DrawQR(e.Graphics, qrCode, cfg.BarcodeLeft, cfg.BarcodeTop, cfg.BarcodeWidth, cfg.BarcodeHeight);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (cfg.BarcodeType == BarcodeType.Code_128_Horizontally || cfg.BarcodeType == BarcodeType.Code_128_Vertically)
|
|
|
|
|
|
{
|
|
|
|
|
|
Image img = Code128Rendering.MakeBarcodeImage(wm.SerialNr, 1, true);
|
|
|
|
|
|
if (cfg.BarcodeType == BarcodeType.Code_128_Vertically)
|
|
|
|
|
|
{
|
|
|
|
|
|
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
|
|
|
|
|
|
}
|
|
|
|
|
|
e.Graphics.DrawImage(img, cfg.BarcodeLeft, cfg.BarcodeTop, cfg.BarcodeWidth, cfg.BarcodeHeight);
|
|
|
|
|
|
}
|
2020-02-10 14:38:05 +00:00
|
|
|
|
}
|
2020-02-25 10:26:55 +00:00
|
|
|
|
catch (Exception)
|
2020-02-10 14:38:05 +00:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-13 11:30:02 +00:00
|
|
|
|
}
|
2020-02-10 14:38:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-13 11:30:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|