本文主要是介绍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();
这篇关于WPF无弹窗打印_se7en3_新浪博客的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!