简体   繁体   中英

How does one create a PDF from a Usercontrol in WPF MVVM

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

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.

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

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.

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.

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

  • Create a separate control that can use your ViewModel as it's DataContext but which has the controls coloured and shaded for printing
  • Export the control to XPS using the System.Windows.Xps namespace libraries.
  • Convert the XPS to PDF using a 3rd party tool.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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