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

发布于 2024-07-14 17:20:05 字数 343 浏览 15 评论 0原文

我使用 WPF DocumentViewer 控件来显示 XPS 文档,如下所示:

viewer.Document = xpsDocument.GetFixedDocumentSequence();

单击文档查看器内的打印按钮时,所有内容都可以正常打印,但是打印作业的名称是 System.Windows.Documents.FixedDocumentSequence,它小于理想的。

如何设置打印作业的名称?

我知道使用 PrintDialog.PrintDocument() 可以让我设置名称,但我不知道如何使用 DocumentViewer 控件来设置名称。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨小沫ゞ 2024-07-21 17:20:05

我找到了解决方案。

将其添加到 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
}

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

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.

无所的.畏惧 2024-07-21 17:20:05

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

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

I had the same issue however overriding the Print command wouldn't work in my situation so i found another work around that works equally well

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文