簡體   English   中英

如何在C#中打印內容時解決文本從頁面溢出的問題

[英]How do I fix text overflowing away from page when printing something in c#

我正在嘗試編寫注釋以使用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));

    }

我如何確定圖中圈出紅色的價格不會溢出並對齊。

由於您要打印數字和文本,因此,如果打印的數字右對齊,而描述則是左對齊,則通常更具“吸引力”。

除了填充之外,您還可以使用制表符,但是使用左對齊和右對齊會有點困難。

我個人將為數量,描述和總行價格以及右對齊,左對齊和右對齊定義三個矩形。

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

希望對您有所幫助,並祝您編程愉快。

您不得在頁面左側使用PadRight(100)作為數字,因為中間的列與數據不相同。 最好為它們的起點設置一個固定的寬度。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM