F Oak F Oak - 19 days ago 5
Vb.net Question

Printing with vb.net

Is there any simple way to print with vb.net?

Specifically, with the console. It seems that stuff that works with forms applications dont work with the console.


Lifted from http://visualbasic.about.com/od/usingvbnet/a/printvb2005.htm

Public Class myPrinter
   Friend TextToBePrinted As String
   Public Sub prt(ByVal text As String)
      TextToBePrinted = text
      Dim prn As New Printing.PrintDocument
      Using (prn)
         prn.PrinterSettings.PrinterName _
            = "PrinterName"
         AddHandler prn.PrintPage, _
            AddressOf Me.PrintPageHandler
         RemoveHandler prn.PrintPage, _
            AddressOf Me.PrintPageHandler
      End Using
   End Sub
   Private Sub PrintPageHandler(ByVal sender As Object, _
      ByVal args As Printing.PrintPageEventArgs)
      Dim myFont As New Font("Microsoft San Serif", 10)
         args.Graphics.DrawString(TextToBePrinted, _
            New Font(myFont, FontStyle.Regular), _
            Brushes.Black, 50, 50)
   End Sub
End Class

Called as follows:

Dim printer As New myPrinter
printer.prt "Hello World"