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.

382 ❘ CHAPTER 16 Printing<br />

When the WpfPrintShapes example program executes, it displays a Print button. When you click the<br />

button, the following code executes.<br />

private void printButton_Click(object sender, RoutedEventArgs e)<br />

{<br />

PrintDialog pd = new PrintDialog();<br />

if (pd.ShowDialog() == true)<br />

{<br />

// Print.<br />

pd.PrintDocument(<br />

new ShapesPaginator(<br />

new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight)),<br />

"Shapes");<br />

}<br />

}<br />

This code creates a PrintDialog object and calls its ShowDialog method to display it. If the user<br />

selects a printer and clicks Print, the ShowDialog method returns true. In that case, the program calls<br />

the dialog’s PrintDocument method, passing it a new ShapesPaginator object and a description of<br />

the document being printed.<br />

When the program creates the ShapesPaginator object, it passes the constructor a Size structure<br />

representing the page’s printable area.<br />

The ShapesPaginator object does all the interesting work of generating the document’s printed<br />

pages. The following code shows the ShapesPaginator class, except for the GetPage method, which<br />

is described shortly.<br />

public class ShapesPaginator : DocumentPaginator<br />

{<br />

// The area in which to print.<br />

private Size MyPageSize;<br />

// Save the page size.<br />

public ShapesPaginator(Size pageSize)<br />

{<br />

MyPageSize = pageSize;<br />

}<br />

// Create and return the requested page.<br />

public override DocumentPage GetPage(int pageNumber)<br />

{<br />

...<br />

}<br />

// If pagination is in progress and PageCount is not final, return false.<br />

// If pagination is complete and PageCount is final, return true.<br />

// In this example, there is no pagination to do.<br />

public override bool IsPageCountValid<br />

{<br />

get { return true; }<br />

}<br />

// The number of pages paginated so far.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!