tbf/ResultsBrowser/Output/Printers/Statistics/StatisticsPrintDocument.cs
2016-11-21 11:03:44 +01:00

188 lines
6.6 KiB
C#

///
/// Copyright (c) 2016 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using Config.Entities;
namespace ResultsBrowser.Output.Printers.Statistics
{
public class StatisticsPrintDocument : PrintDocument
{
const string FontFamilyName = "Arial"; //"Times New Roman";
const int SpacingOne = 18;
const int SpacingOneAndHalf = (3 * SpacingOne) / 2;
const int TitleX = 240;
const int TitleY = 180; /// Depends on the size of the header
const int FirstLineNr = 14; /// Depends on the size of the header
const int Tab1 = 80; /// Positions of columns above the table
const int Tab2 = 240;
const int Tab3 = 430;
const int Tab4 = 590;
const int BorderW = 1; /// Table border line width
const int MarginTop = 7; /// Margins of the text from table lines
const int MarginLeft = 4; /// Margins of the text from table lines
///
/// Data to print
///
string[,] table;
/// Top coordinate of the table
int top;
/// <summary>
/// Property variable for the Font the user wishes to use
/// </summary>
Font font;
Font boldFont;
Font fontTitle;
/// <summary>
/// Constructor
/// </summary>
/// <param name="textToPrint">Text to be printed</param>
public StatisticsPrintDocument(string[,] table, PageOrientation pageOrientation)
{
this.table = table;
DefaultPageSettings.Landscape = (pageOrientation == PageOrientation.Landscape);
}
/// <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, 10, FontStyle.Regular); }
if (boldFont == null) { boldFont = new Font(FontFamilyName, 10, FontStyle.Bold); }
if (fontTitle == null) { fontTitle = new Font(FontFamilyName, 24, FontStyle.Bold); }
}
/// <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);
/// 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);
RectangleF printArea = new RectangleF(TitleX, TitleY, 667, 50);
e.Graphics.DrawString("TITLE", this.fontTitle, Brushes.Black, printArea);
/// Measure texts
float[] columnWidths = new float[table.GetLength(1)];
for (int c = 0; c < columnWidths.Length; c++)
{
columnWidths[c] = e.Graphics.MeasureString(table[0, c], font).Width;
for (int r = 1; r < table.GetLength(0); r++)
{
float width = e.Graphics.MeasureString(table[r, c], font).Width;
if (width > columnWidths[c]) columnWidths[c] = width;
}
}
/// Print texts
float top = topMargin;
for (int r = 0; r < table.GetLength(0); r++)
{
float left = leftMargin;
for (int c = 0; c < columnWidths.Length; c++)
{
PrintAt(e, new Point((int)left, (int)top), table[r, c]);
left += columnWidths[c];
}
top += SpacingOne;
}
}
/// <summary>
/// Draw table lines.
/// </summary>
void DrawTable(System.Drawing.Printing.PrintPageEventArgs e)
{
//int mid = top + SpacingOneAndHalf;
//int bot = top + (rowValues.Count + 1) * SpacingOneAndHalf;
//// Horizontal lines
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, top), new Point(747, top));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, mid), new Point(747, mid));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, bot), new Point(747, bot));
//// Vertical lines
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point( 80, top), new Point( 80, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(267, top), new Point(267, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(347, top), new Point(347, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(427, top), new Point(427, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(507, top), new Point(507, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(587, top), new Point(587, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(667, top), new Point(667, bot));
//e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(747, top), new Point(747, bot));
}
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, SpacingOne);
e.Graphics.DrawString(text, this.font, Brushes.Black, printArea);
}
void PrintBoldAt(System.Drawing.Printing.PrintPageEventArgs e, Point p, string text)
{
PrintBoldAt(e, p.X, p.Y, text);
}
void PrintBoldAt(System.Drawing.Printing.PrintPageEventArgs e, int x, int y, string text)
{
RectangleF printArea = new RectangleF(x, y, 667, SpacingOne);
e.Graphics.DrawString(text, this.boldFont, Brushes.Black, printArea);
}
Point FromTest(int testNr, int parNr)
{
const int NrParamsInOneTableRow = 6;
int x = 267 + (parNr % NrParamsInOneTableRow) * 80 + MarginLeft;
int y = top + SpacingOneAndHalf + MarginTop + testNr * SpacingOneAndHalf;
if (parNr < 0) x = 80 + MarginLeft; /// When parNr == -1 return the left column x coordinate
return new Point(x, y);
}
}
}