Centering Text when Printing 
Author Message
 Centering Text when Printing

Im trying to center some text in the printout.  Assuming that this text is
inside a string variabe and it can be anything when you go to check out the
print preview how could you make sure that its always centered?  Ive tried
couting how many letters there are in the string and then multipying by the
font size but that only works if the font uses the exact same size for all
the characters which most fonts do not.  Can anyone help me with this
problem?


Mon, 31 Jan 2005 19:37:06 GMT  
 Centering Text when Printing
Marco,
    You can use Graphics.MeasureString to check the length of a string
during printing and then adjust the starting location accordingly as in:
SizeF sz = g.MeasureString(s, f, new PointF(x1, y1),  m_Fmt);
Where g is the devrived graphics, s the string, f the Font, x1/y1 the
starting location, and m_Fmt is StringFormat.GenericTypographic.
    This will return the bounding box for the string and you can retrieve
the Width.  This version won't include measurement of trailing spaces and
you should do the DrawString using the same StringFormat.  If you use the
default StringFormat by not specifying it the box is widened in both width
and height by IIRC 1/6em.  If you need to measure trailing spaces as well
search the framework.windowsforms group for MeasureString as someone has
managed to derive a custom StringFormat that will do this.

Ron Allen

Quote:
> Im trying to center some text in the printout.  Assuming that this text is
> inside a string variabe and it can be anything when you go to check out
the
> print preview how could you make sure that its always centered?  Ive tried
> couting how many letters there are in the string and then multipying by
the
> font size but that only works if the font uses the exact same size for all
> the characters which most fonts do not.  Can anyone help me with this
> problem?



Mon, 31 Jan 2005 21:30:20 GMT  
 Centering Text when Printing
That worked.  Thank you.


Quote:
> Marco,
>     You can use Graphics.MeasureString to check the length of a string
> during printing and then adjust the starting location accordingly as in:
> SizeF sz = g.MeasureString(s, f, new PointF(x1, y1),  m_Fmt);
> Where g is the devrived graphics, s the string, f the Font, x1/y1 the
> starting location, and m_Fmt is StringFormat.GenericTypographic.
>     This will return the bounding box for the string and you can retrieve
> the Width.  This version won't include measurement of trailing spaces and
> you should do the DrawString using the same StringFormat.  If you use the
> default StringFormat by not specifying it the box is widened in both width
> and height by IIRC 1/6em.  If you need to measure trailing spaces as well
> search the framework.windowsforms group for MeasureString as someone has
> managed to derive a custom StringFormat that will do this.

> Ron Allen


> > Im trying to center some text in the printout.  Assuming that this text
is
> > inside a string variabe and it can be anything when you go to check out
> the
> > print preview how could you make sure that its always centered?  Ive
tried
> > couting how many letters there are in the string and then multipying by
> the
> > font size but that only works if the font uses the exact same size for
all
> > the characters which most fonts do not.  Can anyone help me with this
> > problem?



Mon, 31 Jan 2005 21:58:29 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. PRINTER.PRINT CENTER? OR LABEL PRINT

2. PRINTER.PRINT CENTER? OR LABEL PRINT

3. How to get centered text on a none multi line text box (VB4)

4. Vertically center for text in text fields

5. How to get centered text on a none multi line text box (VB4)

6. Center text vertically in static text field

7. Printing Label on the Center of a Envelop

8. Printing Label at the center of page

9. Centering a form doesn't center the form

10. center printing

11. How do you center the text in the footer from vb6

12. Text Centering in Table - VBA

 

 
Powered by phpBB® Forum Software