C#使用window API 控制打印纸张大小

windows一个特点就是设备无关性,这样就给程序控制打印机提供了很好的方法。

首先引用“泥人张”写的打印API类。

using  System;
using  System.Collections;
using  System.Text;
using  System.Runtime.InteropServices;
using  System.Security;
using  System.ComponentModel;
using  System.Drawing.Printing;

namespace  PrintAPI
{
    
public class Printer
    
{
        
private Printer()
        
{

        }

        
///泥人张版本加强版
        API声明   API声明

        
internal static int GetPrinterStatusInt(string PrinterName)
        
{
            
int intRet = 0;
            IntPtr hPrinter;
            structPrinterDefaults defaults 
= new structPrinterDefaults();

            
if (OpenPrinter(PrinterName, out hPrinter, ref defaults))
            
{
                
int cbNeeded = 0;
                
bool bolRet = GetPrinter(hPrinter, 2, IntPtr.Zero, 0out cbNeeded);
                
if (cbNeeded > 0)
                
{
                    IntPtr pAddr 
= Marshal.AllocHGlobal((int)cbNeeded);
                    bolRet 
= GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded);
                    
if (bolRet)
                    
{
                        PRINTER_INFO_2 Info2 
= new PRINTER_INFO_2();

                        Info2 
= (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));

                        intRet 
= System.Convert.ToInt32(Info2.Status);
                    }

                    Marshal.FreeHGlobal(pAddr);
                }

                ClosePrinter(hPrinter);
            }


            
return intRet;
        }


        
internal static PRINTER_INFO_2[] EnumPrintersByFlag(PrinterEnumFlags Flags)
        
{
            
uint cbNeeded = 0;
            
uint cReturned = 0;
            
bool ret = EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null2, IntPtr.Zero, 0ref cbNeeded, ref cReturned);

            IntPtr pAddr 
= Marshal.AllocHGlobal((int)cbNeeded);
            ret 
= EnumPrinters(PrinterEnumFlags.PRINTER_ENUM_LOCAL, null2, pAddr, cbNeeded, ref cbNeeded, ref cReturned);

            
if (ret)
            
{
                PRINTER_INFO_2[] Info2 
= new PRINTER_INFO_2[cReturned];

                
int offset = pAddr.ToInt32();

                
for (int i = 0; i < cReturned; i++)
                
{
                    Info2[i].pServerName 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset 
+= 4;
                    Info2[i].pPrinterName 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset 
+= 4;
                    Info2[i].pShareName 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset 
+= 4;
                    Info2[i].pPortName 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset 
+= 4;
                    Info2[i].pDriverName 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset 
+= 4;
                    Info2[i].pComment 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset 
+= 4;
                    Info2[i].pLocation 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset 
+= 4;
                    Info2[i].pDevMode 
= Marshal.ReadIntPtr(new IntPtr(offset));
                    offset 
+= 4;
                    Info2[i].pSepFile 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset 
+= 4;
                    Info2[i].pPrintProcessor 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset 
+= 4;
                    Info2[i].pDatatype 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset 
+= 4;
                    Info2[i].pParameters 
= Marshal.PtrToStringAuto(Marshal.ReadIntPtr(new IntPtr(offset)));
                    offset 
+= 4;
                    Info2[i].pSecurityDescriptor 
= Marshal.ReadIntPtr(new IntPtr(offset));
                    offset 
+= 4;
                    Info2[i].Attributes 
= (uint)Marshal.ReadIntPtr(new IntPtr(offset));
                    offset 
+= 4;
                    Info2[i].Priority 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset 
+= 4;
                    Info2[i].DefaultPriority 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset 
+= 4;
                    Info2[i].StartTime 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset 
+= 4;
                    Info2[i].UntilTime 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset 
+= 4;
                    Info2[i].Status 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset 
+= 4;
                    Info2[i].cJobs 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset 
+= 4;
                    Info2[i].AveragePPM 
= (uint)Marshal.ReadInt32(new IntPtr(offset));
                    offset 
+= 4;

                }


                Marshal.FreeHGlobal(pAddr);

                
return Info2;

            }

            
else
            
{
                
return new PRINTER_INFO_2[0];
            }

        }


        
获取当前指定打印机的状态 获取当前指定打印机的状态
        
删除已经存在的自定义纸张 删除已经存在的自定义纸张
        
指定的打印机设置以mm为单位的自定义纸张(Form) 指定的打印机设置以mm为单位的自定义纸张(Form)
        
        
获取本地打印机列表 获取本地打印机列表

        
获取本机的默认打印机名称 获取本机的默认打印机名称

        
设置默认打印机 设置默认打印机

        
判断打印机是否在系统可用的打印机列表中 判断打印机是否在系统可用的打印机列表中

        
判断表单是否在指定的打印机所支持的纸张列表中 判断表单是否在指定的打印机所支持的纸张列表中

        
判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配 判断指定纸张的宽度和高度和与打印内容指定的宽度和高度是否匹配

        
英寸到厘米的转换 英寸到厘米的转换
    }


}

 然后在程序里调用写好的API即可。

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  System.Runtime.InteropServices;
using  System.IO;

namespace  PrintAPI
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }

        
读取ini文件
        
        
读取打印机相关配置

        
private void button1_Click(object sender, EventArgs e)
        
{

            
            XRInvoice invoice 
= new XRInvoice();
            
            
try
            
{
                
//添加票据类型的打印规格
                AddBillSizeForPrinter();

                
//指定报表打印的规格
                string invoicePrinterName = getPrinterName("InvoicePrinter");
                invoice.PaperKind 
= System.Drawing.Printing.PaperKind.Custom;
                invoice.PaperName 
= "XinLiInvoice";
                invoice.Print(invoicePrinterName);

                
//指定收据打印规格

            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.ToString());
            }

            
        }


        
private void AddBillSizeForPrinter()
        
{
            
//放到整个项目程序加载的时候

            
//读取发票、收据打印机的名称
            string invoicePrinterName = getPrinterName("InvoicePrinter");
            
string receiptPrinterName = getPrinterName("receiptPrinter");
            
//读取发票、收据打印机的名称

            
//判断发票打印机是否在打印机列表中
            bool isExistInvoicePrinter = Printer.PrinterInList(invoicePrinterName);
            
//判断发票打印机是否在打印机列表中
            if (!isExistInvoicePrinter)
            
{
                
throw new Exception("指定的打印机不存在!");
            }

            
            
//从ini文件中读取票据类型的size
            int width = 0;
            
int height = 0;
            
//添加发票规格
            getPrinterSize("invoice"ref width, ref height);
            Printer.AddCustomPaperSize(invoicePrinterName, 
"XinLiInvoice", width, height);
            
//添加收据规格
            getPrinterSize("receipt"ref width, ref height);
            Printer.AddCustomPaperSize(receiptPrinterName, 
"XinLiReceipt", width, height);
        }

    }

}

 使用到的Printer.ini配置文件

[ Printer ]
InvoicePrinter
=  pdfFactory Pro
ReceiptPrinter 
=  pdfFactory Pro

[ BillSize ]
InvoiceWidth 
=   706
InvoiceHeight 
=   515

ReceiptWidth 
=   706
ReceiptHeight 
=   515

是不是很简单呢?

你可能感兴趣的:(.NET技术,api,c#,string,access,struct,printing)