Print output adds lots of carriage returns! Why?

Mateus

Active member
Joined
Apr 16, 2010
Messages
27
Programming Experience
3-5
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!). :confused:

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:
:D
OK, so I solved the problem myself! I had screwed up my page size code (and various other parts). The following works fine...

VB.NET:
Public Class MyPrinter
    Dim TextToBePrinted As String = ""

    Public Sub prt(ByVal sText As String, ByVal sPrinter As String)
        Dim PrntDoc As New Printing.PrintDocument
        Dim PgSettings As New Printing.PageSettings
        PgSettings.PaperSize = New Printing.PaperSize("Label", 600, 400)
        TextToBePrinted = sText
        
        Using PrntDoc
            PrntDoc.PrinterSettings.PrinterName = sPrinter
            PrntDoc.DefaultPageSettings.PaperSize = PgSettings.PaperSize
            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 PrintFont As New Font("Courier New", 10)
        myPrintPage.Graphics.DrawString(TextToBePrinted, PrintFont, Brushes.Black, 0, 0)
    End Sub
End Class

:D
 
Back
Top