Printing in vb.net ( calculate printed sentence width)

Posted on 2008-11-09
Medium Priority
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

omegaomega earned 2000 total points
ID: 22918505
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

ID: 22918522
Hi randy,

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

831 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