Question

This is a code for a small billing software for which the printing has to be done in a thermal printer. The following is my code which works fine for laser printers. I want to know whether this code is fine for thermal printers too or should i ve to change the code specifically for those kind of printers. If so kindly help me with some code. Thnx in advance :)

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    mRow = 0
    Try
        Dim big, small As Font
        big = New Font(Font.FontFamily, 20, FontStyle.Bold)
        small = New Font(Font.FontFamily, 10, FontStyle.Bold)
        newpage = True
        With DGVView
            Dim fmt As StringFormat = New StringFormat(StringFormatFlags.FitBlackBox)
            fmt.LineAlignment = StringAlignment.Near
            fmt.Trimming = StringTrimming.None
            Dim y As Single = e.MarginBounds.Top
            Do While mRow < .RowCount
                Dim row As DataGridViewRow = .Rows(mRow)
                Dim x As Single = e.MarginBounds.Left
                Dim h As Single = 0
                For Each cell As DataGridViewCell In row.Cells
                    Dim rc As RectangleF = New RectangleF(x, y, cell.Size.Width, cell.Size.Height)
                    e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
                    If (newpage = True) Then
                        PrintDocument1.PrinterSettings.DefaultPageSettings.PaperSize = New PaperSize("custom format", 300, 300)
                        'e.Graphics.DrawString("                AYYA NADAR JANAKI AMMAL COLLEGE", big, Brushes.Black, 5, 50)
                        e.Graphics.DrawString(DGVView.Columns(cell.ColumnIndex).HeaderText, small, Brushes.Black, rc, fmt)
                    Else
                        e.Graphics.DrawString(DGVView.Rows(cell.RowIndex - 1).Cells(cell.ColumnIndex).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
                    End If
                    x += rc.Width
                    h = Math.Max(h, rc.Height)
                Next
                newpage = False
                y += h
                mRow += 1
                If y + h > e.MarginBounds.Bottom Then
                    e.HasMorePages = True
                    mRow -= 1
                    newpage = True
                    Exit Sub
                End If
            Loop

            mRow = 0
        End With
    Catch
        MsgBox("Unexpected Error Occured. Sorry for the inconvenience")
    End Try
Was it helpful?

Solution

The Graphics methods are device independant (The graphics class "Encapsulates a GDI+ drawing surface") - when using them with a printer they rely on the printer device driver, so no you won't have to change you code.

Disclaimer: you sometimes find that different devices behave slightly differently so the easiest way is just to try it!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top