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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

376 ❘ CHAPTER 16 Printing<br />

This code sets PagesPrinted to 0 because no pages have been printed yet during this round of printing.<br />

It then copies the ParagraphInfo structures from the AllParagraphs list (which holds all the<br />

data) into the ParagraphsToPrint list (which holds those paragraphs that have not yet been printed).<br />

Before it prints each page, the PrintDocument object raises its QueryPageSettings event. The<br />

program uses the following code to catch this event and prepare the next page for printing.<br />

// Set the margins for the next page.<br />

private void bookletPrintDocument_QueryPageSettings(object sender,<br />

QueryPageSettingsEventArgs e)<br />

{<br />

// Use a 1 inch gutter. (Printer units are 1/100th inch).<br />

const int gutter = 100;<br />

}<br />

// See if the next page will be the first, odd, or even.<br />

if (PagesPrinted == 0)<br />

{<br />

// The first page. Increase the left margin.<br />

e.PageSettings.Margins.Left += gutter;<br />

}<br />

else if ((PagesPrinted % 2) == 0)<br />

{<br />

// It's an odd page. Shift the margins right.<br />

e.PageSettings.Margins.Left += gutter;<br />

e.PageSettings.Margins.Right -= gutter;<br />

}<br />

else<br />

{<br />

// It's an even page. Shift the margins left.<br />

e.PageSettings.Margins.Left -= gutter;<br />

e.PageSettings.Margins.Right += gutter;<br />

}<br />

This code positions the new page’s gutter. If this is the first page, then the page’s margins have their<br />

default values and do not include a gutter. In that case, the code increases the left margin to add a<br />

1-inch gutter.<br />

If this isn’t the first page, the code determines whether it is an odd or even page. If this is an even page,<br />

then the previous page was odd, so the gutter is currently on the left. To move the gutter to the right<br />

side of the page, the code adds 1 inch to the left margin and subtracts 1 inch from the right margin.<br />

Similarly, if this is an even page, the code subtracts 1 inch from the left margin and adds 1 inch to<br />

the right margin.<br />

After the QueryPageSettings event finishes, the PrintDocument object raises its PrintPage event<br />

to generate the next printed page. The program’s PrintPage event handler is fairly long, so the following<br />

paragraphs describe it in pieces. (Download the example to see it all in one chunk.)<br />

The following code shows how the event handler starts.<br />

private void bookletPrintDocument_PrintPage(object sender, PrintPageEventArgs e)<br />

{<br />

// Increment the page number.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!