Настройка PageOrientation для Wpf DocumentViewer PrintDialog

Используя элемент управления Wpf DocumentViewer, я не могу понять, как установить параметр PageOrientation на PrintDialog, отображаемый DocumentViewer, когда пользователь нажимает кнопку печати. Есть ли способ подключиться к этому?

Ответы

Ответ 1

Майк отвечает. То, как я решил реализовать это, было вместо этого создать собственный просмотрщик документов, полученный из 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 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);
        }
    }
}

Ответ 2

Обходной путь, который я использовал для установки ориентации в диалоговом окне печати 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;
    }