WPF 打印功能全解析
1. 打印元素
在 WPF 中,我们可以打印任何元素并根据需求进行缩放。以下代码展示了如何移除变换并使元素再次可见:
// Remove the transform and make the element visible again.
canvas.LayoutTransform = null;
grid.Visibility = Visibility.Visible;
这种方法能很好地工作,但代码结构可能有些杂乱。
1.1 不显示元素进行打印
有时,应用程序中显示数据的方式与打印输出的方式不同,这时可以通过编程方式创建可视化元素。以下是一个示例代码,创建一个内存中的 TextBlock 对象,填充文本,设置换行,调整大小以适应打印页面,然后进行打印:
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
// Create the text.
Run run = new Run("This is a test of the printing functionality " +
"in the Windows Presentation Foundation.");
// Wrap it in a TextBlock.
TextBloc
超级会员免费看
订阅专栏 解锁全文
52

被折叠的 条评论
为什么被折叠?



