从datagridview使用vb.net打印到热敏打印机

时间:2013-03-26 17:42:11

标签: vb.net printing thermal-printer

这是一个小型计费软件的代码,必须在热敏打印机中进行打印。以下是我的代码,适用于激光打印机。我想知道这个代码是否也适用于热敏打印机,或者我是否应该专门为这类打印机更改代码。如果有这么好帮助我一些代码。 Thnx提前:))

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

1 个答案:

答案 0 :(得分:0)

图形方法与设备无关(graphics class “封装GDI +绘图表面”) - 当它们与打印机一起使用时,它们依赖于打印机设备驱动程序,所以没有你赢了不得不改变你的代码。

免责声明:您有时会发现不同设备的行为略有不同,所以最简单的方法就是试用它!