15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

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

Printing ❘ 1091<br />

figure 37-14<br />

To one overload of the Write() method of the XpsDocumentWriter you can also pass a Visual, which is<br />

the base class of UIElement, <strong>and</strong> thus you can pass any UIElement to the writer to create an XPS document<br />

easily. This functionality will be used in the following printing example.<br />

PrinTing<br />

The simplest way to print a FixedDocument that is shown on the screen with the DocumentViewer is to<br />

invoke the Print() method of the DocumentViewer that has the document associated. This is all that<br />

needs to be done with the menu planner application in an OnPrint() h<strong>and</strong>ler. The Print() method of<br />

the DocumentViewer opens up the PrintDialog <strong>and</strong> sends the associated FixedDocument to the<br />

selected printer:<br />

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

{<br />

viewer.Print();<br />

}<br />

Printing with the Printdialog<br />

code snippet CreateXps/DocumentPage.xaml.cs<br />

If you want more control over the printing process, the PrintDialog can be instantiated, <strong>and</strong> the<br />

document printed with the PrintDocument() method. The PrintDocument() method requires a<br />

DocumentPaginator with the first argument. The FixedDocument returns a DocumentPaginator object<br />

with the DocumentPaginator property. The second argument defines the string that shows up with the<br />

current printer <strong>and</strong> in the printer dialogs for the print job:<br />

var dlg = new PrintDialog();<br />

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

{<br />

dlg.PrintDocument(fixedDocument.DocumentPaginator, "Menu Plan");<br />

}<br />

code snippet CreateXps/DocumentPage.xaml.cs<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!