How does printing work in Windows Presentation Foundation (WPF)? We gathered some helpful excerpts and examples from the WPF Reflections blog, by Mark Shurmer below.
If you have ever worked with printing in MFC, then printing in Windows Presentation Foundation is so advanced, as to make you dizzy. Even if you ever tried printing with Windows Forms, then printing in WPF is a bit of a revelation.
How does it work?
Most of the printing stuff is in the System.Printing namespace, and a good starting place is the PrintDialog class. By instantiating the PrintDialog class, you can display the standard XP or Vista print dialog to allow the user to choose where to print, and how.
What else does it give you?
You can call two different methods to do printing. PrintVisual allows you to print any class that derives from System.Windows.Media.Visual, which is a lot. And PrintDocument, which allows you to print any DocumentPaginator (e.g. FlowDocument or XpsDocument).
Your element, canvas, grid or other object may print out very small. I explain why this happens and demonstrate how you can use a Transform before printing to correct.
Also, when printing a multiple-page document from a FlowDocumentPageViewer or FlowDocumentReader, the pages may scale to the same size as the document being shown on the screen. Here's how to change the height and width of the document before printing.
| GOT DEVELOPER QUESTIONS? YOUR PEERS HAVE ANSWERS