Search code examples
.netwpfxpsdocumentviewer

How do I set the name of the print job when using DocumentViewer control?


I've using the WPF DocumentViewer control to display an XPS Document like so:

viewer.Document = xpsDocument.GetFixedDocumentSequence();

When the print button inside the document viewer is clicked everything prints okay, however the name of the print job is System.Windows.Documents.FixedDocumentSequence, which is less than ideal.

How do I set the name of the print job?

I know using PrintDialog.PrintDocument() lets me set the name, but I can't see how to do it using the DocumentViewer control.


Solution

  • I found a solution.

    Add this to the XAML

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

    And this to the code behind

    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
    }
    

    A couple of things of note. The PreviewExecuted method doesn't happen if the Exectued event isn't bound to. Don't know why.