184 lines
6.0 KiB
C#
184 lines
6.0 KiB
C#
///
|
|
/// Copyright (c) 2016 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Printing;
|
|
using Common;
|
|
|
|
namespace ResultsBrowser.Output.Printers.Statistics
|
|
{
|
|
public class StatisticsPrintDocument : PrintDocument
|
|
{
|
|
const string FontFamilyName = "Arial"; //"Times New Roman";
|
|
|
|
const int SpacingOne = 20;
|
|
const int BorderW = 1; /// Table border line width
|
|
const int TopPadding = 2; /// Margins of the text from table lines
|
|
const int LeftPadding = 2; /// Margins of the text from table lines
|
|
const int TableTop = 50;
|
|
|
|
///
|
|
/// Data to print
|
|
///
|
|
string title;
|
|
string[,] table;
|
|
|
|
/// <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 title, string[,] table, PageOrientation pageOrientation)
|
|
{
|
|
this.title = title;
|
|
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, 14, 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;
|
|
}
|
|
|
|
/// Use the StringFormat class for the text layout of our document
|
|
StringFormat format = new StringFormat(StringFormatFlags.LineLimit);
|
|
|
|
RectangleF printArea = new RectangleF(leftMargin, topMargin, 667, TableTop);
|
|
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;
|
|
}
|
|
}
|
|
|
|
float[] rowHeights = new float[table.GetLength(0)];
|
|
for (int i = 0; i < rowHeights.Length; i++) rowHeights[i] = SpacingOne;
|
|
|
|
float top = (float)(topMargin + TableTop);
|
|
|
|
DrawTable(e, leftMargin, top, columnWidths, rowHeights);
|
|
|
|
/// Print texts
|
|
for (int r = 0; r < table.GetLength(0); r++)
|
|
{
|
|
float left = leftMargin;
|
|
for (int c = 0; c < columnWidths.Length; c++)
|
|
{
|
|
float textWidth = e.Graphics.MeasureString(table[r, c], font).Width;
|
|
float leftOffset = (columnWidths[c] - textWidth) / 2;
|
|
PrintAt(e, new Point((int)(left + leftOffset + LeftPadding), (int)top + 2), table[r, c]);
|
|
left += columnWidths[c];
|
|
}
|
|
|
|
top += rowHeights[r];
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Draw table lines.
|
|
/// </summary>
|
|
void DrawTable(System.Drawing.Printing.PrintPageEventArgs e, float left, float top, float[] columnWidths, float[] rowHeights)
|
|
{
|
|
float width = 0;
|
|
foreach (var cw in columnWidths) width += cw;
|
|
|
|
/// First horizontal line
|
|
e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point((int)left, (int)top), new Point((int)(left + width), (int)top));
|
|
|
|
float height = 0;
|
|
foreach (var rh in rowHeights)
|
|
{
|
|
height += rh;
|
|
|
|
/// Horizontal line
|
|
e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point((int)left, (int)(top + height)), new Point((int)(left + width), (int)(top + height)));
|
|
}
|
|
|
|
/// First vertical line
|
|
e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point((int)left, (int)top), new Point((int)left, (int)(top + height)));
|
|
|
|
|
|
width = 0;
|
|
foreach (var cw in columnWidths)
|
|
{
|
|
width += cw;
|
|
|
|
/// Vertical line
|
|
e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point((int)(left + width), (int)top), new Point((int)(left + width), (int)(top + height)));
|
|
}
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|