Printing. DrawLine/DrawString. C# [WIN]

M

MikeY

Hi Everyone,

My problem is this: I'm having a hard time lining up Graphics.DrawString
and Graphics.DrawLine. In that I'm not being able to control the placement
of the lines being drawn to my printer after the header. The "y"
co-ordinates are all fine and dandy with DrawString (Text), but they get
messed up with DrawLine and not being placed exactly co-ordinates where I
want them to be drawn. Co-ordinates being passed are correct. Hopefully
someone has a keen eye and a solution to my problem. A sample code is as
follows:

{
Header_Font = new Font("Tahoma", 20);
Header_Font_2 = new Font("Tahoma", 10);
Line_Font = new Font("Tahoma", 10);
Body_Font = new Font("Tahoma", 15);
Footer_Font = new Font("Tahoma", 10);
float y = 10;
Header_Title = "HOST";
//----------TITLE---------------------------------------------
//HEADER. HEADER ALIGNMENTS
StringFormat sf =(StringFormat)StringFormat.GenericTypographic.Clone();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisWord;


//1st Placement
ev.Graphics.DrawString(Header_Title, Header_Font, Brushes.Black,new
RectangleF(ev.PageBounds.Left,y,ev.PageBounds.Right - 400,y),sf);
ev.Graphics.DrawLine(Pens.Red,ev.PageBounds.Left,y,ev.PageBounds.Right,y);

//SIZE MEASUREMENT
SizeF fs = ev.Graphics.MeasureString(Header_Title, Header_Font,
Int32.MaxValue, StringFormat.GenericTypographic);

//UPDATE PLACEMENT
y += Convert.ToInt32(fs.Height);

//2nd Placement
ev.Graphics.DrawString(Header_Title, Header_Font, Brushes.Black,new
RectangleF(ev.PageBounds.Left,y,ev.PageBounds.Right - 400,y),sf);
ev.Graphics.DrawLine(Pens.Red,ev.PageBounds.Left,y,ev.PageBounds.Right,y);

//SIZE MEASUREMENT
fs = ev.Graphics.MeasureString(Header_Title, Header_Font, Int32.MaxValue,
StringFormat.GenericTypographic);

//UPDATE PLACEMENT
y += Convert.ToInt32(fs.Height);

//3rd Placement
ev.Graphics.DrawString(Header_Title, Header_Font, Brushes.Black,new
RectangleF(ev.PageBounds.Left,y,ev.PageBounds.Right - 400,y),sf);
ev.Graphics.DrawLine(Pens.Red,ev.PageBounds.Left,y,ev.PageBounds.Right,y);
}


Thank you all in advance.
 
P

PvdG42

MikeY said:
Hi Everyone,

My problem is this: I'm having a hard time lining up Graphics.DrawString
and Graphics.DrawLine. In that I'm not being able to control the
placement of the lines being drawn to my printer after the header. The "y"
co-ordinates are all fine and dandy with DrawString (Text), but they get
messed up with DrawLine and not being placed exactly co-ordinates where I
want them to be drawn. Co-ordinates being passed are correct. Hopefully
someone has a keen eye and a solution to my problem. A sample code is as
follows:

{
Header_Font = new Font("Tahoma", 20);
Header_Font_2 = new Font("Tahoma", 10);
Line_Font = new Font("Tahoma", 10);
Body_Font = new Font("Tahoma", 15);
Footer_Font = new Font("Tahoma", 10);
float y = 10;
Header_Title = "HOST";
//----------TITLE---------------------------------------------
//HEADER. HEADER ALIGNMENTS
StringFormat sf =(StringFormat)StringFormat.GenericTypographic.Clone();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisWord;


//1st Placement
ev.Graphics.DrawString(Header_Title, Header_Font, Brushes.Black,new
RectangleF(ev.PageBounds.Left,y,ev.PageBounds.Right - 400,y),sf);
ev.Graphics.DrawLine(Pens.Red,ev.PageBounds.Left,y,ev.PageBounds.Right,y);

//SIZE MEASUREMENT
SizeF fs = ev.Graphics.MeasureString(Header_Title, Header_Font,
Int32.MaxValue, StringFormat.GenericTypographic);

//UPDATE PLACEMENT
y += Convert.ToInt32(fs.Height);

//2nd Placement
ev.Graphics.DrawString(Header_Title, Header_Font, Brushes.Black,new
RectangleF(ev.PageBounds.Left,y,ev.PageBounds.Right - 400,y),sf);
ev.Graphics.DrawLine(Pens.Red,ev.PageBounds.Left,y,ev.PageBounds.Right,y);

//SIZE MEASUREMENT
fs = ev.Graphics.MeasureString(Header_Title, Header_Font, Int32.MaxValue,
StringFormat.GenericTypographic);

//UPDATE PLACEMENT
y += Convert.ToInt32(fs.Height);

//3rd Placement
ev.Graphics.DrawString(Header_Title, Header_Font, Brushes.Black,new
RectangleF(ev.PageBounds.Left,y,ev.PageBounds.Right - 400,y),sf);
ev.Graphics.DrawLine(Pens.Red,ev.PageBounds.Left,y,ev.PageBounds.Right,y);
}


Thank you all in advance.
You say vertical (y) positioning is OK, so I'll assume the issue is
horizontal (x) positioning. Try using a fixed font vs. a proportional font
and see if that helps.
 
M

MikeY

Hi Pvd,

Thank you for your response. The "x" horizontal, is prefectly fine for
both. The "y" vertical positioning is fine for the DrawString, but gets
messed up with the DrawLine. I think that it might be that they are taking
the co-ordinances in different formats, I'm not sure. I've been trying to
find that out, and if so, how do I unify them (convert them to the same
input).

I concluded this from the line placements being alter with new "y" inputs
and everything else being exactly the same. The causes the "y" DrawString
placements to always be where the should be placed, but not so for the "y"
placements of the DrawLine inputs. It seems with the DrawLine being drawn
above the actual spot of where it should be drawn. hmmm. That is the only
thing I can think of as to where the problem is arising.

If I'm making sense.

MikeY
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top