I thought it would be a neat exercise to try to write an XPS viewer with the publically available Silverlight 1.1 Alpha bits so I gave it a try and ended up with an application I call SimpleSilverlightXpsViewer:
I created my own XPS documents so I wouldn't have to worry about get
Ting permission to use someone else's XPS document
s. Office 2007 comes with a h
Andy "Microsoft XPS Document Writer" printer driver that lets you create an XPS document from any application simply by prin
Ting to the XPS "printer" (which then saves the resul
Ting output to a file you specify). I created the three sample documents this way: the "Intro" document came from a simple Word document,the "Blog" document came from my blog via IE7,
and the "Site" document came from the Silverlight Forums via IE7 with a landscape page layout.
Because the only kind of XPS document I've worked with is the kind the XPS printer driver outputs,there's a very good chance SimpleSilverlightXpsViewer won't underst
and thE internal format of other valid XPS file
s. Remember,though,that I didn't set out to write an XPS file parser - I just set out to write a simple XPS viewer for Silverlight. :)
The translation of "XPS XAML" to "Silverlight XAML" is done by the XpsToSilverlightXamlReader class,a minimal derivation from XmlReader that performs on-the-fly modification of the "XPS XAML" to translate it into "Silverlight XAML". Specifically,some elements are r
enamed,some attribute values are tweaked,and some attributes are removed entirely. The tweaking is done to address the Glyph
s.FontUri/ImageBrush.Image
source issue mentioned next and re-points the relevant content to an external
LOCATIOn.
XPS documents are entirely self-contained,with any necessary fonts and images embedded in the file (packag
E) itself. This is great for simplifying di
Stribution and Silverlight's Downloader class makes it easy to get at individual files in a package. However,SimpleSilverlightXpsViewer works best when the images
and the fonts are extracted from the XPS file:
Under the right conditions,embedded images can be fetched by Silverlight by using the ImageBrush.Set
source method. However,things tend to break if there are multiple references to the same image in a single page (an exception is thrown when the second call to Set
source is mad
E),so SimpleSilverlightXpsViewer doesn't enable this by default. Interested parties can #define SET
sourcE (for both C# files) to experiment with this feature (things work fine for the first page of all of the sample documents,but break on the second pages of the Blog and Site documents).
The default behavior of Glyph
s.FontUri does not seem to automatically pull the font out of the package -
at least not as it's used by SimpleSilverlightXpsViewer (possibly because Silverlight doesn't seem to like the leading '/' on package-relative paths). TextBlock has a SetFont
source method that seems interes
Ting,but XPS XAML uses the Glyphs class which doesn't seem to support SetFont
source.
For some reason,the XPS documents generated by the XPS printer driver aren't directly open-able by Silverlight's Downloader (a COM exception is thrown). However,I've found that a quick un-ZIP/re-ZIP with either of my favorite ZIP tools yields XPS documents that open right up. I
SUSPECT this is due to a simple issue with the Alpha Downloader implementation (endian-
ness of the ZIP file,some special section embedded by the XPS printer driver,etc.) that
Could be fixed by the Silverlight team without much difficulty.
Silverlight doesn't support the TIFF file format (which is not surprising because full support can be quite complex and TIFF images are hardly ever used on the web). As it happens,XPS printer driver output may contain TIFF images (it seems they're used as a mask of some kind behind another PNG or JPG imag
E) - SimpleSilverlightXpsViewer simply ignores the TIFF images and neatly side-steps the support issue. :)
While SimpleSilverlightXpsViewer is a cute proof-of-concept application I enjoyed wri
Ting,
it is hardly the final word on Silverlight XPS support. (Hey,I'm not even on the Silverlight team!) I don't k
Now what the official plans are for more formal XPS support in the Silverlight platform,but my experience with SimpleSilverlightXpsViewer suggests that most of the pieces are already in place for a pretty reasonable XPS experience with the Silverlight 1.1 Alpha. Throw in a couple of tweaks to Silverlight (and/or SimpleSilverlightXpsViewer!),
and it should be possible to provide a pretty compelling XPS-like user experience for Silverlight!