WPF备忘录(6)WPF实现打印功能
在WPF 中可以通过PrintDialog 类方便的实现应用程序打印功能,本文将使用一个简单实例进行演示。首先在VS中编辑一个图形(如下图所示)。
将需要打印的内容放入同一个<Canvas>中,并起名为“printArea”,打印按键一般不是我们希望打印出来的内容,则将其放在<Canvas>外面。
<Window x:Class="WpfPrint.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="500">
<Grid>
<Canvas x:Name="printArea">
<Ellipse Canvas.Left="137" Canvas.Top="92" Height="100"
Stroke="Black" Width="200">
<Ellipse.Fill>
<LinearGradientBrush>
<GradientStop Color="#FFAD0FC7" Offset="0" />
<GradientStop Color="#FF3359AD" Offset="1" />
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
<TextBlock FontSize="20" FontWeight="Bold" Foreground="White"
Canvas.Left="151" Canvas.Top="129" Height="33">
Visual Studio 2010</TextBlock>
<Image Source="vs2010.jpg" Height="52" Width="90"
Canvas.Left="388" Canvas.Top="0" />
</Canvas>
<Button Content="Print" Click="Button_Click" Height="23"
Margin="195,268,190,20" />
</Grid>
</Window>
接下来编写Button_Click 事件,由于我们要打印<Canvas>所包含的内容,所以要通过PrintVisual 打印Visual 对象。
private void Button_Click(object sender, RoutedEventArgs e) { PrintDialog dialog = new PrintDialog(); if (dialog.ShowDialog() == true) { dialog.PrintVisual(printArea, "Print Test"); } }
运行程序,点击“Print”按键,弹出打印设置窗口,打印到XPS看看效果。
如下图所示,打印结果中只有<Canvas>中的内容。
WPF 打印控件 无弹框打印
WPF中打印用到了 PrintDialog类。
其中设置打印属性的是PrintTicket,管理打印机的是PrintQueue。
实例如下:
public class PrintDialogHelper
{
private const string PrintServerName = "YULIN-PC";
private const string PrintName = "HP LaserJet M1522 series PCL6 Class Driver";
public PrintDialogHelper()
{
}
/// <summary>
/// 打印控件
/// </summary>
/// <param name="element"></param>
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, "");
}
}
/// <summary>
/// 查找并获取打印机
/// </summary>
/// <param name="printerServerName">服务器名字: Lee-pc</param>
/// <param name="printerName">打印机名字:Hp laserjet m1522 mfp series pcl 6 </param>
/// <returns></returns>
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;//没有找到打印机
}
}
/// <summary>
/// 设置打印格式
/// </summary>
/// <param name="printDialog">打印文档</param>
/// <param name="pageSize">打印纸张大小 a4</param>
/// <param name="pageOrientation">打印方向 竖向</param>
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;//默认竖向打印
}
}