Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing large WPF User Controls

Tags:

c#

wpf

xaml

I have a huge data which I want to print using WPF. I found that WPF provides a PrintDialog.PrintVisual method for printing any WPF control derived from the Visual class.

PrintVisualwill only print a single page so I need to scale the control to fit on the page. Unfortunately this would not work for me since the report was sometimes long enough that it could not be read easily when scaled to fit on the page.

Another option for printing provided by WPF is to create a separate view in a FlowDocument. This is probably the best way to print documents, but it was more work than I wished to put into it, not to mention the extra view that would have to be maintained for each control I wished to print.

I got another solution in this link, but it seems too complex for me.

Is there any better and simple solution for this ? Thanks for any help

like image 936
SHASHI KUMAR S Avatar asked Oct 30 '22 19:10

SHASHI KUMAR S


1 Answers

I'm assuming your report is displayed in a DataGrid or something else that is scrollable?

I believe FlowDocument is definitely your best choice here if you want to print something that looks, for lack of a better word, professional. But if you want something quick and dirty, you could use a series of operations using RenderTargetBitmap.Render. The basic process would be:

  1. Create the RenderTargetBitmap
  2. Scroll the view such that you have a region visible that you want to print on one page
  3. Call RenderTargetBitmap.Render on the DataGrid or the ScrollViewer that's containing the "large" control
  4. Print the resulting bitmap
  5. Repeat for the next "Page"

Again, don't call RenderTargetBitmap.Render on the "large" control. Wrap the large control in a ScrollViewer if it isn't already. That will essentially be your paginator.

I don't know if you'll be satisfied with the results, but this is the easiest method I can think of. It'll look like you manually hit PrintScreen each time. Not sure if that's what you want, but if you want it to look nicer, I think you need to use FlowDocument.

like image 176
Peter Moore Avatar answered Nov 17 '22 22:11

Peter Moore