通过 VisualBasic.NET 将原始数据发送到打印机

详细信息

<script type="text/javascript">loadTOCNode(2, 'summary');</script>以将原始数据发送到打印机从 Microsoft NET Framework, 程序必须使用 Win 32 后台函数。 通过 .NETFramework, 您可以通过使用 PrintDocument 、 PrintController , 和关联类打印。 但是, 使用 .NETFramework, 无法预设格式 - 打印机就绪数据发送到打印机。

您可能需要原始数据发送到打印机进行下列:

发送转义序列。
下载, 然后再使用软字体。
后台处理预打印文件。
以发送到打印机, 这些类型和其他类型的原始数据代码必须使用 Win 32 后台程序 Application 程序员接口 (API)。 以下代码显示如何通过 WritePrinter 然后发送到打印机那些字节读入内存, 预设格式文件的内容和。

注意 : 同一打印作业作为本机 PrintDocument 打印作业中无法使用此方法。 您必须要么使用 .NETFramework 要打印或发送自己的打印作业字节。

Imports  System.IO
Imports  System.ComponentModel
Imports  System.Windows.Forms
Imports  System.Drawing
Imports  System.Drawing.Printing
Imports  System
Imports  System.Runtime.InteropServices

Public   Class RawPrinterHelper
    
' Structure and API declarions:
    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
    
Structure DOCINFOW
        
<MarshalAs(UnmanagedType.LPWStr)> Public pDocName As String
        
<MarshalAs(UnmanagedType.LPWStr)> Public pOutputFile As String
        
<MarshalAs(UnmanagedType.LPWStr)> Public pDataType As String
    
