首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何修复在c#中打印内容时页面溢出的文本

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

Stack Overflow用户
提问于 2017-02-06 00:42:03
回答 2查看 93关注 0票数 2

我正在试着用c#写一张要打印的便条。一些文本从论文中溢出,如下所示:

这是我用来写这段代码的代码

代码语言:javascript
代码运行次数:0
运行
复制
  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));

    }

我如何修复图片中红色圆圈的价格,使其不溢出并对齐。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-06 00:49:49

页面左边的数字不能使用PadRight(100),因为中间的列和数据不一样。最好为它们的起点设置一个固定的宽度。

代码语言:javascript
代码运行次数:0
运行
复制
string prodQnty = product.QuantityOrdered.ToString().PadRight(50);
string prodDesc = product.Description.PadRight(110 - product.Description.Length);
票数 -1
EN

Stack Overflow用户

发布于 2017-02-06 01:16:28

因为你打印的是数字和文本,所以如果打印的数字是右对齐的,而描述是左对齐的,通常会更“吸引人”。

除了填充,你也可以使用制表符,但是使用左对齐和右对齐就有点困难了。

就我个人而言,我将分别为数量、描述和总行项目价格以及右、左和右对齐定义三个矩形。

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

希望这对你有所帮助,并祝你编码愉快。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42054600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档