为Wpf DocumentViewer PrintDialog设置PageOrientation

时间:2009-06-16 19:48:55

标签: wpf documentviewer printdialog

使用Wpf DocumentViewer控件我无法弄清楚当用户单击打印按钮时,如何在PrintDialog上设置DocumentViewer显示的PageOrientation。有没有办法搞定这个?

2 个答案:

答案 0 :(得分:16)

Mike's answer有效。我选择实现它的方式是创建自己的DocumentViewer派生的doc查看器。此外,将Document属性转换为FixedDocument对我来说不起作用 - 转换为FixedDocumentSequence是。

GetDesiredPageOrientation是您需要的任何东西。就我而言,我正在检查第一页的维度,因为我为文档中的所有页面生成了统一大小和方向的文档,并且序列中只有一个文档。

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows.Xps;
using System.Printing;
using System.Windows.Documents;

public class MyDocumentViewer : DocumentViewer
{
    protected override void OnPrintCommand()
    {
        // get a print dialog, defaulted to default printer and default printer's preferences.
        PrintDialog printDialog = new PrintDialog();
        printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
        printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket;

        // get a reference to the FixedDocumentSequence for the viewer.
        FixedDocumentSequence docSeq = this.Document as FixedDocumentSequence;

        // set the default page orientation based on the desired output.
        printDialog.PrintTicket.PageOrientation = GetDesiredPageOrientation(docSeq);

        if (printDialog.ShowDialog() == true)
        {
            // set the print ticket for the document sequence and write it to the printer.
            docSeq.PrintTicket = printDialog.PrintTicket;

            XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);
            writer.WriteAsync(docSeq, printDialog.PrintTicket);
        }
    }
}

答案 1 :(得分:10)

我在DocumentViewer的打印对话框中设置方向的解决方法是通过省略模板中的按钮来隐藏DocumentViewer控件上的打印按钮。然后我提供了自己的打印按钮并将其绑定到以下代码:

public bool Print()
    {
        PrintDialog dialog = new PrintDialog();
        dialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
        dialog.PrintTicket = dialog.PrintQueue.DefaultPrintTicket;
        dialog.PrintTicket.PageOrientation = PageOrientation.Landscape;

        if (dialog.ShowDialog() == true)
        {
            XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(dialog.PrintQueue);
            writer.WriteAsync(_DocumentViewer.Document as FixedDocument, dialog.PrintTicket);
            return true;
        }

        return false;
    }