///
/// Copyright (c) 2019 Sensus Slovensko a.s.
///
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using Gma.QrCodeNet.Encoding;
namespace RecordProcessing.Records
{
public class RFTestPrintDocument_400_900 : PrintDocument
{
int printWidth;
int printHeight;
int leftMargin;
int topMargin;
RFTestRecord_400_900 record;
QrCode qrCode;
///
/// Constructor
///
/// Bitmap to be printed
/// Label width
/// Label height
/// true = landscape
public RFTestPrintDocument_400_900(RFTestRecord_400_900 rcrd, QrCode qrCd, bool landscape)
{
this.record = rcrd;
this.qrCode = qrCd;
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, record, qrCode);
e.HasMorePages = false;
}
public static void DrawLabel(Graphics g, RFTestRecord_400_900 record, QrCode qrCode)
{
/// Assuming 100 dpi (printer), drawing size: 2 inch (width) x 1 inch (height)
const int W = 200;
const int H = 100;
const int Left = 0;
const int Top = 0;
const int L1 = Left + 10;
const int L2 = Left + 58;
const int L3 = Left + 77;
const int L4 = Left + 151;
const int T0 = Top;
const int T1 = Top + 3;
const int T2 = Top + 22;
const int T25 = T2 + 17 / 2;
const int T3 = T2 + 1 * 17;
const int T4 = T2 + 2 * 17;
const int T5 = T2 + 3 * 17;
const int QRL = Left + 151;
const int QRT = Top + 48;
const int QrSize = 42;
string fontFamily = "Arial Narrow";
Font font1 = new Font(fontFamily, 11, FontStyle.Bold);
Font font2 = new Font(fontFamily, 9, FontStyle.Regular);
Font font3 = new Font(fontFamily, 20, FontStyle.Bold);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.Clear(Color.White);
if (record.Status == Status.Passed)
{
g.DrawString("RF test PASSED", font1, Brushes.Black, new Rectangle(L1, T1, L4 - L1, T2 - T1));
}
else
{
g.DrawString("RF test FAILED", font1, Brushes.Red, new Rectangle(L1, T1, L4 - L1, T2 - T1));
}
g.DrawString("PCB nr.:", font2, Brushes.Black, new Rectangle(L1, T2, L2 - L1, T3 - T2));
g.DrawString("Mat. nr.:", font2, Brushes.Black, new Rectangle(L1, T3, L2 - L1, T3 - T2));
g.DrawString("Cust. order:", font2, Brushes.Black, new Rectangle(L1, T4, L2 - L1, T3 - T2));
g.DrawString("TB failed:", font2, Brushes.Black, new Rectangle(L1, T5, L3 - L1, T3 - T2));
g.DrawString(record.SN, font2, Brushes.Black, new Rectangle(L2, T2, L4 - L2, T3 - T2));
g.DrawString("INV-HIPGO02", font2, Brushes.Black, new Rectangle(L2, T3, L4 - L2, T3 - T2));
g.DrawString("CCAATMXXXX", font2, Brushes.Black, new Rectangle(L2, T4, L4 - L2, T3 - T2));
g.DrawString(record.PrintedInfo, font3, Brushes.Black, new Rectangle(L4, T0, W - L4, QRT - T1));
g.DrawString(record.FrequencyInfo, font2, Brushes.Black, new Rectangle(L4, T25, W - L4, QRT - T1));
g.DrawRectangle(new Pen(Brushes.Black, 1), new Rectangle(L3, T5 + 2, 15, 15));
g.DrawRectangle(new Pen(Brushes.Black, 1), new Rectangle(L3 + 22, T5 + 2, 15, 15));
g.DrawRectangle(new Pen(Brushes.Black, 1), new Rectangle(L3 + 44, T5 + 2, 15, 15));
g.Flush();
int QrPixSize = QrSize / qrCode.Matrix.Width;
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 Rectangle(QRL + QrPixSize * j, QRT + QrPixSize * i, QrPixSize, QrPixSize));
}
}
}
}
}
}