213 lines
6.2 KiB
C#
213 lines
6.2 KiB
C#
///
|
|
/// Copyright (c) 2015 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Printing;
|
|
using System.IO;
|
|
using Config.Entities;
|
|
using TBF.Resources;
|
|
|
|
namespace TBF.BenchControl.ResultsPrinters.LabelPrinter
|
|
{
|
|
public class PrintLabelDocument : PrintDocument
|
|
{
|
|
const int FontSize = 9;
|
|
const string FontFamilyName = "Arial"; //"Times New Roman";
|
|
|
|
readonly int paperWidth;
|
|
readonly int paperHeight;
|
|
readonly string templateFile;
|
|
|
|
/// <summary> Property variable for the Font the user wishes to use </summary>
|
|
Font font;
|
|
|
|
///
|
|
/// Data to print
|
|
///
|
|
Results.Entities.Batch batch;
|
|
|
|
///
|
|
/// Items to print
|
|
///
|
|
IList<Results.ItemSpec> rsltItems;
|
|
|
|
///
|
|
/// Layout and status
|
|
///
|
|
int nextWMNr; /// nr. of watermeter to be printed as the first one on the next page
|
|
int nrPages;
|
|
int pageNr; /// Page number to be printed at the bottom of the page
|
|
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <param name="textToPrint">Text to be printed</param>
|
|
public PrintLabelDocument(Results.Entities.Batch batch,
|
|
PageOrientation pageOrientation,
|
|
int paperWidth,
|
|
int paperHeight,
|
|
string templateFile)
|
|
{
|
|
this.batch = batch;
|
|
this.paperWidth = paperWidth;
|
|
this.paperHeight = paperHeight;
|
|
this.templateFile = templateFile;
|
|
|
|
DefaultPageSettings.Landscape = (pageOrientation == PageOrientation.Landscape);
|
|
|
|
if (batch.WaterMeters.Count > 0 && batch.WaterMeters[0].Compound())
|
|
{
|
|
rsltItems = Results.ItemSpec.FromStrArray(Program.LocalSettings.RsltItems_Printer_CombinedWM);
|
|
}
|
|
else
|
|
{
|
|
rsltItems = Results.ItemSpec.FromStrArray(Program.LocalSettings.RsltItems_Printer_SingleWM);
|
|
}
|
|
|
|
/// Set print area size and margins (in dots using 80 dpi)
|
|
int printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
|
|
int printWidth = base.DefaultPageSettings.PaperSize.Width - base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
|
|
int leftMargin = base.DefaultPageSettings.Margins.Left; /// X
|
|
int topMargin = base.DefaultPageSettings.Margins.Top; /// Y
|
|
|
|
/// Check if the user selected to print in Landscape mode
|
|
/// if they did then we need to swap height/width parameters
|
|
if (DefaultPageSettings.Landscape)
|
|
{
|
|
int tmp = printHeight;
|
|
printHeight = printWidth;
|
|
printWidth = tmp;
|
|
}
|
|
|
|
/// One watermeter per label/page
|
|
nrPages = batch.WaterMeters.Count;
|
|
|
|
pageNr = 1;
|
|
nextWMNr = 0;
|
|
}
|
|
|
|
|
|
/// <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);
|
|
|
|
if (font == null) { font = new Font(FontFamilyName, FontSize, FontStyle.Regular); }
|
|
}
|
|
|
|
|
|
/// <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 (File.Exists(templateFile))
|
|
{
|
|
Image img = Image.FromFile(templateFile);
|
|
e.Graphics.DrawImage(new Bitmap(img), new Rectangle(0, 0, paperWidth, paperHeight));
|
|
}
|
|
|
|
/// Set print area size and margins (in dots using 80 dpi)
|
|
int printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom;
|
|
int printWidth = base.DefaultPageSettings.PaperSize.Width - base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right;
|
|
int leftMargin = base.DefaultPageSettings.Margins.Left; /// X
|
|
int topMargin = base.DefaultPageSettings.Margins.Top; /// Y
|
|
|
|
/// Check if the user selected to print in Landscape mode
|
|
/// if they did then we need to swap height/width parameters
|
|
if (base.DefaultPageSettings.Landscape)
|
|
{
|
|
int tmp = printHeight;
|
|
printHeight = printWidth;
|
|
printWidth = tmp;
|
|
}
|
|
|
|
int x = leftMargin;
|
|
int y = topMargin;
|
|
|
|
/// Use the StringFormat class for the text layout of our document
|
|
StringFormat format = new StringFormat(StringFormatFlags.LineLimit);
|
|
|
|
|
|
///--------
|
|
/// Body
|
|
///--------
|
|
|
|
PrintWM(e, nextWMNr);
|
|
nextWMNr++;
|
|
e.HasMorePages = (nextWMNr < batch.WaterMeters.Count);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Print one water meter results
|
|
/// </summary>
|
|
/// <param name="wmNr">Water meter number (0-based)</param>
|
|
void PrintWM(System.Drawing.Printing.PrintPageEventArgs e, int wmNr)
|
|
{
|
|
if (wmNr >= batch.WaterMeters.Count) return;
|
|
|
|
Results.Entities.WaterMeter wm = batch.WaterMeters[wmNr];
|
|
|
|
if (wm == null) return;
|
|
|
|
|
|
/// Serial number
|
|
if (!string.IsNullOrEmpty(wm.SerialNr)) PrintAt(e, 20, 62, string.Format("S/N: {0}", wm.SerialNr));
|
|
|
|
/// Watermeter type
|
|
PrintAt(e, 20, 74, batch.ProtocolTitle);
|
|
|
|
/// Date
|
|
PrintAt(e, 135, 74, string.Format("{0:dd/MM/yy", batch.EndTime));
|
|
|
|
if (rsltItems.Count > 0)
|
|
{
|
|
Results.WMeterRsltItemSpec item1 = Results.WMeterRsltItemSpec.GetItem(Results.ItemID.Test_name);
|
|
|
|
Results.WMeterRsltItemSpec item2 = Results.WMeterRsltItemSpec.GetItem(Results.ItemID.Error);
|
|
item2.Units = Config.Unit.Pct;
|
|
item2.Precision = "F2";
|
|
item2.Format = "{0}%";
|
|
|
|
int printPosition = 1;
|
|
foreach (var mtr in wm.MeterTestRslts)
|
|
{
|
|
item1.TestID = mtr.Name();
|
|
item2.TestID = mtr.Name();
|
|
|
|
PrintAt(e, 20, 6 + 14 * printPosition, item1.Print(wm));
|
|
PrintAt(e, 78, 6 + 14 * printPosition, item2.Print(wm));
|
|
|
|
printPosition++;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void PrintAt(System.Drawing.Printing.PrintPageEventArgs e, Point p, string text)
|
|
{
|
|
PrintAt(e, p.X, p.Y, text);
|
|
}
|
|
|
|
|
|
void PrintAt(System.Drawing.Printing.PrintPageEventArgs e, int x, int y, string text)
|
|
{
|
|
RectangleF printArea = new RectangleF(x, y, 667, 18);
|
|
e.Graphics.DrawString(text, this.font, Brushes.Black, printArea);
|
|
}
|
|
}
|
|
}
|