【问题标题】:How do i do NewLine in C# for e.Graphics.DrawString我如何在 C# 中为 e.Graphics.DrawString 执行 NewLine
【发布时间】:2018-04-06 07:55:59
【问题描述】:

我正在使用打印预览对话框,因此我想创建一个新行,以便在打印时使作业易于理解。我有一个挑战,因为我从文本框中得到它。一切似乎都被卡住了,因此我想知道我该怎么做。

代码是这样的

编辑

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

        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 ,例如徽标和其他一些格式。

【问题讨论】:

  • 它“卡住了”,因为您从 X: 100 Y: 100 开始每个字符串。您需要更改这些。
  • 哦好吧..让我试试@john

标签: c#


【解决方案1】:

你所有的职位都是一样的: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;

【讨论】:

  • 如果您不想静态确定合适的dy 值,您可以随时调用fnt.GetHeight()
  • @Damien_The_Unbeliever 是的,这只是一个快速/简短的答案。谢谢(我更新了代码)
  • @S.Serp。我非常感谢你。你成就了我的一天
  • 所以如果我想添加一个图片,在其他信息之前说一个徽标,我该怎么做?图片在资源中
  • @Hakeem 你可以使用DrawImage (x,y,width,height) 方法
猜你喜欢
  • 2014-02-04
  • 1970-01-01
  • 2010-10-08
  • 2017-03-10
  • 2013-04-23
  • 2011-03-17
  • 2013-07-12
  • 2018-12-21
  • 1970-01-01
相关资源
最近更新 更多