VB.NET - Print to centre middle of page

I have a Windows Forms Application that sends a couple of lines of text, specifically a customer name and current date + 1 month to a printer. So far I have managed to get it to print in the middle of the page but I need it to print in the middle centre. I mean not just centred page width but also centred page height.
Here's my code so far:
Friend TextToBePrinted As String

Dim print = MessageBox.Show("Would you like to print a retain slip to attach to the computer?", "Print retain slip", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If print = Windows.Forms.DialogResult.Yes Then
                    TextToBePrinted = Form1.txtRetainName.Text & vbCrLf & Date.Now.AddMonths(1)
                    Dim prn As New Printing.PrintDocument
                    Dim printer As String = "HP LaserJet P3010 Series UPD PCL 6"
                    Using (prn)
                        prn.PrinterSettings.PrinterName = printer
                        AddHandler prn.PrintPage, _
            AddressOf Me.PrintPageHandler
                        prn.Print()
                        RemoveHandler prn.PrintPage, _
                           AddressOf Me.PrintPageHandler
                    End Using
                End If

Private Sub PrintPageHandler(ByVal sender As Object, _
       ByVal args As Printing.PrintPageEventArgs)
        'Dim myFont As New Font("Arial", 22, FontStyle.Bold)
        'args.Graphics.DrawString(TextToBePrinted, _
        '   New Font(myFont, FontStyle.Regular), _
        '   Brushes.Black, 50, 50)
        Dim sngCenterPage As Single
        Static fntHeadingFont As New Font("Arial", 22, FontStyle.Bold)
        sngCenterPage = Convert.ToSingle(args.PageBounds.Width / 2 - args.Graphics.MeasureString _
                                         (Form1.txtRetainName.Text & vbCrLf & Date.Now.AddMonths(1), fntHeadingFont).Width / 2)
        '()
        args.Graphics.DrawString(Form1.txtRetainName.Text & vbCrLf & Date.Now.AddMonths(1), fntHeadingFont, Brushes.Black, sngCenterPage, 2)
    End Sub

Open in new window

This works fine as it centres it in the middle at the top of the page but I need it in the middle at the centre of the page
LVL 2
fruitloopyAsked:
Who is Participating?

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

x
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.

fruitloopyAuthor Commented:
Solved it myself. This seems to do the trick
Private Sub PrintPageHandler(ByVal sender As Object, _
       ByVal e As Printing.PrintPageEventArgs)
        Dim title_font As New Font("Arial", 22, 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 heightofstring As Single = e.Graphics.MeasureString(TextToPrint, title_font).Height
        Dim heightavailable As Single = e.MarginBounds.Height - heightofstring
        Dim StartingPosition As Single = e.MarginBounds.Left + WidthAvailable / 2
        Dim heightposition As Single = e.MarginBounds.Top + heightavailable / 2
        e.Graphics.DrawString(TextToPrint, title_font, Brushes.Black, StartingPosition, heightposition)
    End Sub

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
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.