03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

360 ❘ CHAPTER 16 Printing<br />

When you master the basic concepts, printing is mostly a matter of learning how to draw graphics.<br />

Drawing graphics in a Windows Forms application is practically the same whether you’re drawing<br />

on a form, PictureBox, or printed page.<br />

The following section describes the basic process. The sections after that explain how to print specific<br />

items, such as text, images, and shapes. The final Windows Forms example shows how to display<br />

page numbers, change margins for odd and even pages, and split paragraphs across multiple pages.<br />

Basic Printing<br />

The heart of the printing process in Windows Forms applications is the PrintDocument object. As<br />

its name implies, it represents a document to be printed.<br />

You might expect the PrintDocument class to provide methods that you call to draw text, images,<br />

lines, and other items on the document. The process actually works in reverse. The program creates<br />

a PrintDocument object. It then directly or indirectly tells the object that it wants to generate a<br />

print preview or an actual printout. The PrintDocument then raises events asking the program what<br />

it should draw on each page of the printout. The program responds with drawing commands that<br />

determine the results.<br />

Perceptive Printing<br />

This process of the PrintDocument object raising event seems backward but it’s<br />

actually fairly clever. The PrintPage event, which is described in detail shortly,<br />

passes the program a Graphics object on which to draw. This is the same kind<br />

of Graphics object that a program uses to draw on a Form, PictureBox, Bitmap,<br />

or anything else in .NET. By giving you a Graphics object, the PrintPage event<br />

enables you to use the same kind of code you use to draw anywhere else.<br />

In fact, if you design the program properly, you can use the same code to draw on<br />

a Form, PictureBox, and printout. If you place all your drawing commands in a<br />

method that takes a Graphics object as a parameter, you can use that method to<br />

generate results for the screen or the printer.<br />

When a PrintDocument object must perform printing-related tasks, it raises four key events:<br />

➤➤<br />

➤➤<br />

➤➤<br />

BeginPrint—The PrintDocument raises its BeginPrint event when it is about to start<br />

printing. The program can initialize data structures, load data, connect to databases, and<br />

otherwise get ready to print.<br />

QueryPageSettings—Before it prints a page, the PrintDocument object raises this event.<br />

The program can catch this event and make changes that are specific to the page it is about<br />

to print. For example, if you are printing a booklet, the program can adjust the margins to<br />

leave extra space on the side of the page where the staples will be.<br />

PrintPage—The PrintDocument object raises the PrintPage event to generate a page.<br />

The program catches this event and uses the e.Graphics parameter to generate output. After<br />

it finishes printing the page, the program should set the value e.HasMorePages to true or<br />

false to tell the PrintDocument whether there are more pages to print after this one.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!