简体   繁体   中英

How to get contents in graphics.Drawstring in new line of c#

Hello I am printing a text on Receipt printer on which I have to print Product name But products name get overwrites on Qty field can i get that name string in new line after particular length I am using following Code. column 3 have a Product name Like (Almond Kesar Kanti Body Cleanser) How to get this text in new line and manage space of Invoice.

  while (i < dataGridView2.Rows.Count)
        {

            if (height > e.MarginBounds.Height)
            {

                height = 500;

                width = 500;

                //e.HasMorePages = true;

                return;

            }

            //height += dataGridView1.Rows[i].Height;

            // e.Graphics.DrawRectangle(Pens.Black, 20, height, dataGridView1.Columns[0].Width, dataGridView1.Rows[0].Height);

            e.Graphics.DrawString(dataGridView2.Rows[i].Cells["Column3"].FormattedValue.ToString(), dataGridView2.Font, Brushes.Black, CurrentX, CurrentY + 20);
            e.Graphics.DrawString(dataGridView2.Rows[i].Cells["Column4"].FormattedValue.ToString(), dataGridView2.Font, Brushes.Black, CurrentX + 150, CurrentY + 20);
            // e.Graphics.DrawString(dataGridView2.Rows[i].Cells["Column5"].FormattedValue.ToString(), dataGridView2.Font, Brushes.Black, CurrentX + 150, CurrentY + 20);
            e.Graphics.DrawString(dataGridView2.Rows[i].Cells["Column10"].FormattedValue.ToString(), dataGridView2.Font, Brushes.Black, CurrentX + 200, CurrentY + 20);
            i++;
            CurrentY = CurrentY + 20;
        }

You are not actually incrementing your CurrentY variable between the calls to DrawString . Therefore each line is printed at the same Y-coordinate.

Add CurrentY = CurrentY + 20; between each call to DrawString and just use

e.Graphics.DrawString(dataGridView2.Rows[i].Cells["Column3"].FormattedValue.ToString(), dataGridView2.Font, Brushes.Black, CurrentX, CurrentY);

Better yet, don't increment it by a fixed value but use e.Graphics.MeasureString to calculate the actual height of each line and increment by this value+spacing, like in this example:

Bitmap bmp = new Bitmap(200, 200);
using (Graphics g = Graphics.FromImage(bmp)) {
    //The individual lines to draw
    string[] lines = {
        "Foo1",
        "Foo2",
        "Foo3"
    };
    int y = 1; //The starting Y-coordinate
    int spacing = 10; //The space between each line in pixels
    for (i = 0; i <= lines.Count - 1; i++) {
        g.DrawString(lines[i], this.Font, Brushes.Black, 1, y);
        //Increment the coordinate after drawing each line
        y += Convert.ToInt32(g.MeasureString(lines[i], this.Font).Height) + spacing;
    }
}
PictureBox1.Image = bmp;

Results:
在此处输入图片说明

Edit
To fit text into a given area you need to use a different overload of DrawString . You need to draw the string with a given LayoutRectangle like this:

Bitmap bmp = new Bitmap(200, 200);
using (Graphics g = Graphics.FromImage(bmp)) {
    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
    string LongText = "This is a really long text that should be automatically wrapped to a certain rectangle.";
    Rectangle DestinationRectangle = new Rectangle(10, 10, 100, 150);
    g.DrawRectangle(Pens.Red, DestinationRectangle);
    using (StringFormat sf = new StringFormat()) {
        g.DrawString(LongText, new Font("Arial", 9), Brushes.Black, DestinationRectangle, sf);
    }
}
PictureBox1.Image = bmp;

The DestinationRectangle defines where the text is printed and it is automatically wrapped. The problem is, that the line spacing is defined by the Font you use, as you can see in this example with different fonts:

在此处输入图片说明

If you can't find a font that works for you (or define your own) you would need to split the text into words and fit them yourself into the given rectangle by drawing them word for word, measuring each with the MeasureString function and break the line when you would go over the limit. But beware, text layout is hard, very very hard, to get all the corner cases right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM