2009-02-26 58 views
2

我使用WPF的DocumentViewer控件顯示XPS文檔,像這樣:如何在使用DocumentViewer控件時設置打印作業的名稱?

viewer.Document = xpsDocument.GetFixedDocumentSequence(); 

當文檔查看器內的打印按鈕被點擊的一切打印好,打印作業的不過名稱是System.Windows .Documents.FixedDocumentSequence,這是不理想的。

如何設置打印作業的名稱?

我知道使用PrintDialog.PrintDocument()讓我設置名稱,但是我看不到如何使用DocumentViewer控件執行此操作。

回答

3

我找到了解決方案。

這對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 
} 

一對夫婦的注意的事情。如果Execute事件未綁定,則PreviewExecuted方法不會發生。不知道爲什麼。

+0

這偉大的工作。謝謝。 – Eternal21 2015-05-21 13:49:11

3

我不得不但是覆蓋打印指令的同樣的問題不會在我的情況下工作,所以我找到了另外的工作圍繞着同樣適用

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); 
    } 
} 
相關問題