VB.NET: PrintDocument Aligment

We have discussed about how to print string text inside a rectangle. In this article:
Setting Vertical (Y) Position Automatically on PrintDocument

Read also these precedence articles:
Introducing PrintDocument Component
Setting Font for PrintDocument Printing

There are 2 types of alignment, vertical and horizontal. Combining them into 9 combinations of alignment positions.
- Top Left
- Top Center
- Top Right
- Middle Left
- Middle Center
- Middle Right
- Bottom Left
- Bottom Center
- Bottom Right


We're still using this UI layout:



These are to declare alignment as a StringFormat object, for all 9 combinations.

Dim TopLeft As StringFormat = New StringFormat()
Dim TopCenter As StringFormat New StringFormat()
Dim TopRight As StringFormat New StringFormat()
Dim MidLeft As StringFormat New StringFormat()
Dim MidCenter As StringFormat New StringFormat()
Dim MidRight As StringFormat New StringFormat()
Dim BottomLeft As StringFormat New StringFormat()
Dim BottomCenter As StringFormat New StringFormat()
Dim BottomRight As StringFormat New StringFormat() 

And get it formatted on Form1_Load() event.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

  TopLeft.LineAlignment = StringAlignment.Near
  TopLeft.Alignment = StringAlignment.Near

  TopCenter.LineAlignment = StringAlignment.Near
  TopCenter.Alignment = StringAlignment.Center

  TopRight.LineAlignment = StringAlignment.Near
  TopRight.Alignment = StringAlignment.Far

  MidLeft.LineAlignment = StringAlignment.Center
  MidLeft.Alignment = StringAlignment.Near

  MidCenter.LineAlignment = StringAlignment.Center
  MidCenter.Alignment = StringAlignment.Center

  MidRight.LineAlignment = StringAlignment.Center
  MidRight.Alignment = StringAlignment.Far

  BottomLeft.LineAlignment = StringAlignment.Far
  BottomLeft.Alignment = StringAlignment.Near

  BottomCenter.LineAlignment = StringAlignment.Far
  BottomCenter.Alignment = StringAlignment.Center

  BottomRight.LineAlignment = StringAlignment.Far
  BottomRight.Alignment = StringAlignment.Far

End Sub

I'll show you how does it work, one by one. See the yellow highlighted text, those texts are parts that will be modified on each trial.

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) _
     Handles PrintDocument1.PrintPage

     Dim CurX As Integer = 50
     Dim CurY As Integer = 50
     Dim iWidth As Integer = 500

     Dim cellRect As RectangleF
     cellRect = New RectangleF()
     cellRect.Location = New Point(CurX, CurY)
     cellRect.Size = New Size(iWidth, CurY + 25)

     e.Graphics.DrawString("Top Left", Font, Brushes.Black, cellRect, TopLeft)
     e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(cellRect))

End Sub

Don't forget to trigger

Private Sub Button1_Click(sender As Object, e As EventArgs) _
    Handles Button1.Click
    PrintDocument1.Print()
End Sub

Run and print:


Result here:



Let's modify code, run, and print other alignment formats.

e.Graphics.DrawString("Top Center", Font, Brushes.Black, cellRect, TopLeft)



e.Graphics.DrawString("Top Right", Font, Brushes.Black, cellRect, TopLeft)


e.Graphics.DrawString("Middle Left", Font, Brushes.Black, cellRect, TopLeft)


e.Graphics.DrawString("Middle Center", Font, Brushes.Black, cellRect, TopLeft)


e.Graphics.DrawString("Middle Right", Font, Brushes.Black, cellRect, TopLeft)


e.Graphics.DrawString("Bottom Left", Font, Brushes.Black, cellRect, TopLeft)


e.Graphics.DrawString("Bottom Center", Font, Brushes.Black, cellRect, TopLeft)


e.Graphics.DrawString("Bottom Right", Font, Brushes.Black, cellRect, TopLeft)


Now, we'll implement format into the function that we've made before. In this article. The yellow highlighted texts are what I modified. 

Public Function PrintCellText(ByVal strValue As String, ByVal x As Integer, ByVal y As Integer, _
                            ByVal As Integer, _
                            ByVal As System.Drawing.Printing.PrintPageEventArgs, _
                            ByVal Font As FontByVal Format As StringFormatAs Integer

    Dim cellRect As RectangleF = New RectangleF()
    cellRect.Location = New Point(x, y)


    cellRect.Size = New Size(w, CInt(e.Graphics.MeasureString(strValue, Font, w,
                              StringFormat.GenericTypographic).Height))

    e.Graphics.DrawString(strValue, Font, Brushes.Black, cellRect, Format)

    Return y + cellRect.Size.Height
End Function

The vertical size (height) of the rectangle automatically adjusts to text. So in this function only horizontal alignment that is affected. PrintPage event  will be modified as below:

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) _
     Handles PrintDocument1.PrintPage

     Dim CurX As Integer = 50
     Dim CurY As Integer = 50
     Dim iWidth As Integer = 500

     CurY = PrintCellText("Align Left Text.", CurX, CurY, iWidth, e, fnt, TopLeft)
     CurY = PrintCellText("Align Center Text.", CurX, CurY, iWidth, e, fnt, TopCenter)
     CurY = PrintCellText("Align Right Text.", CurX, CurY, iWidth, e, fnt, TopRight)

End Sub

We run the program then here is the result:


The vertical alignment will be needed for a complex layout. Specially tabular report layout. We'll discuss it later. Any Question? Please comment below.
Happy coding!!



Post a Comment

0 Comments