I know how to invoke the print dialog and do other things. My question is : what is the best practice to print a WPF page.
To invoke another page with all margins, formatting and layout worked out. And then call the print on that
To invoke a user control with all margins, formatting and layout worked out. And then call the print on that
To invoke a window with all margins, formatting and layout worked out. And then call the print on that
Or any other preferred way of doing this.
Please let me know how you approach printing ?
In WPF you have many options to print. The question which one is best suited dependes IMO extremely on the content to be printed and the desired quality.
Printing visual
If ouy want to print something that looks exactly like the representation in the UI, then you could try printing directly the visual that builds the root-content of your print-content. If you have positioning problems and have to correct properties of this visual, I would recommend to created a memory-copy of it, transform the copy and then print out this copy. Printing out visuals has IMO many drawbacks. I seldom use it except for short hacks and mini-tools. If I use, I mostly have to create the mentioned invisible copy and print out this. If you go this way, take care that you have to call Measure and Arrange from code.
Printing FlowDocument
Another possiblity is to print out a FlowDocument. If you have something that fits into FlowDocuments, I find this an easy way to create nice content to print out. Mostly if you have some simple reports, list ith some images, this way is a nice way. The print-quality is good and many complex operations will be done for you automatically as long as you don't want to override behaviour, and also this is possible. The quality of such print-out's is IMO very good.
XPS Printing
Hovever if you have complex content and you want it to print out nicely, you have to build XPS-content and print it over the XPS Print Path. This way, you have all possibilities and options, the qualitity is very good but you have the most work to do. I don't like it much, but mostly it's the way I take to print.
I assume, that your question mainly concerns the first block, however I think it could be helpfull to see which options you have, if printing the visual is not enought (for me it's very seldom enough). Hope this answers your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With