这个示例演示了如何进行一个最简单的文档打印,为此需要引入一个dll:reachframework.dll
vs2008中还需引入两个命名空间:
using System.IO;
using System.Windows.Xps.Packaging;
XAML前台代码:
<Button Height="24" Width="75" Margin="100,100,0,0" Name="btnPrint"
HorizontalAlignment="Left" VerticalAlignment="Top" Click="btnPrint_Click">打印文档</Button>
C#后台代码:
private void btnPrint_Click(object sender, RoutedEventArgs e)
{
string printFileName = @"C:\入住登记单.xps";
// 打印对话框,设置属性
PrintDialog pDialog = new PrintDialog();
pDialog.PageRangeSelection = PageRangeSelection.AllPages;
pDialog.UserPageRangeEnabled = true;
// 这里你还可以设置对话框的MaxPage, MinPage, PageRange, PrintableAreaHeight, PrintableAreaWidth, PrintQueue, PrintTicket属性值等。
// 显示对话框,如果用户点击“打印”按钮,则返回true。
Nullable<Boolean> print = pDialog.ShowDialog();
if (print == true)
{
System.Windows.Xps.Packaging.XpsDocument xpsDocument = new System.Windows.Xps.Packaging.XpsDocument(printFileName, System.IO.FileAccess.ReadWrite);
FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();
pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, "打印示例");
}
}