C# 字符串。格式未与固定宽度对齐
这是我为订单打印收据的功能,打印工作正常,但我的问题是打印未生成对齐的记录。我已经附加了一个屏幕截图的输出在底部请告诉我,我这样做,使输出良好C# 字符串。格式未与固定宽度对齐,c#,printing,receipt,C#,Printing,Receipt,这是我为订单打印收据的功能,打印工作正常,但我的问题是打印未生成对齐的记录。我已经附加了一个屏幕截图的输出在底部请告诉我,我这样做,使输出良好 private void PrintReceipt(object sender, PrintPageEventArgs e) { //Graphics g = this.CreateGraphics(); Graphics graphic = e.Graphics; Font font = new
private void PrintReceipt(object sender, PrintPageEventArgs e)
{
//Graphics g = this.CreateGraphics();
Graphics graphic = e.Graphics;
Font font = new Font("Courier", 8);
Font Heading = new Font("Courier", 10);
float fontHeight = font.GetHeight();
int startX = 10;
int startY = 10;
int offset = 40;
int receipt_w = 400;
//int receipt_h = 0;
string company_name = "Welcome to Arslan Medicose";
string company_address = "Kohenoor One Jaranwala road faisalbad";
string company_phone = "Phone:+9233898937";
string developer_str = "Developed by: virk";
string developer_contact = "Phone:+923338989937";
string underLine = "-------------------------------------------------------------------------------------------------------";
SizeF size = graphic.MeasureString(company_name, Heading);
graphic.DrawString(company_name, Heading, new SolidBrush(Color.Black), (receipt_w - size.Width) / 2, startY);
//offset = offset + (int)fontHeight + 5;
size = graphic.MeasureString(company_address, Heading);
graphic.DrawString(company_address, font, new SolidBrush(Color.Black), (receipt_w - size.Width) / 2, startY + offset);
offset = offset + (int)fontHeight + 5;
size = graphic.MeasureString(company_phone, Heading);
graphic.DrawString(company_phone, font, new SolidBrush(Color.Black), (receipt_w - size.Width) / 2, startY + offset);
offset = offset + (int)fontHeight + 5;
graphic.DrawString(underLine, font, new SolidBrush(Color.Black), startX, startY + offset);
offset = offset + (int)fontHeight + 5;
int total_rows = cart_pro_gv.RowCount;
int recs = 1;
foreach (DataGridViewRow row in cart_pro_gv.Rows) {
if (recs < total_rows)
{
string pn = (null != row.Cells[1].Value) ? row.Cells[1].Value.ToString().Trim() : String.Empty;
string pq = (null != row.Cells[2].Value) ? row.Cells[2].Value.ToString().Trim() : String.Empty;
string pp = (null != row.Cells[3].Value) ? row.Cells[3].Value.ToString().Trim() : String.Empty;
string ppt = (null != row.Cells[4].Value) ? row.Cells[4].Value.ToString().Trim() : String.Empty;
string pline = string.Format("{0,-40}|{1,-15}|{2,-15}|{3,-15}", pn, pq, pp, ppt);
//string pline = string.Format("{0}|{1}|{2}|{3}", pn.PadRight(30), pq.PadRight(30), pp.PadRight(30), ppt.PadRight(30));
/*int padding = 3;
int maxCol0width = pn.Length;
int maxCol1width = pq.Length;
int maxCol2width = pp.Length;
int maxCol3width = ppt.Length;
string fmt0 = "{0,-" + (maxCol0width + padding) + "}";
string fmt1 = "{1,-" + (maxCol1width + padding) + "}";
string fmt2 = "{2,-" + (maxCol2width + padding) + "}";
string fmt3 = "{3,-" + (maxCol3width + padding) + "}";
string fmt = fmt0 + fmt1 + fmt2 + fmt3;
string pline = string.Format(fmt, pn, pq, pp, ppt);*/
graphic.DrawString(pline, font, Brushes.Black, startX, startY + offset);
offset = offset + (int)fontHeight + 5;
}
recs++;
}
// offset = offset + (int)fontHeight + 5;
graphic.DrawString(underLine, font, Brushes.Black , startX, startY + offset);
offset = offset + 10;
string total_paying = "Net Total: " + cart_total_label.Text;
graphic.DrawString(total_paying, font, new SolidBrush(Color.Black), startX, startY + offset);
}
下面是我得到的输出:
对齐问题是由左侧字符串的长度变化引起的。请注意,Perasilamole字符串是最短的字符串,因此数字最左边。我不太清楚你的代码是如何工作的,但也许你可以根据药物名称的长度改变药物名称后的空格数?我本来打算建议使用String.PadRight,但看起来你已经尝试过了。你在使用它时遇到了什么问题?马克,我很困惑它到底发生了什么,为什么它会采用这种观点。我理解,只是想找出你为什么注释掉String.PadRight行。如果您知道字段的长度,并且像您一样使用单间距字体,那么应该可以使用。显示的字体不是Courier,也不是单间距字体。