using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
namespace TBF.BenchControl.ResultsPrinters.Basic
{
public class BasicPrintDocument : PrintDocument
{
///
/// Property variable for the Font the user wishes to use
///
public Font Font
{
get { return font; }
set { font = value; }
}
Font font;
///
/// Data to print
///
BenchControl.BenchId.Component benchId;
IList waterMeters;
IList results;
string title;
///
/// Constructor
///
/// Text to be printed
public BasicPrintDocument(BenchControl.BenchId.Component benchId,
IList waterMeters,
IList results,
string title)
{
this.benchId = benchId;
this.waterMeters = waterMeters;
this.results = results;
this.title = title;
}
///
/// Override the default onbeginPrint method of the PrintDocument Object
///
///
///
protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e)
{
base.OnBeginPrint(e);
if (font == null) { font = new Font("Times New Roman", 10); }
}
///
/// 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);
/// 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);
foreach (var testResults in results)
{
RectangleF printArea = new RectangleF(x, y, printWidth, printHeight);
e.Graphics.DrawString("Test: " + testResults.TestName, font, Brushes.Black, printArea);
y += 30;
printArea = new RectangleF(x + 100, y, printWidth, printHeight);
e.Graphics.DrawString("Watermeter1: Error = " + testResults.Meters[0].VolumeErrorPct.ToString() +
"% Result = " + testResults.Meters[0].Passed.ToString(),
font, Brushes.Black, printArea);
y += 30;
printArea = new RectangleF(x + 100, y, printWidth, printHeight);
e.Graphics.DrawString("Watermeter2: " + testResults.Meters[1].Passed.ToString(),
font, Brushes.Black, printArea);
y += 30;
printArea = new RectangleF(x + 100, y, printWidth, printHeight);
e.Graphics.DrawString("Watermeter3: " + testResults.Meters[2].Passed.ToString(),
font, Brushes.Black, printArea);
y += 50;
}
e.HasMorePages = false;
}
}
}