【发布时间】:2015-08-06 04:05:12
【问题描述】:
我有一些代码用于从 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
------------------------------------------
【问题讨论】:
-
您希望最终输出是什么? (你想要什么居中,右/左对齐?)
-
喜欢文字“欢迎以 MSST 为中心
-
你应该看看string formats!给drawstring方法一个布局矩形。
-
如果您使用的是等宽字体(Courier New),您可以通过计算字符数和适当数量的空格前缀来使文本居中。
标签: c# winforms printing alignment