1. 程式人生 > 其它 >WPF備忘錄(6)WPF實現列印功能

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;//預設豎向列印
}
}