Last active
December 31, 2015 00:59
-
-
Save peace2048/7910790 to your computer and use it in GitHub Desktop.
レシートプリンターに出力するのに使うつもり
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Drawing.Printing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Printer.Sample | |
{ | |
/// <summary> | |
/// レシートプリンターへの印刷 | |
/// </summary> | |
public class ReceiptPrinter | |
{ | |
static ReceiptPrinter() | |
{ | |
Japanes11 = new Font("MS ゴシック", 9.5f, FontStyle.Regular); | |
DefaultFont = Japanes11; | |
} | |
/// <summary> | |
/// Gets the japanes11 font. | |
/// </summary> | |
/// <value> | |
/// The japanes11 font. | |
/// </value> | |
public static Font Japanes11 { get; private set; } | |
/// <summary> | |
/// Gets or sets the default font. | |
/// </summary> | |
/// <value> | |
/// The default font. | |
/// </value> | |
public static Font DefaultFont { get; set; } | |
/// <summary> | |
/// Gets or sets the name of the printer. | |
/// </summary> | |
/// <value> | |
/// The name of the printer. | |
/// </value> | |
public static string PrinterName { get; set; } | |
/// <summary> | |
/// Prints the specified execute. | |
/// </summary> | |
/// <param name="execute">The execute.</param> | |
public static void Print(Action<PrinterWriter> execute) | |
{ | |
using (var pd = new PrintDocument()) | |
{ | |
if (!string.IsNullOrEmpty(ReceiptPrinter.PrinterName)) | |
{ | |
pd.PrinterSettings.PrinterName = ReceiptPrinter.PrinterName; | |
} | |
pd.PrintPage += (o, e) => | |
{ | |
execute(new PrinterWriter(e)); | |
e.HasMorePages = false; | |
}; | |
pd.Print(); | |
} | |
} | |
} | |
/// <summary> | |
/// プリンターへの描画を処理します。 | |
/// </summary> | |
public class PrinterWriter | |
{ | |
private Graphics _g; | |
private float _pageWidth; | |
private float _lineHeight; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="PrinterWriter"/> class. | |
/// </summary> | |
/// <param name="e">The <see cref="PrintPageEventArgs"/> instance containing the event data.</param> | |
public PrinterWriter(PrintPageEventArgs e) | |
{ | |
_g = e.Graphics; | |
_pageWidth = e.PageBounds.Width; | |
this.CurrentFont = ReceiptPrinter.DefaultFont; | |
_g.DrawLine(Pens.Red, 0, _lineHeight / 2, _pageWidth, _lineHeight / 2); | |
} | |
private Font _font; | |
/// <summary> | |
/// Gets or sets the current font. | |
/// </summary> | |
/// <value> | |
/// The current font. | |
/// </value> | |
public Font CurrentFont | |
{ | |
get { return _font; } | |
set | |
{ | |
_font = value; | |
_lineHeight = _font.GetHeight(_g); | |
MaxChars = ComputeMaxChars(10, 10); | |
} | |
} | |
private int ComputeMaxChars(int chars, int add) | |
{ | |
var w =_g.MeasureString(new string('W', chars), CurrentFont).Width; | |
if (w < _pageWidth) | |
{ | |
chars += add; | |
while (_g.MeasureString(new string('W', chars), CurrentFont).Width < _pageWidth) | |
{ | |
chars += add; | |
} | |
if (add == 1) return chars - 1; | |
return ComputeMaxChars(chars, add / 2); | |
} | |
else if (w > _pageWidth) | |
{ | |
chars -= add; | |
while (_g.MeasureString(new string('W', chars), CurrentFont).Width > _pageWidth) | |
{ | |
chars -= add; | |
} | |
if (add == 1) return chars; | |
return ComputeMaxChars(chars, add / 2); | |
} | |
else | |
{ | |
return chars; | |
} | |
} | |
/// <summary> | |
/// Gets or sets the current x. | |
/// </summary> | |
/// <value> | |
/// The current x. | |
/// </value> | |
public float CurrentX { get; set; } | |
/// <summary> | |
/// Gets or sets the current y. | |
/// </summary> | |
/// <value> | |
/// The current y. | |
/// </value> | |
public float CurrentY { get; set; } | |
/// <summary> | |
/// Gets the maximum chars. | |
/// </summary> | |
/// <value> | |
/// The maximum chars. | |
/// </value> | |
public int MaxChars { get; private set; } | |
/// <summary> | |
/// Writes the line. | |
/// </summary> | |
/// <param name="text">The text.</param> | |
public void WriteLine(string text) | |
{ | |
_g.DrawString(text, CurrentFont, Brushes.Black, CurrentX, CurrentY); | |
CurrentY += _lineHeight; | |
CurrentX = 0; | |
} | |
/// <summary> | |
/// Draws the image fill. | |
/// </summary> | |
/// <param name="img">The img.</param> | |
public void DrawImageFill(Image img) | |
{ | |
float x = 0; | |
float y = CurrentY; | |
float w = img.Width; | |
float h = img.Height; | |
if (w != _pageWidth) | |
{ | |
h = h * (_pageWidth / w); | |
w = _pageWidth; | |
} | |
_g.DrawImage(img, x, y, w, h); | |
CurrentY = y + h; | |
CurrentX = 0; | |
} | |
/// <summary> | |
/// Draws the image center. | |
/// </summary> | |
/// <param name="img">The img.</param> | |
public void DrawImageCenter(Image img) | |
{ | |
float x = 0; | |
float y = CurrentY; | |
float w = img.Width; | |
float h = img.Height; | |
if (w > _pageWidth) | |
{ | |
h = h * (_pageWidth / w); | |
w = _pageWidth; | |
} | |
else if (w < _pageWidth) | |
{ | |
x = (_pageWidth - w) / 2; | |
} | |
_g.DrawImage(img, x, y, w, h); | |
CurrentY = y + h; | |
CurrentX = 0; | |
} | |
} | |
/// <summary> | |
/// 文字列に拡張メソッドを追加します。 | |
/// </summary> | |
public static class StringExt | |
{ | |
/// <summary> | |
/// Pads the right bytes. | |
/// </summary> | |
/// <param name="text">The text.</param> | |
/// <param name="width">The width.</param> | |
/// <param name="paddingChar">The padding character.</param> | |
/// <param name="eclips">if set to <c>true</c> [eclips].</param> | |
/// <returns></returns> | |
static public string PadRightB(this string text, int width, char paddingChar = ' ', bool eclips = false) | |
{ | |
var enc = Encoding.GetEncoding("sjis"); | |
var w = enc.GetByteCount(text); | |
if (w > width) | |
{ | |
var ew = eclips ? 2 : 0; | |
while (w + ew > width) | |
{ | |
text = text.Substring(0, text.Length - 1); | |
w = enc.GetByteCount(text); | |
} | |
if (eclips) | |
{ | |
text = text + "…"; | |
w += 2; | |
} | |
} | |
if (w < width) | |
{ | |
var paddingCharWidth = enc.GetByteCount(new[] { paddingChar }); | |
var padCount = (width - w) / paddingCharWidth; | |
if (padCount > 0) | |
{ | |
text = text + new string(paddingChar, padCount); | |
w += (padCount * paddingCharWidth); | |
} | |
if (w < width) | |
{ | |
text += new string(' ', width - w); | |
} | |
} | |
return text; | |
} | |
/// <summary> | |
/// Pads the left bytes. | |
/// </summary> | |
/// <param name="text">The text.</param> | |
/// <param name="width">The width.</param> | |
/// <param name="paddingChar">The padding character.</param> | |
/// <param name="eclips">if set to <c>true</c> [eclips].</param> | |
/// <returns></returns> | |
static public string PadLeftB(this string text, int width, char paddingChar = ' ', bool eclips = false) | |
{ | |
var enc = Encoding.GetEncoding("sjis"); | |
var w = enc.GetByteCount(text); | |
if (w > width) | |
{ | |
var ew = eclips ? 2 : 0; | |
while (w + ew > width) | |
{ | |
text = text.Substring(1); | |
w = enc.GetByteCount(text); | |
} | |
if (eclips) | |
{ | |
text = "…" + text; | |
w += 2; | |
} | |
} | |
if (w < width) | |
{ | |
var paddingCharWidth = enc.GetByteCount(new[] { paddingChar }); | |
var padCount = (width - w) / paddingCharWidth; | |
if (padCount > 0) | |
{ | |
text = new string(paddingChar, padCount) + text; | |
w += (padCount * paddingCharWidth); | |
} | |
if (w < width) | |
{ | |
text = new string(' ', width - w) + text; | |
} | |
} | |
return text; | |
} | |
/// <summary> | |
/// Pads the side bytes. | |
/// </summary> | |
/// <param name="text">The text.</param> | |
/// <param name="width">The width.</param> | |
/// <param name="paddingChar">The padding character.</param> | |
/// <returns></returns> | |
static public string PadSideB(this string text, int width, char paddingChar = ' ') | |
{ | |
var enc = Encoding.GetEncoding("sjis"); | |
var w = enc.GetByteCount(text); | |
var right = true; | |
while (w > width) | |
{ | |
if (right) | |
{ | |
text = text.Substring(0, text.Length - 1); | |
} | |
else | |
{ | |
text = text.Substring(1); | |
} | |
w = enc.GetByteCount(text); | |
} | |
if (w < width) | |
{ | |
var paddingCharWidth = enc.GetByteCount(new[] { paddingChar }); | |
while (w + paddingCharWidth <= width) | |
{ | |
if (right) | |
{ | |
text = text + paddingChar; | |
} | |
else | |
{ | |
text = paddingChar + text; | |
} | |
right = !right; | |
w += paddingCharWidth; | |
} | |
while (w < width) | |
{ | |
if (right) | |
{ | |
text = text + " "; | |
} | |
else | |
{ | |
text = " " + text; | |
} | |
right = !right; | |
w++; | |
} | |
} | |
return text; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment