使用 vb.net 打印

使用 vb.net 打印

问题描述:

是否有任何简单方法可以使用 VB.NET 打印到打印机?

Is there any simple way to print to a printer with VB.NET?

特别是使用控制台.似乎适用于表单应用程序的东西不适用于控制台.

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

摘自 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
         prn.Print()
         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

调用如下:

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