15.02.2015 Views

C# 4 and .NET 4

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

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

1090 ❘ ChaPTer 37 creAtinG dOcuments with wpf<br />

To create an XPS document, you use the XpsDocument class from the namespace System.Windows<br />

.Xps.Packaging. To use this class, you will need to reference the assembly ReachFramework as<br />

well. With this class you can add a thumbnail (AddThumbnail()) <strong>and</strong> fixed document sequences<br />

(AddFixedDocumentSequence()) to the document, as well as digitally sign the document. A fixed<br />

document sequence is written by using the interface IXpsFixedDocumentSequenceWriter that in turn uses<br />

an IXpsFixedDocumentWriter to write the document within the sequence.<br />

If a FixedDocument already exists, there’s an easier way to write the XPS document. Instead of adding<br />

every resource <strong>and</strong> every document page, you can use the class XpsDocumentWriter from the namespace<br />

System.Windows.Xps. For this class the assembly System.Printing must be referenced.<br />

With the following code snippet you can see the h<strong>and</strong>ler to create the XPS document. First, a filename for<br />

the menu plan is created that uses a week number in addition to the name menuplan. The week number<br />

is calculated with the help of the GregorianCalendar class. Then the SaveFileDialog is opened to<br />

let the user overwrite the created filename <strong>and</strong> select the directory where the file should be stored. The<br />

SaveFileDialog class is defined in the namespace Microsoft.Win32 <strong>and</strong> wraps the native file dialog.<br />

Then a new XpsDocument is created where the filename is passed to the constructor. Recall that the XPS<br />

file uses a ZIP format to compress the content. With the CompressionOption you can specify whether the<br />

compression should be optimized on time or space.<br />

Next, an XpsDocumentWriter is created with the help of the static method XpsDocument.<br />

CreateXpsDocumentWriter(). The Write method of the XpsDocumentWriter is overloaded to accept<br />

different content or content parts to write the document. Examples of acceptable options with the Write()<br />

method are FixedDocumentSequence, FixedDocument, FixedPage, string, <strong>and</strong> a DocumentPaginator.<br />

In the sample code, only the fixedDocument that was created earlier is passed:<br />

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

{<br />

var c = new GregorianCalendar();<br />

int weekNumber = c.GetWeekOfYear(menus[0].Day,<br />

CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);<br />

string fileName = String.Format("menuplan{0}", weekNumber);<br />

var dlg = new SaveFileDialog<br />

{<br />

FileName = fileName,<br />

DefaultExt = "xps",<br />

Filter = "XPS Documents|*.xps|All Files|*.*",<br />

AddExtension = true<br />

};<br />

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

{<br />

XpsDocument doc = new XpsDocument(dlg.FileName, FileAccess.Write,<br />

CompressionOption.Fast);<br />

XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);<br />

writer.Write(fixedDocument);<br />

doc.Close();<br />

}<br />

}<br />

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

By running the application to store the XPS document, you can view the document with an XPS viewer, as<br />

shown in Figure 37-14.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!