VB.NET: Setting Vertical (Y) Position Automatically on PrintDocument

Keep going with PrintDocument things. This article is a continuation of:
Introducing PrintDocument Component
Setting Font for PrintDocument Printing

Still using similar UI, we only change the code:



I'll create a function for printing that returns the height of string value character. The height value will become a reference to set y position for the next row.

Public Function PrintCellText(ByVal strValue As String, ByVal x As Integer, ByVal y As Integer, _
                            ByVal w As Integer, _
                            ByVal e As System.Drawing.Printing.PrintPageEventArgs, _
                            Font As Font) As Integer

    'declare new rectangle
    Dim cellRect As RectangleF = New RectangleF()

    'positioning rectangle
    cellRect.Location = New Point(x, y)

    'setting size of the rectangle, automatically adjust with string character size
    '(auto fit, auto size)
    cellRect.Size = New Size(w, CInt(e.Graphics.MeasureString(strValue, Font, w, _
                              StringFormat.GenericTypographic).Height))

    'defining text for printing
    e.Graphics.DrawString(strValue, Font, Brushes.Black, cellRect)

    Return y + cellRect.Size.Height
End Function

In this PrintCellText function, we print a string inside a rectangle. This rectangle will adjust its size to the string value. Then we can get height value by rectangle size. However, we must assign width value manually.

Dim fnt As Font = New Font("Arial", 11, FontStyle.Regular, GraphicsUnit.Point)

Set the Event PrintPage 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("Text on 1st row.", CurX, CurY, iWidth, e, fnt)
     CurY = PrintCellText("Text on 2nd row.", CurX, CurY, iWidth, e, fnt)
     CurY = PrintCellText("Text on 3rd row.", CurX, CurY, iWidth, e, fnt)
     CurY = PrintCellText("Text on 4th row.", CurX, CurY, iWidth, e, fnt)

End Sub

Event button1_click to trigger printing.

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

Complete code:

Public Class Form1

Dim fnt As Font New Font("Arial", 11, FontStyle.Regular, GraphicsUnit.Point)

Public Function PrintCellText(ByVal strValue As StringByVal As IntegerByVal As Integer, _
                            ByVal w As Integer, _
                            ByVal e As System.Drawing.Printing.PrintPageEventArgs, _
                            Font As FontAs Integer

    'declare new rectangle

    Dim cellRect As RectangleF = New RectangleF()


    'positioning rectangle
    cellRect.Location = New Point(x, y)

    'setting size of rectangle, automatically adjust wirh string character size
    cellRect.Size = New Size(w, CInt(e.Graphics.MeasureString(strValue, Font, w, _
                              StringFormat.GenericTypographic).Height))

    'defining text for printing
    e.Graphics.DrawString(strValue, Font, Brushes.Black, cellRect)

    Return y + cellRect.Size.Height
End Function

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

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("Text on 1st row.", CurX, CurY, iWidth, e, fnt)
     CurY = PrintCellText("Text on 2nd row.", CurX, CurY, iWidth, e, fnt)
     CurY = PrintCellText("Text on 3rd row.", CurX, CurY, iWidth, e, fnt)
     CurY = PrintCellText("Text on 4th row.", CurX, CurY, iWidth, e, fnt)

End Sub

End Class


Let's run the code and print.




I use DoPDF as a printer, and here is the result.





The next article will discuss text alignment. If there is any question, please comment below.
Happy coding!

Post a Comment

0 Comments