End Structure


    
<DllImport("winspool.Drv", EntryPoint:="OpenPrinterW", _
       SetLastError:
=True, CharSet:=CharSet.Unicode, _
       ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
    
Public Shared Function OpenPrinter(ByVal src As StringByRef hPrinter As IntPtr, ByVal pd As LongAs Boolean
    
End Function

    
<DllImport("winspool.Drv", EntryPoint:="ClosePrinter", _
       SetLastError:
=True, CharSet:=CharSet.Unicode, _
       ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
    
Public Shared Function ClosePrinter(ByVal hPrinter As IntPtr) As Boolean
    
End Function

    
<DllImport("winspool.Drv", EntryPoint:="StartDocPrinterW", _
       SetLastError:
=True, CharSet:=CharSet.Unicode, _
       ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
    
Public Shared Function StartDocPrinter(ByVal hPrinter As IntPtr, ByVal level As Int32, ByRef pDI As DOCINFOW) As Boolean
    
End Function

    
<DllImport("winspool.Drv", EntryPoint:="EndDocPrinter", _
       SetLastError:
=True, CharSet:=CharSet.Unicode, _
       ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
    
Public Shared Function EndDocPrinter(ByVal hPrinter As IntPtr) As Boolean
    
End Function

    
<DllImport("winspool.Drv", EntryPoint:="StartPagePrinter", _
       SetLastError:
=True, CharSet:=CharSet.Unicode, _
       ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
    
Public Shared Function StartPagePrinter(ByVal hPrinter As IntPtr) As Boolean
    
End Function

    
<DllImport("winspool.Drv", EntryPoint:="EndPagePrinter", _
       SetLastError:
=True, CharSet:=CharSet.Unicode, _
       ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
    
Public Shared Function EndPagePrinter(ByVal hPrinter As IntPtr) As Boolean
    
End Function

    
<DllImport("winspool.Drv", EntryPoint:="WritePrinter", _
       SetLastError:
=True, CharSet:=CharSet.Unicode, _
       ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
    
Public Shared Function WritePrinter(ByVal hPrinter As IntPtr, ByVal pBytes As IntPtr, ByVal dwCount As Int32, ByRef dwWritten As Int32) As Boolean
    
End Function

    
<DllImport("kernel32.dll", EntryPoint:="GetLastError", _
       SetLastError:
=True, CharSet:=CharSet.Unicode, _
       ExactSpelling:
=True, CallingConvention:=CallingConvention.StdCall)> _
    
Public Shared Function GetLastError() As Int32
    
End Function


    
' SendBytesToPrinter()
    ' When the function is given a printer name and an unmanaged array of  
    ' bytes, the function sends those bytes to the print queue.
    ' Returns True on success or False on failure.
    Public Shared Function SendBytesToPrinter(ByVal szPrinterName As StringByVal pBytes As IntPtr, ByVal dwCount As Int32) As Boolean
        
Dim hPrinter As IntPtr      ' The printer handle.
        Dim dwError As Int32        ' Last error - in case there was trouble.
        Dim di As DOCINFOW          ' Describes your document (name, port, data type).
        Dim dwWritten As Int32      ' The number of bytes written by WritePrinter().
        Dim bSuccess As Boolean     ' Your success code.

        
' Set up the DOCINFO structure.
        With di
            .pDocName 
= "My Visual Basic .NET RAW Document"
            .pDataType 
= "RAW"
        
End With
        
' Assume failure unless you specifically succeed.
        bSuccess = False
        
If OpenPrinter(szPrinterName, hPrinter, 0Then
            
If StartDocPrinter(hPrinter, 1, di) Then
                
If StartPagePrinter(hPrinter) Then
                    
' Write your printer-specific bytes to the printer.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, dwWritten)
                    EndPagePrinter(hPrinter)
                
End If
                EndDocPrinter(hPrinter)
            
End If
            ClosePrinter(hPrinter)
        
End If
        
' If you did not succeed, GetLastError may give more information
        ' about why not.
        If bSuccess = False Then
            dwError 
= GetLastError()
        
End If
        
Return bSuccess
    
End Function
 ' SendBytesToPrinter()

    
' SendFileToPrinter()
    ' When the function is given a file name and a printer name, 
    ' the function reads the contents of the file and sends the
    ' contents to the printer.
    ' Presumes that the file contains printer-ready data.
    ' Shows how to use the SendBytesToPrinter function.
    ' Returns True on success or False on failure.
    Public Shared Function SendFileToPrinter(ByVal szPrinterName As StringByVal szFileName As StringAs Boolean
        
' Open the file.
        Dim fs As New FileStream(szFileName, FileMode.Open)
        
' Create a BinaryReader on the file.
        Dim br As New BinaryReader(fs)
        
' Dim an array of bytes large enough to hold the file's contents.
        Dim bytes(fs.Length) As Byte
        
Dim bSuccess As Boolean
        
' Your unmanaged pointer
        Dim pUnmanagedBytes As IntPtr

        
' Read the contents of the file into the array.
        bytes = br.ReadBytes(fs.Length)
        
' Allocate some unmanaged memory for those bytes.
        pUnmanagedBytes = Marshal.AllocCoTaskMem(fs.Length)
        
' Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, fs.Length)
        
' Send the unmanaged bytes to the printer.
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, fs.Length)
        
' Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(pUnmanagedBytes)
        
Return bSuccess
    
End Function
 ' SendFileToPrinter()

    
' When the function is given a string and a printer name,
    ' the function sends the string to the printer as raw bytes.
    Public Shared Function SendStringToPrinter(ByVal szPrinterName As StringByVal szString As String)
        
Dim pBytes As IntPtr
        
Dim dwCount As Int32
        
' How many characters are in the string?
        dwCount = szString.Length()
        
' Assume that the printer is expecting ANSI text, and then convert
        ' the string to ANSI text.
        pBytes = Marshal.StringToCoTaskMemAnsi(szString)
        
' Send the converted ANSI string to the printer.
        SendBytesToPrinter(szPrinterName, pBytes, dwCount)
        Marshal.FreeCoTaskMem(pBytes)
    
End Function

End Class
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值