在c#中打印内容时,如何修复文本溢出页面

时间:2017-02-05 16:42:03

标签: c# printing

我正在尝试使用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));

    }

如何确定图片中红色圆圈的价格溢出并对齐。

2 个答案:

答案 0 :(得分:0)

由于您正在打印数字和文本,如果打印的数字是右对齐的,那么它通常会更具吸引力,而描述是左对齐的。

您也可以使用制表符代替填充,但使用左对齐和右对齐会更难。

我个人会为数量,描述和总的行项目价格定义三个矩形,分别为右,左和右对齐。

您可以在MSDN上找到一个示例:https://msdn.microsoft.com/en-us/library/332kzs7c(v=vs.110).aspx

希望这有帮助,并且编码很快。

答案 1 :(得分:-1)

您不能将PadRight(100)用于页面左侧的数字,因为中间列与数据不相同。最好为起点设置固定宽度。

string prodQnty = product.QuantityOrdered.ToString().PadRight(50);
string prodDesc = product.Description.PadRight(110 - product.Description.Length);