UPDATE: Resolved, see post below. 
Hi All,
I'm having difficulty printing labels. I'm a newbie to printing but I feel I'm slowly getting there...
Having searched forums I've come up with the following code for printing and it works but the output add a load of carriage returns to the end (e.g. 66 carriage returns!).
I've tried adding PageSettings values but these seem to make no difference.
For info, here's some of the code from the main form...
Many thanks,
Matt
Hi All,
I'm having difficulty printing labels. I'm a newbie to printing but I feel I'm slowly getting there...
Having searched forums I've come up with the following code for printing and it works but the output add a load of carriage returns to the end (e.g. 66 carriage returns!).
I've tried adding PageSettings values but these seem to make no difference.
VB.NET:
Public Class MyPrinter
Dim TextToBePrinted As String = ""
Public Sub prt(ByVal sText As String, ByVal sPrinter As String)
Dim PgSettings As System.Drawing.Printing.PageSettings = New System.Drawing.Printing.PageSettings
Dim PrntDoc As New Printing.PrintDocument
TextToBePrinted = sText
PgSettings.PaperSize = New System.Drawing.Printing.PaperSize("Label", 200, 500)
Using (PrntDoc)
PrntDoc.DefaultPageSettings = PgSettings
PrntDoc.PrinterSettings.PrinterName = sPrinter
AddHandler PrntDoc.PrintPage, AddressOf Me.PrintPageHandler
PrntDoc.Print()
RemoveHandler PrntDoc.PrintPage, AddressOf Me.PrintPageHandler
End Using
PrntDoc.Dispose()
End Sub
Private Sub PrintPageHandler(ByVal sender As Object, ByVal myPrintPage As Printing.PrintPageEventArgs)
Dim myFont As New Font("Courier New", 10)
myPrintPage.Graphics.DrawString(TextToBePrinted, myFont, Brushes.Black, 10, 10)
End Sub
End Class
For info, here's some of the code from the main form...
VB.NET:
sPrinter = PrintDialog1.PrinterSettings.PrinterName
Print.prt(TextBox1.Text, sPrinter)
Many thanks,
Matt
Last edited: