简体   繁体   English

如何在c#的新行中获取graphics.Drawstring中的内容

[英]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. 第3列具有产品名称,如(杏仁Kesar Kanti身体清洁剂)如何在换行中获取此文本并管理发票的空间。

  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 . 您实际上并没有在对DrawString的调用之间增加CurrentY变量。 Therefore each line is printed at the same Y-coordinate. 因此,每行都以相同的Y坐标打印。

Add CurrentY = CurrentY + 20; CurrentY = CurrentY + 20; between each call to DrawString and just use 在每次调用DrawString并使用之间

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: 更好的是,不要将其递增固定值,而是使用e.Graphics.MeasureString来计算每行的实际高度,并以该值+间距递增,如以下示例所示:

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 . 为了使文本适合给定区域,您需要使用不同的DrawString重载。 You need to draw the string with a given LayoutRectangle like this: 您需要使用给定的LayoutRectangle绘制字符串,如下所示:

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. DestinationRectangle定义了文本的打印位置,并自动对其进行换行。 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. 如果找不到适合您的字体(或定义自己的字体),则需要将文本拆分为多个单词,然后逐个单词地绘制它们,并使用MeasureString函数对其进行测量,然后将其拆分,从而将它们调整为给定的矩形超出限制的线。 But beware, text layout is hard, very very hard, to get all the corner cases right. 但是请注意,要正确处理所有常见情况,文本布局非常困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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