简体   繁体   English

我如何在 C# 中为 e.Graphics.DrawString 做 NewLine

[英]How do i do NewLine in C# for e.Graphics.DrawString

I am using a print preview Dialog, so i want to make a new Line so as to make the job understandable when i am printing.我正在使用打印预览对话框,所以我想创建一个新行,以便在打印时使工作易于理解。 I have a Challenge as i get it from the Textbox.我有一个挑战,因为我从文本框中得到它。 Everything seems to be Jammed, Hence i wanted to know how i can go about it.一切似乎都被卡住了,因此我想知道我该怎么做。

Code looks like this代码看起来像这样

Edits编辑

Now My code looks like this now :现在我的代码看起来像这样:

        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;

Whilst some parts are Jammed , Like with the Logo and some other formatting.虽然有些部分被卡住了,就像徽标和其他一些格式一样。

All your positions are the same: PointF(100, 100) .你所有的位置都是一样的: PointF(100, 100) So they will be printed over each other.因此,它们将相互打印。 You need to change the y position.您需要更改 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;

Another method, is combining all your texts lines (using crlf) and passing a bounding rectangle to the DrawString as shown here另一种方法是组合所有文本行(使用 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);

EDIT:编辑:

The updated code based on your edit will be as following:根据您的编辑更新后的代码如下:

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;

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

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