Printing in ( calculate printed sentence width)

Hi experts,

About the title, Let say i have a sentence with fonts and size set and I want to place the sentence in the centre of the page. How do i design the algorithms to do that?

I tried calculated page width and then half the page width but the first character of the sentence starts from the (page_width/2) position.

Dim centre As Single = Convert.ToSingle((e.MarginBounds.Width) / 2)
 Dim title_font As New Font("Impact", 45, FontStyle.Bold, GraphicsUnit.Pixel, 18)
e.Graphics.DrawString("Printed a Line on page " & ass & i.ToString, title_font, Brushes.Black, centre, 50)

Open in new window

Who is Participating?
omegaomegaConnect With a Mentor DeveloperCommented:
Hello, aaxen,

Try the example in the attached snippet.  


        Dim TextToPrint As String = "Printed a Line on page... etc. etc."
        Dim title_font As New Font("Impact", 45, FontStyle.Bold, GraphicsUnit.Pixel, 18)
        Dim WidthOfString As Single = e.Graphics.MeasureString(TextToPrint, title_font).Width
        Dim WidthAvailable As Single = e.MarginBounds.Width - WidthOfString
        Dim StartingPosition As Single = e.MarginBounds.Left + WidthAvailable / 2
        e.Graphics.DrawString(TextToPrint, title_font, Brushes.Black, StartingPosition, 50)

Open in new window

aaxenAuthor Commented:
Hi randy,

Thanks alot with the code it works.
Guess Measure string is the thing that i need
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.