
Printing
Most of the programs described in earlier chapters display output on the computer’s screen.
Chapters 29 and 30 explain how to save output in files.
This chapter explains a third method for saving output: printing. Using these techniques, you
can print text, shapes, images — just about anything you want.
Before you start a printing project, however, be warned that printing in C# isn’t
trivial. It’s easy enough to display some text or a few lines in a printout, but
producing a complex formatted document can be a lot of work.
If you need to produce a nicely formatted resume, graph, or grid of values, you
should ask yourself whether there’s an easier way. For example, Microsoft Word
is great at producing nicely formatted text documents, and Microsoft Excel
does a wonderful job of making charts and graphs. You can certainly generate
these sorts of printouts using C#, but it may be a lot faster if you use another
tool such as Word or Excel.
Note that this isn’t the only way to print with C# code. For example, a C# pro-
gram that uses Windows Presentation Foundation (WPF) can build WPF docu-
ments for later printing, or it can use the WPF printing techniques described in
Lesson 41. A program can also use a reporting tool such as Crystal Reports.
BASIC PRINTING
The PrintDocument component sits at the center of the printing process. To print, a program
creates an instance of this class ...