星空网 > 软件开发 > ASP.net

C#通过调用WinApi打印PDF文档类,服务器PDF打印、IIS PDF打印

其他网站下载来的类,可以用于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         通知假脱机打印程序应向给定的打印机写指定               的数据用底层的通讯命令,实现很多命令



原标题:C#通过调用WinApi打印PDF文档类,服务器PDF打印、IIS PDF打印

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流