Essa Khan Essa Khan - 4 years ago 119
C# Question

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

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

enter image description here

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.

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download