C#指定打印机打印

public bool Print(string _paperName, decimal _width, decimal _height, string _fpdm)
        {
            lock (_doPrint)
            {
                try
                {
                    //获取打印尺寸
                    GetInvoiceSize(ConnStr, _fpdm);
                    System.Drawing.Printing.PrintDocument printDoc = new System.Drawing.Printing.PrintDocument();
                    PrintController printController = new StandardPrintController();
                    printDoc.PrintController = printController;
                    int pWidth = Tools.ToInt(_width * Convert.ToDecimal(inchToMM)); //转换成百分之一英寸
                    int pHeight = Tools.ToInt(_height * Convert.ToDecimal(inchToMM));
                    bool isFind = false;
                    //遍历打印机
                    foreach (string pName in PrinterSettings.InstalledPrinters)
                    {
                        if (PrinterName.Trim().ToUpper() == pName.Trim().ToUpper())
                        {
                            printDoc.PrinterSettings.PrinterName = PrinterName;
                            isFind = true;
                            break;
                        }
                    }
                    if (isFind == false)
                    {
                        MessageBox.Show("未找到名为\'" + PrinterName.Trim().ToUpper() + "\'的打印机!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return false;
                    }

                    //遍历纸张
                    foreach (PaperSize paperSize in printDoc.PrinterSettings.PaperSizes)
                    {
                        if (paperSize.PaperName.ToUpper() == PaperName.ToUpper())
                        {
                            printDoc.DefaultPageSettings.PaperSize = paperSize;
                            break;
                        }
                    }

                    //  printDoc.DefaultPageSettings.PaperSize = new PaperSize();
                    printDoc.OriginAtMargins = true;
                    printDoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
                    printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDoc_PrintPage);
                    printDoc.Print();
                    return true;
                }
                catch (Exception ex)
                {
                    Tools.SaveErrLog(ex.Message, "PrinterPzb_Print");
                    return false;
                }
            }
        }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值