使用C#获得指定打印机所支持的所有PaperSize及PaperName

获得的PaperSize可以用在CrystalReport中设置PrintOptions.PaperSize的值,从而达到可使用自定义纸张的目的。


using System;

using System.Security;

using System.Drawing.Printing;

using System.Runtime.InteropServices;

 

namespace PaperSizeTest

{

       
class PaperSizeTest

       {

              [StructLayout(LayoutKind.Sequential, CharSet
=CharSet.Auto)] 

              
internal struct PRINTER_INFO_5 

              {

                     [MarshalAs(UnmanagedType.LPTStr)] 
public String PrinterName;

                     [MarshalAs(UnmanagedType.LPTStr)] 
public String PortName;

                     [MarshalAs(UnmanagedType.U4)] 
public Int32 Attributes;

                     [MarshalAs(UnmanagedType.U4)] 
public Int32 DeviceNotSelectedTimeout;

                     [MarshalAs(UnmanagedType.U4)] 
public Int32 TransmissionRetryTimeout;

              }

 

              
const int PRINTER_ENUM_LOCAL = 2;

              
const int PRINTER_ENUM_CONNECTIONS = 4;

              
const int DC_PAPERNAMES = 16;

              
const int DC_PAPERS = 2;

              
const int DC_PAPERSIZE = 3;

 

              [DllImport(
"winspool.drv", EntryPoint="DeviceCapabilitiesA", SetLastError=true)]

              
static extern Int32 DeviceCapabilities(

                     [MarshalAs(UnmanagedType.LPStr)] String device,

                     [MarshalAs(UnmanagedType.LPStr)] String port,

                     Int16 capability,

                     IntPtr outputBuffer,

                     IntPtr deviceMode);

 

              [DllImport(
"winspool.drv", SetLastError=true)]

              
static extern bool EnumPrintersW(Int32 flags,

                     [MarshalAs(UnmanagedType.LPTStr)] 
string printerName,

                     Int32 level, IntPtr buffer, Int32 bufferSize, 
out Int32

                     requiredBufferSize, 
out Int32 numPrintersReturned);

 

              [DllImport(
"kernel32.dll", EntryPoint="GetLastError", SetLastError=false,

                      ExactSpelling
=true, CallingConvention=CallingConvention.StdCall),

              SuppressUnmanagedCodeSecurityAttribute()]

              
internal static extern Int32 GetLastError();

 

              
public static void GetDefinedPapers(string printerName)

              {

                     PRINTER_INFO_5 info5;

                     
int requiredSize;

                     
int numPrinters;

                     
bool foundPrinter = EnumPrintersW(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, 

                            String.Empty, 
5, IntPtr.Zero, 0out requiredSize, out numPrinters);

 

                     
int info5Size = requiredSize;

                     IntPtr info5Ptr 
= Marshal.AllocHGlobal(info5Size);

                     IntPtr buffer 
= IntPtr.Zero;

                     
try

                     {

                            foundPrinter 
= EnumPrintersW(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, 

                                   String.Empty, 
5, info5Ptr, info5Size, out requiredSize, out numPrinters);

 

                            
string port = null;

                            
for (int i = 0; i < numPrinters; i++)

                            {

                                   info5 
= (PRINTER_INFO_5)Marshal.PtrToStructure(

                                          (IntPtr)((i 
* Marshal.SizeOf(typeof(PRINTER_INFO_5))) + (int)info5Ptr),

                                          
typeof(PRINTER_INFO_5));

                                   
if (info5.PrinterName == printerName)

                                   {

                                          port 
= info5.PortName;

                                   }

                            }

 

                            
int numNames = DeviceCapabilities(printerName, port, DC_PAPERNAMES, IntPtr.Zero, IntPtr.Zero);

                            
if (numNames < 0)

                            {

                                   
int errorCode = GetLastError();

                                   Console.WriteLine(
"Number of names = {1}: {0}", errorCode, numNames);

                                   
return;

                            }

 

                            buffer 
= Marshal.AllocHGlobal(numNames * 64);

                            numNames 
= DeviceCapabilities(printerName, port, DC_PAPERNAMES, buffer, IntPtr.Zero);

                            
if (numNames < 0)

                            {

                                   
int errorCode = GetLastError();

                                   Console.WriteLine(
"Number of names = {1}: {0}", errorCode, numNames);

                                   
return;

                            }

                            
string[] names = new string[numNames];

                            
for (int i = 0; i < numNames; i++)

                            {

                                   names[i] 
= Marshal.PtrToStringAnsi((IntPtr)((i * 64+ (int)buffer));

                            }

                            Marshal.FreeHGlobal(buffer);

                            buffer 
= IntPtr.Zero;

 

                            
int numPapers = DeviceCapabilities(printerName, port, DC_PAPERS, IntPtr.Zero, IntPtr.Zero);

                            
if (numPapers < 0)

                            {

                                   Console.WriteLine(
"No papers");

                                   
return;

                            }

 

                            buffer 
= Marshal.AllocHGlobal(numPapers * 2);

                            numPapers 
= DeviceCapabilities(printerName, port, DC_PAPERS, buffer, IntPtr.Zero);

                            
if (numPapers < 0)

                            {

                                   Console.WriteLine(
"No papers");

                                   
return;

                            }

                            
short[] kinds = new short[numPapers];

                            
for (int i = 0; i < numPapers; i++)

                            {

                                   kinds[i] 
= Marshal.ReadInt16(buffer, i * 2);

                            }

 

                            
for(int i = 0; i < numPapers; i++)

                            {

                                   Console.WriteLine(
"Paper {0} : {1}", kinds[i], names[i]);

                            }

                     }

                     
finally

                     {

                            Marshal.FreeHGlobal(info5Ptr);

                     }

              }

 

              
public static void Main()

              {

                     PrintDocument pd 
= new PrintDocument();

                     
string sPrinterName = pd.PrinterSettings.PrinterName;

                     GetDefinedPapers(
"Adobe PDF");

              }

 

       }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值