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.
21.3k 23 23 gold badges 97 97 silver badges 112 112 bronze badges asked Nov 12, 2010 at 19:00 Connor Albright Connor Albright 723 4 4 gold badges 13 13 silver badges 30 30 bronze badges What are you trying to print? Text in a TextArea? The Window itself? Commented Nov 12, 2010 at 19:01 I want to print a string of text a user enters in the console. Commented Nov 12, 2010 at 19:02Public 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"
answered Nov 12, 2010 at 19:04
Patrick McDonald Patrick McDonald
65.1k 14 14 gold badges 111 111 silver badges 125 125 bronze badges
Did you actually try that? "Printing" is not defined for me.
Commented Nov 12, 2010 at 19:06
That is because I am using a console application instead of a forms application. Thanks to Oded parts of the problems went away when I referenced system.drawing. And then changed "Printing." to "Drawing.Printing."
Commented Nov 12, 2010 at 19:20Its Monday, and it now works. an Import Printing.Drawing statement is needed(as well as referencing system.drawing). A small help is to use rinting.PrinterSettings.InstalledPrinters.Item(0) to find the printer you want instead of typing in something in "PrinterName" .