1

I have my printer set to MM. I am trying to work out how to centre text when printing using the following code:

Dim CentrePage As Single
CentrePage = Convert.ToString((e.PageBounds.Width / 2) - (e.Graphics.MeasureString("SENpic Report - " & Format(Now, "dddd, dd MMMM, yyyy") & " - Page " & prnPage, f).Width / 2))
e.Graphics.DrawString("SENpic Report - " & Format(Now, "dddd, dd MMMM, yyyy") & " - Page " & prnPage, f, br, CentrePage, 17)

Now I know it's a matter of units and I think one is in MM but I am not sure what the other unit is, so I can not work out a conversion factor for it.

Any Ideas?

Graham Jones
  • 165
  • 4
  • 19

4 Answers4

3

The default Graphics.PageUnit you get in your PrintPage event handler is GraphicsUnit.Display. Which maps 100 pixels to an inch. It is a convenience setting, it makes objects you draw on paper about the same size as you when you draw them to the screen. Video adapters commonly run in 96 dots-per-inch resolution. Helps you to write graphics code that works both for the printer and the screen.

You can simply reassign it. You of course are looking for GraphicsUnit.Millimeter.

Do keep in mind that this has no effect whatsoever on centering text. Which of course works just as well when you measure in millimeters as in 0.01 inches.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

After a lot of fiddling I've done it:

Basically I set the Graphics Unit to Millimetres. Even though this has been set the page bounds still return in pixels, BUT the Measure string returns Millimetres.

After a lot of trial and error I found out that to convert the Page Bounds to millimetres you have to times it by 0.264583333, this is the size of a pixel in MM.

Here is the code:

e.Graphics.PageUnit = GraphicsUnit.Millimeter
Dim f As Font = New Font("Vanada", 12)
Dim br As SolidBrush = New SolidBrush(Color.Black)
Dim CentrePage As Single
Dim CentreText As String = "My Report - " & Format(Now, "dddd, dd MMMM, yyyy") & " - Page " & prnPage
CentrePage = Convert.ToString(((e.PageBounds.Width * 0.264583333) / 2) - (e.Graphics.MeasureString(CentreText, f).Width / 2))
e.Graphics.DrawString(CentreText, f, br, Int(CentrePage), 17)

I hope this helps others.

Graham Jones
  • 165
  • 4
  • 19
0

e.Graphics.PageUnit = GraphicsUnit.Millimeter e.Graphics.DrawString(TextBox1.Text, New Font("ARIAL", 12, FontStyle.Bold Or FontStyle.Italic), Brushes.Red, 105, 30, YAZI_ORTA)

  • 3
    Answer's are great. But for best practices, please provide an explanation. You only posting code makes the OP and future commers copy and paste your answer without understanding the logic behind the answer. Please provide an answer with some explanation. Thank You! – Buddy Bob May 12 '21 at 02:35
0
    Public Class Form1

 Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
  e.Graphics.PageUnit = GraphicsUnit.Millimeter

  Dim TEXT_RIGHT As New StringFormat
  TEXT_RIGHT.LineAlignment = StringAlignment.Center
  TEXT_RIGHT.Alignment = StringAlignment.Far

  Dim TEXT_LEFT As New StringFormat
  TEXT_LEFT.LineAlignment = StringAlignment.Center
  TEXT_LEFT.Alignment = StringAlignment.Near

  Dim TEXT_CENTER As New StringFormat
  TEXT_CENTER.LineAlignment = StringAlignment.Center
  TEXT_CENTER.Alignment = StringAlignment.Center


  Dim FONT1 As Font = New Font("ARIAL", 16, FontStyle.Bold Or FontStyle.Italic)

  Dim P1 As Point = New Point(100, 20)
  e.Graphics.DrawLine(Pens.Blue, P1, New Point(P1.X, P1.Y + 150))

  e.Graphics.DrawString("THIS IS CENTERED TEXT", FONT1, Brushes.Red, _
                        P1, TEXT_CENTER)

  P1 = New Point(P1.X, P1.Y + 10)
  e.Graphics.DrawString("THIS IS RIGHT TEXT", FONT1, Brushes.Red, _
                        P1, TEXT_RIGHT)

  P1 = New Point(P1.X, P1.Y + 10)
  e.Graphics.DrawString("THIS IS RIGHT LEFT", FONT1, Brushes.Red, _
                          P1, TEXT_LEFT)

 End Sub

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  PrintPreviewControl1.Width = Me.Width - PrintPreviewControl1.Left - 50
  PrintPreviewControl1.Height = Me.Height - PrintPreviewControl1.Top - 50
  PrintPreviewControl1.Document = PrintDocument1
 End Sub
End Class