
Printing with WPF
Lesson 31 explained how to print in a Windows Forms application. Your program catches
a
PrintDocument object’s PrintPage event handler and uses its e.Graphics parameter to
generate graphics for each page of the printout.
Windows Presentation Foundation (WPF) uses a different printing model that many program-
mers find more intuitive. Instead of making your program respond to
PrintPage events to gen-
erate printed pages, a WPF program’s code can directly print visual objects that it draws using
WPF controls such as
Label and TextBox.
In addition to being easier to understand, this approach has a couple of other benefits.
For example, it lets the program use the same kind of code to display and print data. In
Windows Forms, a program uses controls such as
TextBox and Label to display text on the
screen but it uses a
Graphics object’s DrawString method to draw text on a printout. WPF
uses the same kinds of
TextBoxes and Labels for both display and printing.
WPF also allows you to zoom in as much as you like without creating a pixellated result. That
means, for example, you can enlarge a window as much as you like for a printout and you’ll
still see a smooth result.
In this lesson, you learn how to produce printouts in WPF. You learn how to print images of win-
dows and other visual objects displayed on the screen centered and scaled to fill the printed page.
PRINTING VISUALS ...