博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WPF中的文档打印

Posted on 2012-07-23 23:53  linFen  阅读(1153)  评论(0编辑  收藏  举报

C#代码:
    string printFileName = @"C:/TestForPrint.xps";
    public void InvokePrint(object sender, RoutedEventArgs e)
    {
        // 打印对话框,设置属性
        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)
        {
            XpsDocument xpsDocument = new XpsDocument(printFileName, FileAccess.ReadWrite);
            FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();
            pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, "Test print");
        }
    }