Skip to Main Content
Programming WPF, 2nd Edition
book

Programming WPF, 2nd Edition

by Chris Sells, Ian Griffiths
August 2007
Intermediate to advanced content levelIntermediate to advanced
864 pages
25h 52m
English
O'Reilly Media, Inc.
Content preview from Programming WPF, 2nd Edition

Generating XPS Output

Whether you are printing, or writing your output to an XPS file, you can use the same API: the XpsDocumentWriter class. If you wish to print, you can obtain one of these from the printing API, as Example 15-9 shows.

Example 15-9. Obtaining an XpsDocumentWriter for printing

PrintDocumentImageableArea imageArea = null;
XpsDocumentWriter xpdw = PrintQueue.CreateXpsDocumentWriter(ref imageArea);
if (xpdw != null) {
    ...provide XpsDocumentWriter with output here...
}

This will cause the standard print dialog to be shown, allowing the user to select a printer. If the user cancels the dialog, CreateXpsDocumentWriter returns null, but otherwise it returns an XpsDocumentWriter, along with an object that describes the size of the target's paper and the margins of the printable area. If you wish to exercise more control over the print dialog and printer selection, there are several variations on this theme, described later in this chapter.

If you wish to send your output to an XPS file instead of a printer, you can obtain the document writer using the code shown in Example 15-10.

Example 15-10. Obtaining an XpsDocumentWriter for file output

using (XpsDocument xpsFile = new XpsDocument(xpsOutputPath, FileAccess.Write)) {
    XpsDocumentWriter xpdw = XpsDocument.CreateXpsDocumentWriter(xpsFile);

    ...provide XpsDocumentWriter with output here...

}

Once you have obtained an XPS document writer, you can use the same code whether you are writing to an XPS file or to a printer. For clarity, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming C#, 4th Edition

Programming C#, 4th Edition

Jesse Liberty
Programming C# 10

Programming C# 10

Ian Griffiths

Publisher Resources

ISBN: 9780596510374Supplemental ContentErrata Page