使用.net中的打印机对象打印文本文件

时间:2014-07-21 05:08:30

标签: .net printing dos

我正在创建一个实用程序,使用dotmetrilx打印机中的打印机对象在vb.net中打印文本文件。当我们使用我们的应用程序打印任何文本文件时,花费大约10秒来打印25行。当我们使用dos命令打印相同的文件进行打印时,大约需要5秒。所以我的问题是如何提高我们的应用程序的性能。示例代码如下:

Using Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6;
Printer..FontName = "Courier New"
objPrinter.FontSize = 8;
objPrinter.Print(sb.ToString());

我们正在使用以下DOS命令打印文本。

print c:\abc.txt \d:printername

请指导我如何做到这一点。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您需要使用打印机对象..

这是一个打印类的示例,用于在标签打印机上打印简单的条形码标签。

示例代码在这里,

导入System.Drawing 命名空间打印机

Public Class PrintDocument
    Private WithEvents _PrintDialog As New System.Windows.Forms.PrintDialog
    Private _PrinterObject As New System.Printing.LocalPrintServer
    Private Regfunctions As New DataFunctions.Regfunctions.Regfunctions
    Private _PrinterName As DataFunctions.Regfunctions.Regfunctions.Regkeys
    Private WithEvents _PrintItem As Drawing.Printing.PrintDocument
    Public Event PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)

    Public Sub New(ByVal PrinterID As String)
        _PrinterName = Regfunctions.Load(DataFunctions.Regfunctions.Regfunctions.Reg_type.Current_Config, DataFunctions.Regfunctions.Regfunctions.KeyRegloc & "\Printers", PrinterID, _PrinterObject.DefaultPrintQueue.FullName)
        _PrintDialog.PrinterSettings.PrinterName = _PrinterName.KeyVal
    End Sub

    Public Function ShowDialog() As Windows.Forms.DialogResult
        Dim TmpResult As Windows.Forms.DialogResult
        TmpResult = _PrintDialog.ShowDialog()
        _PrinterName.KeyVal = _PrintDialog.PrinterSettings.PrinterName
        Regfunctions.Save(_PrinterName)
        Return TmpResult
    End Function

    Public Function Print() As Windows.Forms.DialogResult
        _PrintItem = New Printing.PrintDocument
        _PrintItem.PrinterSettings = _PrintDialog.PrinterSettings
        _PrintItem.Print()
    End Function

    Private Sub _PrintItem_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles _PrintItem.PrintPage
        RaiseEvent PrintPage(sender, e)
    End Sub

End Class

Public Class PrintTags
    Private _BarcodeNo As String
    Private _DateString As String
    Private _PartNO As String
    Private _Station As String
    Private _BarCode As Image
    Private Barcodes As New Barcodes.BarcodeModule

    Public Sub PrintTag(ByVal BarcodeNo As String, ByVal DateStr As String, ByVal PartNO As String, ByVal Station As String)
        Dim TmpRec As New Rectangle
        TmpRec.Width = 280
        TmpRec.Height = 100


        _BarcodeNo = BarcodeNo
        _DateString = DateStr
        _PartNO = PartNO
        _Station = Station
        _BarCode = Barcodes.NewBarcode(_BarcodeNo, TmpRec)

        Dim TmpPrinter As New Printers.PrintDocument("BCPrinter")
        AddHandler TmpPrinter.PrintPage, AddressOf PrintTheTag
        TmpPrinter.Print()
    End Sub

    Private Sub PrintTheTag(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Dim canvas As Graphics = e.Graphics
        Dim Font1 As New Font("Arial", 24, FontStyle.Bold, GraphicsUnit.Pixel)
        canvas.DrawString(_Station, Font1, Brushes.Black, 10, 10)
        canvas.DrawString(_BarcodeNo, Font1, Brushes.Black, 10, 50)
        canvas.DrawString(_PartNO, Font1, Brushes.Black, 10, 90)
        canvas.DrawString(_DateString, Font1, Brushes.Black, 10, 130)
        canvas.DrawImage(_BarCode, 0, 180, 280, 100)
    End Sub

End Class

结束命名空间

然后我需要打印我使用的标签..

代码在这里,

Private TagPrint As New Printers.Printers.PrintTags

    TagPrint.PrintTag(Fonumber, Date.Now.ToString, Part, StationName)