We help IT Professionals succeed at work.
Get Started

VB.NET - Print to centre middle of page

643 Views
Last Modified: 2015-10-08
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
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE