WPF DocumentViewer覆盖“打印"按钮 [英] WPF DocumentViewer override Print button
问题描述
我已经使用自定义DocumentViewer(如下所示)在我的应用程序中实现了打印预览功能.我在显示预览之前先调用PrintDialog.ShowDialog()
,以便根据纸张方向正确创建文档.
I have implemented print preview functionality within my application using a custom DocumentViewer (shown below). I call PrintDialog.ShowDialog()
before showing the preview so as to correctly create the document based on paper orientation.
但是,DocumentViewer的打印按钮会调用PrintDialog.ShowDialog()
,提示用户再次选择打印机和选项(在打开预览窗口之前,他们已经这样做了).
The DocumentViewer print button however calls the PrintDialog.ShowDialog()
prompting the user to chose printer and options once again (which they already did prior to the preview window opening).
有没有一种方法可以让DocumentViewer打印按钮简单地打印而无需调用PrintDialog.ShowDialog()
?
Is there a way to have the DocumentViewer print button simply print without calling PrintDialog.ShowDialog()
?
这是我的方法调用:
ReportViewModel.cs
public void PrintButtonClick(DataGrid dataGrid)
{
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == false)
return;
// Get page size based on print dialog printable area (orientation)
Size pageSize = new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight);
// create new paginator for datagrid
DataGridDocumentPaginator paginator = new DataGridDocumentPaginator(dataGrid as DataGrid, "Employer Match Report", pageSize, new Thickness(30, 20, 30, 20));
...
}
我这样做是为了正确生成纵向或横向尺寸值的分页器.否则,基于所选的方向,DocumentViewer中的预览文档可能无法正确显示.
I am doing it this way so that I can correctly generate the paginator with either Portrait or Landscape size values. Without this the preview document within the DocumentViewer might not display correctly based on orientation chosen.
PrintDocumentViewer:DocumentViewer
protected override void OnPrintCommand()
{
PrintDialog printDialog = new PrintDialog();
printDialog.PrintQueue = LocalPrintServer.GetDefaultPrintQueue();
printDialog.PrintTicket = printDialog.PrintQueue.DefaultPrintTicket;
printDialog.PrintTicket.PageOrientation = PageOrientation;
// Code assumes this.Document will either by a FixedDocument or a FixedDocumentSequence
FixedDocument fixedDocument = this.Document as FixedDocument;
FixedDocumentSequence fixedDocumentSequence = this.Document as FixedDocumentSequence;
if (fixedDocument != null)
fixedDocument.PrintTicket = printDialog.PrintTicket;
if (fixedDocumentSequence != null)
fixedDocumentSequence.PrintTicket = printDialog.PrintTicket;
XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(printDialog.PrintQueue);
if (fixedDocument != null)
writer.WriteAsync(fixedDocument, printDialog.PrintTicket);
if (fixedDocumentSequence != null)
writer.WriteAsync(fixedDocumentSequence, printDialog.PrintTicket);
// Create Preview Window and show preview
string s = _previewWindowXaml;
using (var reader = new System.Xml.XmlTextReader(new StringReader(s)))
{
Window preview = System.Windows.Markup.XamlReader.Load(reader) as Window;
DocumentViewer _docViewer = LogicalTreeHelper.FindLogicalNode(preview, "PrintDocumentViewer") as DocumentViewer;
_docViewer.Document = (fixedDocument != null) ? fixedDocument as IDocumentPaginatorSource : fixedDocumentSequence as IDocumentPaginatorSource;
// hide the search bar in the PrintPreview dialog
ContentControl cc = _docViewer.Template.FindName("PART_FindToolBarHost", _docViewer) as ContentControl;
cc.Visibility = Visibility.Collapsed;
preview.ShowDialog();
}
}
推荐答案
您可以填写打印机和纸张尺寸属性,然后不会显示对话框.
You may fill printer and paper size properties, then dialog is not shown.
var pd = new PrintDialog();
pd.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.NorthAmericaLetter, 816.0, 1056.0);
pd.PrintQueue = new LocalPrintServer().GetPrintQueue("Microsoft Print to PDF");
要覆盖Print
命令:
<DocumentViewer>
<DocumentViewer.CommandBindings>
<CommandBinding Command="ApplicationCommands.Print" Executed="Print_Executed" />
</DocumentViewer.CommandBindings>
</DocumentViewer>
这篇关于WPF DocumentViewer覆盖“打印"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!