如何从 WPF MVVM 中的用户控件创建 PDF [英] How does one create a PDF from a Usercontrol in WPF MVVM

查看:34
本文介绍了如何从 WPF MVVM 中的用户控件创建 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 WPF 应用程序.我使用 MS Visual Studio 2010、C# 和 MVVM.

I am creating a WPF application. I use MS Visual Studio 2010, C# and MVVM.

在用户控件上,我有一个网格视图.该 gridview 填充了来自另一个类的数据,通过绑定和 itemsourceing 等......那里没有问题.我看到了数据和一切.我在用户控件周围也有一些标签和文本框.用户将使用这些来填充随机数据.诸如姓名、地址之类的东西.你可以称之为订单.gridview 中的产品列表以及下面提供的详细信息.

On a usercontrol, I have a gridview. That gridview is populated with data from another class, via binding and itemsourceing etc... No problems there. I see the data and everything. I also have a few labels and textboxes around the usercontrol. These will be used by the user to fill in random data. Stuff like name, address and the such. You could call it an order form. The list of products in the gridview with the details provided below.

一旦用户单击确定",应用程序现在应该创建整个用户控件的 PDF.我怎么做?

Once the user clicks OK, the application should now create a PDF of the entire usercontrol. How do I do that?

我可以很好地将 gridview 导出到 Excel.但现在我想基本上将用户控件重新创建为 PDF 文件.我在网上读到我可以创建用户控件的图像并将该图像转换为 PDF.那会有帮助吗?我从屏幕截图中知道文本框周围仍然有边框等.

I can export a gridview to Excel just fine. But now I want to basically recreate the usercontrol into a PDF file. I have read online that I can create an image of the usercontrol and convert that image to PDF. Would that help? I know from screenshots that the textboxes still have the borders around them and such.

但是现在,我如何使用 MVVM 方法从用户控件创建 PDF?还有一个附带问题.如果我可以使用保存到对话框,用户可以在其中选择保存文件的位置,我可以在字符串中获取该位置以使用它在单独用户控件上的报告查看器中打开 PDF 文档吗?

But for now, how do I create a PDF from the usercontrol, using the MVVM approach? And a side question. If I can use a save to dialog box, where the user can select where to save the file, can I get that location in a string to use it to open the PDF document in a report viewer on a seperate usercontrol?

如果需要编码、图像或其他任何内容,请发表评论,我将根据更改编辑问题.

If there is a need for coding, images or anything else, please comment and I'll edit the question with the changes.

推荐答案

在 C# 中从 UserControl 生成 PDF 非常重要.我强烈建议您使用第 3 方工具来创建您的报告设计并使用它导出为 PDF.

Generating a PDF from a UserControl is non-trivial in C#. I'd strongly recommend finding a 3rd party tool to create your report design and to use that to export to PDF.

如果你决定不站在巨人的肩膀上,而是想自己动手,那么你需要实现以下内容

If you decide not to stand on the shoulders of giants and want to roll your own, then you'll need to implement the following

  • 创建一个单独的控件,该控件可以像 DataContext 一样使用您的 ViewModel,但该控件具有用于打印的彩色和阴影控件
  • 使用 System.Windows.Xps 命名空间库将控件导出到 XPS.
  • 使用第三方工具将 XPS 转换为 PDF.

这很痛苦,我知道,但我找不到更简单的路线.

It's a pain, I know, but I can't see a simpler route for you.

这篇关于如何从 WPF MVVM 中的用户控件创建 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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