如何在C#中为e.Graphics.DrawString执行NewLine

时间:2017-10-25 09:31:09

标签: c#

我正在使用打印预览对话框,所以我想创建一个新的Line,以便在我打印时使工作变得可理解。我有一个挑战,因为我从文本框中得到它。一切似乎都是Jammed,因此我想知道我该如何去做。

代码看起来像这样

编辑

现在我的代码现在看起来像这样:

        Image newImage = Image.FromFile("logo.png");
        int width = 80;
        int height = 50;
        int ix = 100;
        int iy = 100;
        e.Graphics.DrawImage(newImage, ix, iy, width, height);

        var fnt = new Font("Times new Roman", 14, FontStyle.Bold);
        int x = 100, y = 100;
        int dy = 20;

        var header = new Font("Calibri", 21, FontStyle.Bold);
        int hx = 100, hy = 100;
        int hdy = 20;

        e.Graphics.DrawString("Visitor GatePass™", header, Brushes.Black, new PointF(hx, hy)); hy += hdy;
        e.Graphics.DrawString("Unique Number : " + uniqueNum.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
        e.Graphics.DrawString("Full Name : " + fullname.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
        e.Graphics.DrawString("Method of Identification : " + id_method.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
        e.Graphics.DrawString("Ward Name : " + ward_name.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
        e.Graphics.DrawString("Ward Class : " + ward_class.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
        e.Graphics.DrawString("Ward House : " + ward_house.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
        e.Graphics.DrawString("House Master : " + house_master.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
        e.Graphics.DrawString("Accompanying People : " + no_accPeople.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;

虽然有些部分是Jammed,但与徽标和其他格式一样。

1 个答案:

答案 0 :(得分:0)

您的所有职位都是相同的:PointF(100, 100)。所以它们将被打印在彼此之上。你需要改变y位置。

var fnt = new Font("Times new Roman", 14, FontStyle.Bold);
int x = 100, y = 100;
int dy = (int)fnt.GetHeight(e.Graphics) * 1; //change this factor to control line spacing

e.Graphics.DrawString(uniqueNum.Text, fnt , Brushes.Black, new PointF(x, y)); y+=dy;
e.Graphics.DrawString(fullname.Text, fnt, Brushes.Black, new PointF(x, y)); y+=dy;
e.Graphics.DrawString(id_method.Text, fnt, Brushes.Black, new PointF(x, y)); y+=dy;
e.Graphics.DrawString(ward_name.Text, fnt, Brushes.Black, new PointF(x, y)); y+=dy;
e.Graphics.DrawString(ward_class.Text, fnt, Brushes.Black, new PointF(x, y)); y+=dy;
e.Graphics.DrawString(ward_house.Text, fnt, Brushes.Black, new PointF(x, y)); y+=dy;
e.Graphics.DrawString(house_master.Text, fnt, Brushes.Black, new PointF(x, y)); y+=dy;
e.Graphics.DrawString(no_accPeople.Text, fnt, Brushes.Black, new PointF(x, y)); y+=dy;

另一种方法是组合所有文本行(使用crlf)并将一个边界矩形传递给DrawString,如下所示

StringFormat format1 = new StringFormat();
format1.Trimming = StringTrimming.EllipsisWord;

string s = uniqueNum.Text + "\r\n" + fullname.Text + "\r\n" + id_method.Text; // + ...

e.Graphics.DrawString(s, this.Font, Brushes.Black, new RectangleF(100F, 100F, 500F, 500F), format1);

修改

基于您的编辑的更新代码如下:

int x = 100, y = 100; //start position

Image newImage = Image.FromFile("logo.png");
int width = 80, height = 50;
int ix = x, iy = y; //image position
e.Graphics.DrawImage(newImage, ix, iy, width, height);

x += 0; //left align texts with logo image
y += height + 30; //some space below logo

var header = new Font("Calibri", 21, FontStyle.Bold);
int hdy = (int)header.GetHeight(e.Graphics); //30; //line height spacing

var fnt = new Font("Times new Roman", 14, FontStyle.Bold);
int dy = (int)fnt.GetHeight(e.Graphics); //20; //line height spacing

e.Graphics.DrawString("Visitor GatePass™", header, Brushes.Black, new PointF(x, y)); y += hdy;
e.Graphics.DrawString("Unique Number : " + uniqueNum.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
e.Graphics.DrawString("Full Name : " + fullname.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
e.Graphics.DrawString("Method of Identification : " + id_method.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
e.Graphics.DrawString("Ward Name : " + ward_name.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
e.Graphics.DrawString("Ward Class : " + ward_class.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
e.Graphics.DrawString("Ward House : " + ward_house.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
e.Graphics.DrawString("House Master : " + house_master.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;
e.Graphics.DrawString("Accompanying People : " + no_accPeople.Text, fnt, Brushes.Black, new PointF(x, y)); y += dy;