代码之家  ›  专栏  ›  技术社区  ›  Ray

使用DocumentViewer控件时,如何设置打印作业的名称?

  •  2
  • Ray  · 技术社区  · 16 年前

    viewer.Document = xpsDocument.GetFixedDocumentSequence();
    

    单击文档查看器中的“打印”按钮时,所有内容都会正常打印,但打印作业的名称为System.Windows.Documents.FixedDocumentSequence,这并不理想。

    2 回复  |  直到 16 年前
        1
  •  4
  •   Ray    16 年前

    将此添加到XAML

    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Print" PreviewExecuted="CommandBinding_PreviewExecuted" Executed="CommandBinding_Executed" />
    </Window.CommandBindings>
    

    private void CommandBinding_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        PrintDialog dialog = new PrintDialog();
        if (dialog.ShowDialog() == true)
        {
            dialog.PrintDocument(Document.DocumentPaginator, "Print Job Title");
        }
    }
    
    private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        //needed so that preview executed works
    }
    

    有几件事值得注意。如果Executed事件未绑定到,则PreviewExecuted方法不会发生。不知道为什么。

        2
  •  3
  •   Matt    13 年前

    我也有同样的问题,但是覆盖Print命令在我的情况下不起作用,所以我找到了另一个同样有效的解决方法

    internal class MyDocumentViewer : DocumentViewer
    {
        public string JobTitle { get; set; }
    
        protected override void OnPrintCommand()
        {
            PrintDialog dialog = new PrintDialog();
            if (dialog.ShowDialog() == true)
                dialog.PrintDocument(Document.DocumentPaginator, JobTitle);
        }
    }