简体   繁体   English

如何从WPF MVVM中的用户控件创建PDF

[英]How does one create a PDF from a Usercontrol in WPF MVVM

I am creating a WPF application. 我正在创建一个WPF应用程序。 I use MS Visual Studio 2010, C# and MVVM. 我使用MS Visual Studio 2010,C#和MVVM。

On a usercontrol, I have a gridview. 在用户控件上,我有一个gridview。 That gridview is populated with data from another class, via binding and itemsourceing etc... No problems there. 通过绑定和itemsourceing等,该gridview填充了来自另一个类的数据。在那里没有问题。 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. gridview中的产品列表,下面提供了详细信息。

Once the user clicks OK, the application should now create a PDF of the entire usercontrol. 用户单击“确定”后,应用程序现在应创建整个用户控件的PDF。 How do I do that? 我怎么做?

I can export a gridview to Excel just fine. 我可以将gridview导出到Excel。 But now I want to basically recreate the usercontrol into a PDF file. 但是现在我想基本上将用户控件重新创建为PDF文件。 I have read online that I can create an image of the usercontrol and convert that image to PDF. 我已经在线阅读了可以创建用户控件的图像并将其转换为PDF的信息。 Would that help? 有帮助吗? I know from screenshots that the textboxes still have the borders around them and such. 从屏幕截图中我知道,文本框周围仍然有边框等。

But for now, how do I create a PDF from the usercontrol, using the MVVM approach? 但是现在,如何使用MVVM方法从用户控件创建PDF? 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? 如果我可以使用“保存到”对话框(用户可以在其中选择文件的保存位置),是否可以在字符串中获取该位置以在单独的用户控件上的报表查看器中使用它打开PDF文档?

If there is a need for coding, images or anything else, please comment and I'll edit the question with the changes. 如果需要编码,图像或其他任何东西,请发表评论,我将对所做的更改进行编辑。

Generating a PDF from a UserControl is non-trivial in C#. 在C#中,从UserControl生成PDF并非易事。 I'd strongly recommend finding a 3rd party tool to create your report design and to use that to export to PDF. 我强烈建议您找到一个第三方工具来创建您的报告设计,并将其用于导出为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 如果您决定不站在巨人的肩膀上并想自己滚滚,那么您需要实现以下目标

  • Create a separate control that can use your ViewModel as it's DataContext but which has the controls coloured and shaded for printing 创建一个单独的控件,该控件可以将ViewModel用作DataContext,但是将控件着色和着色以进行打印
  • Export the control to XPS using the System.Windows.Xps namespace libraries. 使用System.Windows.Xps命名空间库将控件导出到XPS。
  • Convert the XPS to PDF using a 3rd party tool. 使用第三方工具将XPS转换为PDF。

It's a pain, I know, but I can't see a simpler route for you. 我知道这很痛苦,但是我找不到适合您的简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM