简体   繁体   English

WPF DocumentViewer 覆盖打印按钮

[英]WPF DocumentViewer override Print button

I have implemented print preview functionality within my application using a custom DocumentViewer (shown below).我已经使用自定义 DocumentViewer(如下所示)在我的应用程序中实现了打印预览功能。 I call PrintDialog.ShowDialog() before showing the preview so as to correctly create the document based on paper orientation.我在显示预览之前调用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() ?有没有办法让 DocumentViewer 打印按钮简单地打印而不调用PrintDialog.ShowDialog()

Here are my method calls:这是我的方法调用:

ReportViewModel.cs报表视图模型.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));
        ...
    }

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.如果没有这个,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();
       }
   }

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");

To override Print command:要覆盖Print命令:

<DocumentViewer>
    <DocumentViewer.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Print" Executed="Print_Executed" />
    </DocumentViewer.CommandBindings>
</DocumentViewer>

The print button allone can not easily be overridden but I found a way to do it.打印按钮不容易被覆盖,但我找到了一种方法。 Even if it may be considered a dirty hack, what counts in the end is the result.即使它可能被认为是一个肮脏的黑客,最终重要的是结果。

I just overlayed the print button (which is the first button anyway) with a non-visible button that redirects clicks to my own print function.我只是用一个不可见的按钮覆盖了打印按钮(无论如何这是第一个按钮),该按钮将点击重定向到我自己的打印功能。 In this example code the search toolbar is also hidden and Ctrl+P is disabled:在此示例代码中,搜索工具栏也被隐藏并且 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>

I hope you have resolved this by now, but you may be able to use this question as a base for modifying the template for the DocumentViewer control:我希望您现在已经解决了这个问题,但是您可以使用这个问题作为修改 DocumentViewer 控件模板的基础:

How do you hide a WPF DocumentViewer's menu bars? 如何隐藏 WPF DocumentViewer 的菜单栏?

There is a link to an MSDN article that should show you how to modify the template so that the print button does what you want instead of the default Print action.有一个指向 MSDN 文章的链接,它应该向您展示如何修改模板,以便打印按钮执行您想要的操作,而不是默认的打印操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM