如何打印 wpf 窗口而不在屏幕上显示它? [英] How to print a wpf window without showing it on the screen?

查看:46
本文介绍了如何打印 wpf 窗口而不在屏幕上显示它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我需要打印一份完整的表格,但不显示在屏幕上.

I have a requirement where I need to print a complete form without showing it on screen.

我需要的是:

  1. 初始化表单
  2. 打印

一切都没有显示在屏幕上.

All without showing on the screen.

请问有什么建议吗?

推荐答案

您可以使用 PrintDialog.PrintVisual 方法执行此操作.

You can do this using PrintDialog.PrintVisual method.

var capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);

//get scale of the print wrt to screen of WPF visual
var scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight / this.ActualHeight);

//Transform the Visual to scale
this.LayoutTransform = new ScaleTransform(scale, scale);

// Get the size of the printer page
var sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

//update the layout of the visual to the printer page size.
this.Measure(sz);
this.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

//now print the visual to printer to fit on the one page.
printDlg.PrintVisual(visual, String.Empty);

这篇关于如何打印 wpf 窗口而不在屏幕上显示它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