2

I am trying to write a note to print using c#. Some of the text overflows away from the paper like this:

This is my code used to write this

  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));

    }

How do I fix the price circled red in the picture from overflowing away and be aligned.

  • Since Arial is not a fixed size Font you need to set the x-position for each column you want to create. __Padding__ only works for Fixed Fonts like `Consolas` or `Courier`.. also do use a right-aligned StringFormat for the last column! See [here](http://stackoverflow.com/questions/41920220/format-a-receipt-with-winforms-application/41921150#41921150) and [here for examples](http://stackoverflow.com/questions/28560319/generate-staff-card/28580657?s=28|0.0468#28580657) – TaW Feb 05 '17 at 17:15
  • Use a smaller font, make your columns not as wide, print in landscape mode, use Graphics.ScaleTransform(). Oodles of options. – Hans Passant Feb 05 '17 at 17:21
  • Please do not vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](https://meta.stackoverflow.com/q/323395) – NobodyNada May 04 '17 at 22:18

2 Answers2

0

Since you're printing numbers and text, it is usually a bit more 'appealing' if the printed numbers are right-aligned, while the description is left-aligned.

Instead of padding you can also use tabs, but that is a bit harder with using left- and right-alignments.

Personally I would define three rectangles for the quantity, description and total line-item price, and right-, left- and right-align respectively.

You can find an example on MSDN here: https://msdn.microsoft.com/en-us/library/332kzs7c(v=vs.110).aspx

Hope this helps, and happy coding.

-1

You mustn't use PadRight(100) for the numbers at left of page, Because the middle column has not same with data. It is better to set a fixed width for their start point.

string prodQnty = product.QuantityOrdered.ToString().PadRight(50);
string prodDesc = product.Description.PadRight(110 - product.Description.Length);
Farzin Kanzi
  • 3,380
  • 2
  • 21
  • 23
  • That should not really matter. The padding makes sure that the string for quantity and description are of a fixed length (50 and 100 respectively) assuming that neither the quantity and description exceed the 50 and 100 characters. – Michaël van der Haven Feb 05 '17 at 17:03
  • 50 ch padding is true because in first column you have same width data. But 100, if you want the left numbers are in same width, you can not use a fixed value padding (100). If you do this some values go further than you want. I will edit my answer. – Farzin Kanzi Feb 05 '17 at 17:13