其他網站下載來的類,可以用於Winform、Asp.Net,用於服務器端PDF或其他文件打印。
直接上代碼:
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.IO; namespace CyServer.WinAPI { public class RawPrinterHelper { // Structure and API declarions: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public class DOCINFOA { [MarshalAs(UnmanagedType.LPStr)] public string pDocName; [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile; [MarshalAs(UnmanagedType.LPStr)] public string pDataType; } [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd); [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool ClosePrinter(IntPtr hPrinter); [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di); [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool EndDocPrinter(IntPtr hPrinter); [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool StartPagePrinter(IntPtr hPrinter); [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool EndPagePrinter(IntPtr hPrinter); [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten); // 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, false on failure. public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount) { Int32 dwError = 0, dwWritten = 0; IntPtr hPrinter = new IntPtr(0); DOCINFOA di = new DOCINFOA(); bool bSuccess = false; // Assume failure unless you specifically succeed. di.pDocName = "My C#.NET RAW Document"; di.pDataType = "RAW"; // Open the printer. if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) { // Start a document. if (StartDocPrinter(hPrinter, 1, di)) { // Start a page. if (StartPagePrinter(hPrinter)) { // Write your bytes. bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten); EndPagePrinter(hPrinter); } EndDocPrinter(hPrinter); } ClosePrinter(hPrinter); } // If you did not succeed, GetLastError may give more information // about why not. if (bSuccess == false) { dwError = Marshal.GetLastWin32Error(); } return bSuccess; } public static bool SendFileToPrinter(string szPrinterName, string szFileName) { // Open the file. FileStream fs = new FileStream(szFileName, FileMode.Open); // Create a BinaryReader on the file. BinaryReader br = new BinaryReader(fs); // Dim an array of bytes big enough to hold the file's contents. Byte[] bytes = new Byte[fs.Length]; bool bSuccess = false; // Your unmanaged pointer. IntPtr pUnmanagedBytes = new IntPtr(0); int nLength; nLength = Convert.ToInt32(fs.Length); // Read the contents of the file into the array. bytes = br.ReadBytes(nLength); // Allocate some unmanaged memory for those bytes. pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength); // Copy the managed byte array into the unmanaged array. Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength); // Send the unmanaged bytes to the printer. bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength); // Free the unmanaged memory that you allocated earlier. Marshal.FreeCoTaskMem(pUnmanagedBytes); return bSuccess; } public static bool SendStringToPrinter(string szPrinterName, string szString) { IntPtr pBytes; Int32 dwCount; // 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); return true; } } }
下面再Copy一點介紹:
打印管理庫函數Winspool.drv
打印機底層驅動的包
函數名稱 說明
AbortPrinter 刪除打印機的假脫機文件
AddForm 向可被選擇用於給定打印機的格式表中添加一
格式
AddJob 返回一個可用來存儲打印工作的文件的完整路
徑和文件名
AddMonitor 安裝一個本機打印機監視器,並連接配置文件
、數據文件和監視器文件
AddPort 向支持的端口列表中添加一端口名
AddPrinter 向指定的服務器所支持的打印機列表中添加一
打印機
AddPrinterConnection 為當前用戶添加指定的打印機並連接
AddPrinterDriver 安裝一本地或遠程打印機並連接培植文件、數
據文件和驅動文件
AddPrintProcessor 在指定的服務器上安裝一打印處理程序,並將它
的名稱添加到所支持的內部列表中
AddPrintProvidor 安裝一本地打印機提供程序,並連接配置文件、
數據文件和提供程序文件
AdvancedDocumentProperties 為給定的打印機顯示一個打印機高級配置對話
框,以允許進行配置
ClosePrinter 關閉給定的打印機對象
ConfigurePort 顯示指定服務器上給定端口的配置對話框,以
允許進行配置
ConnectToPrinterDlg 顯示一對話框供用戶在網絡瀏覽並連接打印機
DeleteForm 從所支持的格式表中刪除一個格式名
DeleteMonitor 刪除一個由AddMonitor函數所添加的打印機監
視器
DeleteProt 顯示一對話框,以允許用戶刪除一個端口名
DeletePrinter 刪除指定的打印機對象
DeletePrinterConnection 刪除指定的打印機連接
DeletePrinterDriver 從給定服務器所支持的驅動器名稱表中刪除指
定的打印機驅動器
DeletePrintProcessor 刪除由AddPrintProcessor函數所添加的打印機
處理程序
DeletePrintProvidor 刪除由AddPrintProvidor函數所添加的提供器
DeviceCapabilities 獲取指定的打印機所需求的性能
DocumentProperties 為給定的打印機顯示一個打印機配置對話框,
以允許進行配置
EndDocPrinter 終止給定打印機的一個打印作業
EndPagePrinter 指示一頁的結束和下一頁的開始
EnumForms 枚舉指定打印機所支持的格式
EnumJobs 用描述打印機的打印作業數據初始化一個
JOB_INFO_1或JOB_INFO_2結構數組,以便枚舉打
印機作業
EnumMonitors 用描述給定服務器的監視器數據初始化一個
MONITOR_INFO_1結構數組,以便枚舉打印機監視
器
EnumPorts 枚舉可用於在指定服務器上進行打印的端口
EnumPrinterDrivers 枚舉在給定打印機服務器上安裝的所有打印機
驅動程序
EnumPrinters 枚舉指定服務器上可用的打印機
EnumPrintProcessorDatatypes 枚舉指定打印機處理程序所支持的數據類型
EnumPrintProcessors 枚舉在指定服務器上安裝的打印處理器
FindClosePrinterChangeNotification關閉通過調用
FindFirstPrinterChangeNotification函數建
立的改變通知對象
FindFirstPrinterChangeNotification創建一個改變通知對象並返回句柄,使用該
句柄在調用一個等待函數期間,檢查打印機或打
印服務器的變化
FindNextPrinterChangeNotification為與指定打印機或打印服務器相關的改變通
知對象檢取最近的改變通知通知信息,也可用來
重新設置該改變通知對象為不發信息狀態
FreePrinterNotifyInfo 釋放系統分配的由
FindNextPrinterChangeNotification函數返回
的緩沖區
GetForm 利用描述給定打印機指定格式的數據初始化一
個FORM_INFO_1結構
GetJob 檢取指定打印機的打印作業數據
GetPrinter 檢取給定打印機的數據
GetPrinterData 檢取給定打印機的配置數據
GetPrinterDriver 檢取給定打印機的驅動程序數據
GetPrinterDriverDirectory 檢取給定打印機驅動程序的目錄路徑
GetPrintProcessDirectory 檢取指定服務器上打印機處理程序的路徑
OpenPrinter 檢取一個標識特定打印機或打印服務器的句柄
並打開
PrinterMessageBox 顯示一個消息框,供一個正打印的應用程序通知
用戶發生一個打印作業錯誤
PrinterProperties 為給定打印機顯示一個打印機性能對話框,以允
許進行設置
ReadPrinter 從給定打印機檢取數據
ResetPrinter 讓應用程序設定有StartDocPrinter函數提交的
打印文檔的數據類型和設備模式值
ScheduleJob 通知假脫機打印程序,可為指定的作業安排打印
SetForm 為給定的打印機設置格式信息
SetJob 暫停,恢復,取消或重新啟動給定打印機上指定
的打印作業
SetPrinter 用暫停,恢復,或清除所有打印作業來設置指定
的打印機
SetPrinterData 設置一台打印機的配置數據
StartDocPrinter 通知假脫機打印程序將在假脫機上打印一個文
檔
StartPagePrinter 通知假脫機打印程序將在給定打印機上打印一頁
WritePrinter 通知假脫機打印程序應向給定的打印機寫指定
的數據
用底層的通訊命令,實現很多命令