3

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.

Hakeem
  • 55
  • 1
  • 5

1 Answers1

1

All your positions are the same: PointF(100, 100). So they will be printed over each other. You need to change the y position.

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

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;
S.Serpooshan
  • 7,608
  • 4
  • 33
  • 61
  • 2
    You can always call `fnt.GetHeight()` if you don't want to statically determine the appropriate `dy` value. – Damien_The_Unbeliever Oct 25 '17 at 09:38
  • @Damien_The_Unbeliever yes, it was just a fast/short answer. thanks (i updated the code) – S.Serpooshan Oct 25 '17 at 09:39
  • @S.Serp. I Thank you so Much. You Made my day – Hakeem Oct 25 '17 at 09:44
  • so if i want to add an Image, say a Logo before this other information, How do i go about it? The Image is in Resource – Hakeem Oct 25 '17 at 09:54
  • @Hakeem you can use [DrawImage (x,y,width,height)](https://msdn.microsoft.com/en-us/library/dbsak4dc(v=vs.110).aspx) method – S.Serpooshan Oct 25 '17 at 09:57
  • @S.Serp, Okay, this case the Image is in my resource. I wanted to do something like Image newimage = Resource.ResourceManager..... Only it does not work. – Hakeem Oct 25 '17 at 10:02
  • @Hakeem use `Image myImage = Resources.ResourceManager.GetObject("myImage") as Image`. you can see [this](https://stackoverflow.com/a/1192072/2803565) and [that](https://stackoverflow.com/a/13592336/2803565) answers – S.Serpooshan Oct 25 '17 at 10:12
  • I have made edits to my code above for your viewing. Please correct me if i am wrong anywhere i am . – Hakeem Oct 25 '17 at 10:16
  • @Hakeem you have used overlaped x,y for image and first string. try to draw them on paper and choose your desired layout before coding. for example the image must be at left or top of texts? use proper position for them... – S.Serpooshan Oct 25 '17 at 10:32
  • @S.Serp yes, like i said before i am quite new to this, i want the Logo to be at the top left, then the logo then the header text, then the other information. I have updated the source once again for your viewing. Kindly take a look. – Hakeem Oct 25 '17 at 10:41
  • @Hakeem logo with 80x50 should be at top left of the page (ok). then header shall be at its right or below that logo? – S.Serpooshan Oct 25 '17 at 10:50
  • Below The Logo @S.Serp. – Hakeem Oct 25 '17 at 10:54
  • @S.Serp, Please have a Look at this Line int hdy = header.GetHeight(e.Graphics); and this Line also int dy = fnt.GetHeight(e.Graphics); triggers an error like this **Cannot implicitly convert type Float to Int** just thought i'd show you. – Hakeem Oct 25 '17 at 11:02
  • @Hakeem yes simply add `(int)` to convert float result of GetHeight to int: `int hdy = (int)header.GetHeight...` I updated the code. – S.Serpooshan Oct 25 '17 at 11:07
  • You seem very Good with graphics, Many thanks and May God Bless you. Thanks for your help @S.Serp – Hakeem Oct 25 '17 at 11:12