2018-03-06 17:37:43 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
using System.Drawing.Printing;
|
|
|
|
|
|
|
2022-01-23 18:56:15 +00:00
|
|
|
|
namespace Common.Printers
|
2018-03-06 17:37:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
public class PrintersCommon : PrintDocument
|
|
|
|
|
|
{
|
|
|
|
|
|
static Font lastFont = null;
|
|
|
|
|
|
static float lastSpacing = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Measure the size of a paragraph of text, lines are separated by '~'.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="e">PrintPageEventArgs</param>
|
|
|
|
|
|
/// <param name="text">Text, lines are separated by '~'</param>
|
|
|
|
|
|
/// <param name="font">Font</param>
|
|
|
|
|
|
/// <returns>Size of the paragraph of text</returns>
|
2018-03-15 15:08:19 +00:00
|
|
|
|
public static SizeF Measure(System.Drawing.Printing.PrintPageEventArgs e, string text, Font font)
|
2018-03-06 17:37:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (font != lastFont)
|
|
|
|
|
|
{
|
|
|
|
|
|
lastFont = font;
|
|
|
|
|
|
lastSpacing = e.Graphics.MeasureString("Abcgq", font).Height;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string[] lines = text.Split(new char[] { '~' });
|
|
|
|
|
|
float width = 0;
|
|
|
|
|
|
foreach (var line in lines)
|
|
|
|
|
|
{
|
|
|
|
|
|
float lineWid = e.Graphics.MeasureString(line, font).Width;
|
|
|
|
|
|
if (lineWid > width) width = lineWid;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new SizeF(width, 0.8f * lines.Length * lastSpacing + 0.2f);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Print a paragraph of text, lines are separated by '~'.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="e">PrintPageEventArgs</param>
|
|
|
|
|
|
/// <param name="text">Text, lines are separated by '~'</param>
|
|
|
|
|
|
/// <param name="font">Font</param>
|
|
|
|
|
|
/// <param name="x">Position x</param>
|
|
|
|
|
|
/// <param name="y">Position y</param>
|
|
|
|
|
|
/// <param name="width">Width (required when alignment=Alignment.Center or alignment=Aligmenr.Right</param>
|
|
|
|
|
|
/// <param name="height">Height, 0 = no vertical centering</param>
|
|
|
|
|
|
/// <param name="horizAlignment">Horizontal alignment</param>
|
|
|
|
|
|
/// <param name="vertAlignment">Vertical alignment</param>
|
2018-03-15 15:08:19 +00:00
|
|
|
|
public static void PrintAt(System.Drawing.Printing.PrintPageEventArgs e, string text, Font font,
|
|
|
|
|
|
float x, float y, float width, float height,
|
|
|
|
|
|
Alignment horizAlignment, VertAlignment vertAlignment)
|
2018-03-06 17:37:43 +00:00
|
|
|
|
{
|
2018-03-15 15:08:19 +00:00
|
|
|
|
SizeF size = PrintersCommon.Measure(e, text, font);
|
2018-03-06 17:37:43 +00:00
|
|
|
|
|
|
|
|
|
|
string[] lines = text.Split(new char[] { '~' });
|
|
|
|
|
|
|
|
|
|
|
|
if (height <= size.Height) vertAlignment = VertAlignment.Top;
|
|
|
|
|
|
float y0;
|
|
|
|
|
|
switch (vertAlignment)
|
|
|
|
|
|
{
|
|
|
|
|
|
default:
|
|
|
|
|
|
case VertAlignment.Top: y0 = y; break;
|
|
|
|
|
|
case VertAlignment.Middle: y0 = y + (height - size.Height) / 2; break;
|
|
|
|
|
|
case VertAlignment.Bottom: y0 = y + (height - size.Height); break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var line in lines)
|
|
|
|
|
|
{
|
|
|
|
|
|
float x0;
|
|
|
|
|
|
switch (horizAlignment)
|
|
|
|
|
|
{
|
|
|
|
|
|
default:
|
|
|
|
|
|
case Alignment.Left: x0 = x; break;
|
|
|
|
|
|
case Alignment.Center: x0 = x + (width - e.Graphics.MeasureString(line, font).Width) / 2; break;
|
|
|
|
|
|
case Alignment.Right: x0 = x + width - e.Graphics.MeasureString(line, font).Width; break;
|
|
|
|
|
|
}
|
|
|
|
|
|
e.Graphics.DrawString(line, font, Brushes.Black, new PointF(x0, y0));
|
|
|
|
|
|
y0 += 0.8f * lastSpacing;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|