2017-10-25 4 views
1

印刷プレビューダイアログを使用していますので、印刷時にジョブを理解しやすくするために新しい行を作成します。私はテキストボックスからそれを得るようにチャレンジを持っています。すべてがジャムされているようですので、私はそれについてどうすればいいのか知りたかったのです。e.Graphics.DrawStringのC#でNewLineを行う方法

コードは、この

編集

のように見えることは今、私のコードは次のようになります。いくつかのパーツながら

 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; 

は、ロゴや他のいくつかのフォーマットと同じように、詰まっています。

+0

「詰まっています」 X:100 Y:100ですべての文字列を開始するからです。これらを変更する必要があります。 – john

+0

Ohay .. lemme try @ johnを試してみてください – Hakeem

答えて

0

あなたのポジションはすべてPointF(100, 100)です。彼らはお互いの上に印刷されます。あなたはyの位置を変更する必要があります。

更新されたコードベース:ここ

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示すよう

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に外接する四角形を渡しています

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; 
+1

適切な 'dy'値を静的に決定したくない場合はいつでも' fnt.GetHeight() 'を呼び出すことができます。 –

+0

@Damien_The_Unbelieverはい、それは単なる速い/短い答えでした。ありがとう(私はコードを更新しました) –

+0

@ S.Serp。私はありがとうございます。あなたは私の一日を作りました – Hakeem