问题
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.
回答1:
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"
回答2:
Look at the PrintDocument class.
Defines a reusable object that sends output to a printer, when printing from a Windows Forms application.
回答3:
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 !")
回答4:
Easiest way I can think of is using a Printing Engine such as CrystalReports.
来源:https://stackoverflow.com/questions/4168117/printing-with-vb-net