我已使用自定义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));
...
}
我这样做是为了能够正确生成具有纵向或横向大小值的分页器。如果没有这个,DocumentViewer中的预览文档可能无法根据所选方向正确显示。
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();
}
}
答案 0 :(得分:3)
您可以填写打印机和纸张尺寸属性,然后不显示对话框。
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>
答案 1 :(得分:0)
我希望您现在已经解决了这个问题,但您可以将此问题用作修改DocumentViewer控件模板的基础:
How do you hide a WPF DocumentViewer's menu bars?
有一个MSDN文章的链接,该文章应该向您展示如何修改模板,以便打印按钮执行您想要的操作而不是默认的Print操作。
答案 2 :(得分:0)
打印按钮不能轻易被覆盖,但我找到了一种方法。即使它可能被认为是一个肮脏的黑客,最终重要的是结果。
我只是用一个不可见的按钮覆盖了打印按钮(无论如何是第一个按钮),该按钮将点击重定向到我自己的打印功能。在此示例代码中,搜索工具栏也被隐藏,Ctrl + P被禁用:
<Grid>
<DocumentViewer x:Name="DocumentViewer">
<DocumentViewer.Resources>
<Style TargetType="ContentControl">
<Style.Triggers>
<Trigger Property="Name" Value="PART_FindToolBarHost">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</Style.Triggers>
</Style>
</DocumentViewer.Resources>
<DocumentViewer.InputBindings>
<KeyBinding Key="P" Modifiers="Control" Command="ApplicationCommands.NotACommand" />
</DocumentViewer.InputBindings>
</DocumentViewer>
<Button Width="32" Height="32" VerticalAlignment="Top" HorizontalAlignment="Left" Opacity="0.01" Click="OnPrint" ></Button>
</Grid>