收据打印的中心文本
Center text for receipt printing
我有一些代码用于从 C# 打印收据。
下面的代码打印正常,但我很难将文本左右对齐并居中,
Graphics graphics = e.Graphics;
Font font = new Font("Courier New", 10);
float fontHeight = font.GetHeight();
int startX = 0;
int startY = 0;
int Offset = 0;
graphics.DrawString("Welcome to MSST", new Font("Courier New", 14), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
graphics.DrawString("Recept No :" + receptno + 1, new Font("Courier New", 14), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
graphics.DrawString("Date :" + DateTime.Today, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
graphics.DrawString("------------------------------------------", new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
谁能帮我解决文本对齐问题?
更新: 这是所需的输出:
Welcome to MSST
Receipt No : 3
Date : 5/24/2014 10:06:22
------------------------------------------
这是您示例的完整代码,使用三个 StringFormats
和添加的一行来显示右对齐文本。我还添加了一个前导数字并将所有内容转换为 floats
..我正在使用 Panel
绘制并将布局 Rectangle
设置为面板的尺寸。当然,你应该使用你的打印目标..
int receptno = 42;
Graphics graphics = e.Graphics;
Font font10 = new Font("Courier New", 10);
Font font12 = new Font("Courier New", 12);
Font font14 = new Font("Courier New", 14);
float leading = 4;
float lineheight10 = font10.GetHeight() + leading;
float lineheight12 = font12.GetHeight() + leading;
float lineheight14 = font14.GetHeight() + leading;
float startX = 0;
float startY = leading;
float Offset = 0;
StringFormat formatLeft = new StringFormat(StringFormatFlags.NoClip);
StringFormat formatCenter = new StringFormat(formatLeft);
StringFormat formatRight = new StringFormat(formatLeft);
formatCenter.Alignment = StringAlignment.Center;
formatRight.Alignment = StringAlignment.Far;
formatLeft.Alignment = StringAlignment.Near;
SizeF layoutSize = new SizeF(yourPrintAreaWidth - Offset * 2, lineheight14);
RectangleF layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
Brush brush = Brushes.Black;
graphics.DrawString("Welcome to MSST", font14, brush, layout, formatCenter);
Offset = Offset + lineheight14;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
graphics.DrawString("Recept No :" + receptno + 1, font14, brush, layout, formatLeft);
Offset = Offset + lineheight14;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
graphics.DrawString("Date :" + DateTime.Today, font12, brush, layout, formatLeft);
Offset = Offset + lineheight12;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
graphics.DrawString("".PadRight(46,'_'), font10, brush, layout, formatLeft);
Offset = Offset + lineheight10;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
graphics.DrawString("copyright SO", font10, brush, layout, formatRight);
Offset = Offset + lineheight10;
font10.Dispose(); font12.Dispose(); font14.Dispose();
此代码适用于文本中心
Center = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString("Valley boulevard Auto Center", HeadingFont).Width / 2)
我有一些代码用于从 C# 打印收据。
下面的代码打印正常,但我很难将文本左右对齐并居中,
Graphics graphics = e.Graphics;
Font font = new Font("Courier New", 10);
float fontHeight = font.GetHeight();
int startX = 0;
int startY = 0;
int Offset = 0;
graphics.DrawString("Welcome to MSST", new Font("Courier New", 14), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
graphics.DrawString("Recept No :" + receptno + 1, new Font("Courier New", 14), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
graphics.DrawString("Date :" + DateTime.Today, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
graphics.DrawString("------------------------------------------", new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
Offset = Offset + 20;
谁能帮我解决文本对齐问题?
更新: 这是所需的输出:
Welcome to MSST
Receipt No : 3
Date : 5/24/2014 10:06:22
------------------------------------------
这是您示例的完整代码,使用三个 StringFormats
和添加的一行来显示右对齐文本。我还添加了一个前导数字并将所有内容转换为 floats
..我正在使用 Panel
绘制并将布局 Rectangle
设置为面板的尺寸。当然,你应该使用你的打印目标..
int receptno = 42;
Graphics graphics = e.Graphics;
Font font10 = new Font("Courier New", 10);
Font font12 = new Font("Courier New", 12);
Font font14 = new Font("Courier New", 14);
float leading = 4;
float lineheight10 = font10.GetHeight() + leading;
float lineheight12 = font12.GetHeight() + leading;
float lineheight14 = font14.GetHeight() + leading;
float startX = 0;
float startY = leading;
float Offset = 0;
StringFormat formatLeft = new StringFormat(StringFormatFlags.NoClip);
StringFormat formatCenter = new StringFormat(formatLeft);
StringFormat formatRight = new StringFormat(formatLeft);
formatCenter.Alignment = StringAlignment.Center;
formatRight.Alignment = StringAlignment.Far;
formatLeft.Alignment = StringAlignment.Near;
SizeF layoutSize = new SizeF(yourPrintAreaWidth - Offset * 2, lineheight14);
RectangleF layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
Brush brush = Brushes.Black;
graphics.DrawString("Welcome to MSST", font14, brush, layout, formatCenter);
Offset = Offset + lineheight14;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
graphics.DrawString("Recept No :" + receptno + 1, font14, brush, layout, formatLeft);
Offset = Offset + lineheight14;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
graphics.DrawString("Date :" + DateTime.Today, font12, brush, layout, formatLeft);
Offset = Offset + lineheight12;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
graphics.DrawString("".PadRight(46,'_'), font10, brush, layout, formatLeft);
Offset = Offset + lineheight10;
layout = new RectangleF(new PointF(startX, startY + Offset), layoutSize);
graphics.DrawString("copyright SO", font10, brush, layout, formatRight);
Offset = Offset + lineheight10;
font10.Dispose(); font12.Dispose(); font14.Dispose();
此代码适用于文本中心
Center = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString("Valley boulevard Auto Center", HeadingFont).Width / 2)