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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aaxenAuthor Commented:
Hi randy,

Thanks alot with the code it works.
Guess Measure string is the thing that i need
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.