121 lines
4.2 KiB
C#
121 lines
4.2 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.Drawing.Printing;
|
|
using Gma.QrCodeNet.Encoding;
|
|
using DataMatrix4Net;
|
|
|
|
namespace LabelPrinting.Printers
|
|
{
|
|
public class DataMatrixAndTextPrintDoc : PrintDocument
|
|
{
|
|
const float mm = 1 / 0.254F; /// 1 mm expressed in 1/100 inch
|
|
|
|
int printWidth;
|
|
int printHeight;
|
|
int leftMargin;
|
|
int topMargin;
|
|
|
|
|
|
BitMatrix bitMatrix;
|
|
string text;
|
|
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <param name="bitmap">Bitmap to be printed</param>
|
|
/// <param name="paperWidth">Label width</param>
|
|
/// <param name="paperHeight">Label height</param>
|
|
/// <param name="landscape">true = landscape</param>
|
|
public DataMatrixAndTextPrintDoc(BitMatrix bitMatrix, string text, bool landscape = false)
|
|
{
|
|
this.bitMatrix = bitMatrix;
|
|
this.text = text;
|
|
|
|
DefaultPageSettings.Landscape = landscape;
|
|
|
|
/// Set print area size and margins (in dots using 80 dpi)
|
|
if (landscape)
|
|
{
|
|
printWidth = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
|
|
printHeight = base.DefaultPageSettings.PaperSize.Width - base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
|
|
leftMargin = base.DefaultPageSettings.Margins.Top; /// X
|
|
topMargin = base.DefaultPageSettings.Margins.Left; /// Y
|
|
}
|
|
else
|
|
{
|
|
printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
|
|
printWidth = base.DefaultPageSettings.PaperSize.Width - base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
|
|
leftMargin = base.DefaultPageSettings.Margins.Left; /// X
|
|
topMargin = base.DefaultPageSettings.Margins.Top; /// Y
|
|
}
|
|
}
|
|
|
|
protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e)
|
|
{
|
|
base.OnBeginPrint(e); /// Run base code
|
|
}
|
|
|
|
protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e)
|
|
{
|
|
base.OnPrintPage(e); /// Run base code
|
|
DrawLabel(e.Graphics, bitMatrix, text);
|
|
e.HasMorePages = false;
|
|
}
|
|
|
|
public static void DrawLabel(Graphics g, BitMatrix bitMatrix, string text)
|
|
{
|
|
string[] lines = text.Split(new char[] { '\n' });
|
|
|
|
/// Assuming 100 dpi (printer), drawing size: 2 inch (width) x 1 inch (height)
|
|
const float W = 18 * mm;
|
|
const float H = 10 * mm;
|
|
|
|
const float L1 = 7.0f * mm;
|
|
const float L2 = 7.0f * mm;
|
|
const float T1 = 3.5f * mm;
|
|
const float T2 = 5.5f * mm;
|
|
|
|
const float QRL = 1.0f * mm;
|
|
const float QRT = 2.5f * mm;
|
|
const float QrSize = 5.5f * mm;
|
|
|
|
string fontFamily = "Arial";
|
|
Font font = new Font(fontFamily, 4.5f, FontStyle.Bold);
|
|
|
|
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
|
|
|
|
g.Clear(Color.White);
|
|
|
|
if (lines.Length > 0)
|
|
{
|
|
g.DrawString(lines[0], font, Brushes.Black, new RectangleF(L1, T1, W - L1, H - T1));
|
|
}
|
|
|
|
if (lines.Length > 1)
|
|
{
|
|
g.DrawString(lines[1], font, Brushes.Black, new RectangleF(L2, T2, W - L2, H - T2));
|
|
}
|
|
|
|
g.Flush();
|
|
|
|
float QrPixSize = QrSize / bitMatrix.Width;
|
|
for (int i = 0; i < bitMatrix.Height; i++)
|
|
{
|
|
for (int j = 0; j < bitMatrix.Width; j++)
|
|
{
|
|
if (bitMatrix[j, i])
|
|
{
|
|
g.FillRectangle(Brushes.Black, new RectangleF(QRL + QrPixSize * j, QRT + QrPixSize * i, QrPixSize, QrPixSize));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|