我正在试着用c#写一张要打印的便条。一些文本从论文中溢出,如下所示:
这是我用来写这段代码的代码
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
/*A note with all the order details is printed for the kitchen staff
*/
e.Graphics.DrawString("Daddy John’s restaurant", new Font("Forte", 25, FontStyle.Bold), Brushes.Black, new Point(200, 30));
e.Graphics.DrawString("Kitchen Staff Note", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(200, 70));
e.Graphics.DrawString("Order taken by: " + dataTransferToOtherForms.LoginDetails.UserName, new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(200, 100));
e.Graphics.DrawString("Order belongs to table: " + dataTransferToOtherForms.TableName, new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(200, 125));
e.Graphics.DrawString("-------------" + DateTime.Now, new Font("Courier", 12, FontStyle.Bold), Brushes.Black, new Point(25, 150));
//Displaying Date Time on the note
e.Graphics.DrawString("Ordered On: " + DateTime.Now, new Font("Courier", 12, FontStyle.Bold), Brushes.Black, new Point(25, 200));
//Constants for the products
string font = "Arial";
int ycord = 300;
int xcord = 25;
//
foreach (ProductSelected product in productsObjList)
{
string prodQnty = product.QuantityOrdered.ToString().PadRight(50);
string prodDesc = product.Description.PadRight(100);
string prodPrice = "£" + product.Price.ToString();
string prodLineQntyDescPrice = prodQnty + prodDesc + prodPrice;
//Displaying the Quantity + decription + price of a product.
e.Graphics.DrawString(prodLineQntyDescPrice, new Font(font, 12, FontStyle.Regular), Brushes.Black, new Point(xcord, ycord));
ycord = ycord + 20;
}
//Adding you know
ycord = ycord + 40;
//displaying total price of receipt.
e.Graphics.DrawString("Total to pay:".PadRight(30) + Convert.ToString(transactionTot), new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(xcord, ycord));
}
我如何修复图片中红色圆圈的价格,使其不溢出并对齐。
发布于 2017-02-05 16:49:49
页面左边的数字不能使用PadRight(100)
,因为中间的列和数据不一样。最好为它们的起点设置一个固定的宽度。
string prodQnty = product.QuantityOrdered.ToString().PadRight(50);
string prodDesc = product.Description.PadRight(110 - product.Description.Length);
发布于 2017-02-05 17:16:28
因为你打印的是数字和文本,所以如果打印的数字是右对齐的,而描述是左对齐的,通常会更“吸引人”。
除了填充,你也可以使用制表符,但是使用左对齐和右对齐就有点困难了。
就我个人而言,我将分别为数量、描述和总行项目价格以及右、左和右对齐定义三个矩形。
你可以在MSDN上找到一个例子:https://msdn.microsoft.com/en-us/library/332kzs7c(v=vs.110).aspx
希望这对你有所帮助,并祝你编码愉快。
https://stackoverflow.com/questions/42054600
复制