如何在c#

时间:2015-08-21 10:22:55

标签: c# .net

您好我在Receipt打印机上打印文本 我必须打印产品名称但产品名称覆盖数量字段我可以在特定长度后使用以下代码获取新行中的名称字符串。 第3列的产品名称为(Almond 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;
        }

1 个答案:

答案 0 :(得分:1)

您实际上并没有在CurrentY的调用之间递增DrawString变量。因此,每条线都以相同的Y坐标打印。

在每次调用DrawString之间添加CurrentY = CurrentY + 20;,然后使用

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

更好的是,不要用固定值递增它,而是使用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;

结果:
enter image description here

修改
要将文本放入给定区域,您需要使用DrawString的不同重载。您需要使用给定的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;

DestinationRectangle定义文本的打印位置并自动换行。问题是,行间距是由您使用的字体定义的,正如您在此示例中可以看到的使用不同的字体:

enter image description here

如果找不到适合自己的字体(或定义自己的字体),则需要将文本拆分为单词,并通过逐字逐句地将它们装入给定的矩形中,并使用MeasureString函数,当超出限制时断开线。 但要注意,文本布局很难,非常非常难以让所有角落情况正确。