WPF打印流程文档

发布于 2024-08-10 03:39:55 字数 194 浏览 7 评论 0原文

问候, 我在 WPF 中打印时遇到问题。 我正在创建一个流程文档并向该流程文档添加一些控件。 打印预览工作正常,我从打印预览窗口打印没有问题。 当我直接打印到打印机而不进行打印预览时,会出现此问题。但更令人惊讶的是 - 当我使用 XPS Document Writer 作为打印机时 一切正常,当我使用某些物理打印机时,我的流程文档上的某些控件不显示。 提前致谢

Greetings,
I have a problem with printing in WPF.
I am creating a flow document and add some controls to that flow document.
Print Preview works ok and i have no problem with printing from a print preview window.
The problem exists when I print directly to the printer without a print preview. But what is more surprisingly - when I use XPS Document Writer as a printer
everyting is ok, when i use some physical printer, some controls on my flow document are not displayed.
Thanks in advance

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

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

发布评论

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

评论(3

情深已缘浅 2024-08-17 03:39:55

需要注意的重要事项:即使直接打印到物理打印机,您也可以使用 XpsDocumentWriter。不要犯我所犯的错误,仅仅因为您没有创建 .xps 文件就避免了它!

无论如何 - 我也遇到了同样的问题,并且没有任何 DoEvents() hacks似乎有效。我对于一开始就必须使用它们也不是特别高兴。在我的情况下,一些数据绑定控件打印得很好,但其他一些(嵌套用户控件)却没有。就好像只有一个“级别”正在进行数据绑定,其余的即使使用“DoEvents()”黑客也不会绑定。

解决方案很简单。像这样使用 XpsDocumentWriter。它将打开一个对话框,您可以在其中选择您想要安装的物理打印机。

        // 8.5 x 11 paper
        Size sz = new Size(96 * 8.5, 96 * 11);

        // create your visual (this is a WPF UserControl)
        var template = new PackingSlipTemplate()
        {
            DataContext = new PackingSlipViewModel(order)
        };

        // arrange
        template.Measure(sz);
        template.Arrange(new Rect(sz));
        template.UpdateLayout();

        // print to XpsDocumentWriter
        // this will open a dialog and you can print to any installed printer
        // not just a 'virtual' .xps file
        PrintDocumentImageableArea area = null;
        XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(ref area,);

        xps.Write(template);

我发现 OReilly 的关于 'Programming WPF' 的书非常有用,其中的章节打印 - 通过 Google 图书找到


如果您不希望出现打印对话框,但希望直接打印到默认打印机,您可以执行以下操作。 (对我来说,该应用程序是在仓库环境中打印装箱单 - 我不希望每次都弹出一个对话框)。

        var template = new PackingSlipTemplate()
        {
            DataContext = new PackingSlipViewModel(orders.Single())
        };

        // arrange
        template.Measure(sz);
        template.Arrange(new Rect(sz));
        template.UpdateLayout();

        LocalPrintServer localPrintServer = new LocalPrintServer();
        var defaultPrintQueue = localPrintServer.DefaultPrintQueue;

        XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(defaultPrintQueue);
        xps.Write(template, defaultPrinter.DefaultPrintTicket);

Important thing to note : You can use XpsDocumentWriter even when printing directly to a physical printer. Don't make the mistake I did of avoiding it just because you're not creating an .xps file!

Anyway - I had this same problem, and none of the DoEvents() hacks seemed to work. I also wasn't particularly happy about having to use them in the first place. In my situation some of the databound controls printed fine, but some others (nested UserControls) didnt. It was as if only one 'level' was being databound and the rest wouldn't bind even with a 'DoEvents()' hack.

The solution was simple though. Use XpsDocumentWriter like this. it will open a dialog where you can choose whichever installed physical printer you want.

        // 8.5 x 11 paper
        Size sz = new Size(96 * 8.5, 96 * 11);

        // create your visual (this is a WPF UserControl)
        var template = new PackingSlipTemplate()
        {
            DataContext = new PackingSlipViewModel(order)
        };

        // arrange
        template.Measure(sz);
        template.Arrange(new Rect(sz));
        template.UpdateLayout();

        // print to XpsDocumentWriter
        // this will open a dialog and you can print to any installed printer
        // not just a 'virtual' .xps file
        PrintDocumentImageableArea area = null;
        XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(ref area,);

        xps.Write(template);

I found the OReilly book on 'Programming WPF' quite useful with its chapter on Printing - found through Google Books.


If you don't want a print dialog to appear, but want to print directly to the default printer you can do the following. (For me the application is to print packing slips in a warehouse environment - and I don't want a dialog popping up every time).

        var template = new PackingSlipTemplate()
        {
            DataContext = new PackingSlipViewModel(orders.Single())
        };

        // arrange
        template.Measure(sz);
        template.Arrange(new Rect(sz));
        template.UpdateLayout();

        LocalPrintServer localPrintServer = new LocalPrintServer();
        var defaultPrintQueue = localPrintServer.DefaultPrintQueue;

        XpsDocumentWriter xps = PrintQueue.CreateXpsDocumentWriter(defaultPrintQueue);
        xps.Write(template, defaultPrinter.DefaultPrintTicket);
吃兔兔 2024-08-17 03:39:55

XPS文档可以毫无问题地打印

XPS Document can be printed without a problem

内心荒芜 2024-08-17 03:39:55

我注意到一件事:
提示:没有显示的控件是我正在绑定一些数据的控件,所以结论是绑定不起作用。是否可能是在将文档发送到打印机之前未执行装订?

i have noticed one thing:
tip: the controls that are not displayed are the controls I am binding some data, so the conclusion is that the binding doesn't work. Can it be the case that binding is not executing before sending the document to the printer?

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