WPF无弹窗打印_se7en3_新浪博客

项目打印小单,不想弹窗

 public class PrintDialogHelper
    {
        private const string PrintServerName = "艹";
        private const string PrintName = "Microsoft XPS Document Writer";

        public PrintDialogHelper()
        {
        }

        ///
        /// 打印控件
        ///
        ///
        public void PrintVisual(FrameworkElement element)
        {
            var printDialog = new PrintDialog();
            SetPrintProperty(printDialog);
            var printQueue = SelectedPrintServer(PrintServerName, PrintName);
            if (printQueue != null)
            {
                printDialog.PrintQueue = printQueue;
                printDialog.PrintVisual(element, "");
            }
        }

        public void PrintDocument(DocumentPaginator docuemnt)
        {
            var printDialog = new PrintDialog();
            SetPrintProperty(printDialog);
            var printQueue = SelectedPrintServer(PrintServerName, PrintName);
            if (printQueue != null)
            {
                printDialog.PrintQueue = printQueue;
                printDialog.PrintDocument(docuemnt, "");
            }
        }

        ///
        /// 查找并获取打印机
        ///
        /// 服务器名字: Lee-pc
        /// 打印机名字:Hp laserjet m1522 mfp series pcl 6
        ///
        public PrintQueue SelectedPrintServer(string printerServerName, string printerName)
        {
            try
            {
                var printers = PrinterSettings.InstalledPrinters;//获取本机上的所有打印机
                PrintServer printServer = null;

                foreach (string printer in printers)
                {
                    if (printer.Contains(printerName))
                        printServer = new PrintServer("\\\\" + printerServerName);
                }

                if (printServer == null) return null;//没有找到打印机服务器

                var printQueue = printServer.GetPrintQueue(printerName);
                return printQueue;
            }
            catch (Exception)
            {
                return null;//没有找到打印机
            }
        }

        ///
        /// 设置打印格式
        ///
        /// 打印文档
        /// 打印纸张大小 a4
        /// 打印方向 竖向
        public void SetPrintProperty(PrintDialog printDialog, PageMediaSizeName pageSize = PageMediaSizeName.ISOA4, PageOrientation pageOrientation = PageOrientation.Portrait)
        {
            var printTicket = printDialog.PrintTicket;
            printTicket.PageMediaSize = new PageMediaSize(pageSize);//A4纸
            printTicket.PageOrientation = pageOrientation;//默认竖向打印
        }
    }

需引用

System.Printing.dll

reachframework.dll

打印的按钮事件 调用

//需打印窗体,页或控件

var printPage = new frmShopOrderPrintView();

            var printdialog = new PrintDialogHelper();
            printdialog.PrintVisual(printPage);
            printPage.ShowDialog();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值