Connor Albright
Connor Albright

Reputation: 733

Printing with 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.

Upvotes: 6

Views: 18970

Answers (4)

Valentin O.
Valentin O.

Reputation: 75

Okay this post is old but from my understanding of the question, you wanted to print a string to the console, in other words :

System.Console.Write("My magnificent string !")

Upvotes: 0

Oded
Oded

Reputation: 498904

Look at the PrintDocument class.

Defines a reusable object that sends output to a printer, when printing from a Windows Forms application.

Upvotes: 1

Patrick McDonald
Patrick McDonald

Reputation: 65411

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
         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

Called as follows:

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

Upvotes: 9

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181270

Easiest way I can think of is using a Printing Engine such as CrystalReports.

Upvotes: -2

Related Questions