Printing in ( calculate printed sentence width)

Posted on 2008-11-09
Last Modified: 2012-05-05
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

Question by:aaxen
    LVL 12

    Accepted Solution

    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


    Author Comment

    Hi randy,

    Thanks alot with the code it works.
    Guess Measure string is the thing that i need

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now