c# – WPF DocumentViewer覆盖打印按钮

我使用自定义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();
       }
   }

最佳答案 您可以填写打印机和纸张尺寸属性,然后不显示对话框.

var pd = new PrintDialog();
pd.PrintTicket.PageMediaSize = new PageMediaSize(PageMediaSizeName.NorthAmericaLetter, 816.0, 1056.0);
pd.PrintQueue = new LocalPrintServer().GetPrintQueue("Microsoft Print to PDF");

要覆盖打印命令:

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