我已经在我的应用程序中使用自定义的DocumentViewer实现了打印预览功能(如下所示)。在显示预览之前,我调用
然而,DocumentViewer打印按钮调用了
有没有办法让DocumentViewer打印按钮在不调用
以下是我的方法调用:
ReportViewModel.cs
我这样做是为了能够正确生成具有纵向或横向大小值的分页器。如果没有这样做,则文档查看器中的预览文档可能根据所选择的方向无法正确显示。 PrintDocumentViewer: DocumentViewer
PrintDialog.ShowDialog()
以便根据纸张方向正确创建文档。然而,DocumentViewer打印按钮调用了
PrintDialog.ShowDialog()
,再次提示用户选择打印机和选项(他们在打开预览窗口之前已经这样做了)。有没有办法让DocumentViewer打印按钮在不调用
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));
...
}
我这样做是为了能够正确生成具有纵向或横向大小值的分页器。如果没有这样做,则文档查看器中的预览文档可能根据所选择的方向无法正确显示。 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();
}
}