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

最佳答案

Mike's answer有效。我选择实现它的方法是创建自己的派生自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);
        }
    }
}

关于wpf - 为Wpf DocumentViewer PrintDialog设置PageOrientation,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1003585/

10-09 14:45