18

Using the Wpf DocumentViewer control I can't figure out how to set the PageOrientation on the PrintDialog that the DocumentViewer displays when the user clicks the print button. Is there a way to hook into this?

4

2 回答 2

16

迈克的回答有效。我选择实现它的方式是创建我自己的从 DocumentViewer 派生的文档查看器。此外,将 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);
        }
    }
}
于 2010-01-14T18:29:38.270 回答
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;
    }
于 2009-07-01T11:51:42.583 回答