为Wpf DocumentViewer PrintDialog设置PageOrientation [英] Setting PageOrientation for the Wpf DocumentViewer PrintDialog
问题描述
使用Wpf DocumentViewer控件,我无法弄清楚如何在用户单击打印按钮时在DocumentViewer显示的PrintDialog上设置PageOrientation.有没有办法迷上这个?
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?
推荐答案
迈克的答案有效.我选择实现它的方法是改为创建自己的从DocumentViewer派生的doc查看器.另外,将Document属性强制转换为FixedDocument对我不起作用-强制转换为FixedDocumentSequence.
Mike's answer works. The way I chose to implement it was to instead create my own doc viewer derived from DocumentViewer. Also, casting the Document property to FixedDocument wasn't working for me - casting to FixedDocumentSequence was.
GetDesiredPageOrientation是您需要的任何内容.就我而言,我正在检查第一页的尺寸,因为我生成的文档的尺寸和方向对于文档中的所有页面都是一致的,并且序列中只有一个文档.
GetDesiredPageOrientation is whatever you need it to be. In my case, I'm inspecting the first page's dimensions, because I generate documents that are uniform size and orientation for all pages in the document, and with only one doc in the sequence.
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 DocumentViewer PrintDialog设置PageOrientation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!