VB.NET: Setting Font for PrintDocument Printing

Hi There, It's me again.

Continue from this article:
https://rani-irsan.blogspot.com/2019/10/vbnet-introducing-printdocument.html

We took font from form properties. However, we actually can define font type ourselves. See image below, how to define a font type.



Lets try to declare 3 font objects. At first we focus on its font style so we can distinguish regular, bold, and italic styles.


Dim fnt14Regular As Font New Font("Arial", 14, FontStyle.Regular, GraphicsUnit.Point)
Dim fnt14Bold As Font New Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Point)
Dim fnt14Italic As Font New Font ("Arial", 14, FontStyle.Italic, GraphicsUnit.Point)

This one is how to declare combination of bold and italic.
Dim fnt14ItalicBold As Font New Font("Arial", 14, _                                        FontStyle.Italic FontStyle.Bold, GraphicsUnit.Point)

We put plus (+) sign to combine FontStyle.Italic + FontStyle.Bold. Let's have a try

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

     e.Graphics.DrawString("Beautiful Day.", fnt14Regular, Brushes.Black, 100, 100)
     e.Graphics.DrawString("Beautiful Day.", fnt14Bold, Brushes.Black, 100, 125)
     e.Graphics.DrawString("Beautiful Day.", fnt14Italic, Brushes.Black, 100, 150)
     e.Graphics.DrawString("Beautiful Day.", fnt14ItalicBold, Brushes.Black, 100, 175)

End Sub

Se how we define x,y value, we add manualy 25 point each row. 14 point is height of font character and 11 additional for space between. There is a way to generate height automatically that we will discuss later.

Read previous article to understand how e.Graphics.DrawString in PrintDocument1_PrintPage works.

UI is also similar with previous article.


Don't forget to add event button1_click to trigger printing process.

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

Complete code:

Public Class Form1

    Dim fnt14Regular As Font = New Font("Arial", 14, FontStyle.Regular, GraphicsUnit.Point)
    Dim fnt14Bold As Font New Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Point)
    Dim fnt14Italic As Font New Font ("Arial", 14, FontStyle.Italic, GraphicsUnit.Point)
    Dim fnt14ItalicBold As Font New Font("Arial", 14, FontStyle.Italic + _
                                           FontStyle.Bold, GraphicsUnit.Point)

    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

        e.Graphics.DrawString("Beautiful Day.", fnt14Regular, Brushes.Black, 100, 100)
        e.Graphics.DrawString("Beautiful Day.", fnt14Bold, Brushes.Black, 100, 125)
        e.Graphics.DrawString("Beautiful Day.", fnt14Italic, Brushes.Black, 100, 150)
        e.Graphics.DrawString("Beautiful Day.", fnt14ItalicBold, Brushes.Black, 100, 175)

    End Sub

End Class

Try to run.

Then click on Button1, so printing will executed.
I use pdf printer for printing simulation. Below is the result.



Next we'll see how font-size work. Modify code as below:

Public Class Form1

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

 Dim fnt11Times As Font = New Font("Times New Roman", 11, FontStyle.Regular, GraphicsUnit.Point)
 Dim fnt14Time As Font = New Font("Times New Roman", 14, FontStyle.Regular, GraphicsUnit.Point)

 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

     e.Graphics.DrawString("Text with Arial size 11 Font.", fnt11Arial, Brushes.Black, 100, 100)
     e.Graphics.DrawString("Text with Arial size 14 Font.", fnt14Arial, Brushes.Black, 100, 123)

     e.Graphics.DrawString("Text with Times size 11 Font.", fnt11Times, Brushes.Black, 100, 150)
     e.Graphics.DrawString("Text with Times size 14 Font.", fnt14Time, Brushes.Black, 100, 173)

 End Sub

End Class


When we run and print, result will be like this:


Any question? Please comment below.
Happy coding!!!

Post a Comment

0 Comments