Silverlight 2 RTW read XPS Solution

Source: Internet
Author: User
Tags silverlight
Lighting up the XML Paper Specification [Proof-of-concept XPS reader for Silverlight!]

Since getting involved with Silverlight and finding out the XPS document type WPF enables has XAML at its core, I 've been wondering how Silverlight wocould do as a lightweight XPS viewer.

First, a bit of background: WPF is the Windows Presentation Foundation and represents a new approach to UI for Windows. XPS refers to the XML Paper Specification, a device-independent file format for flexible document representation (think PDF) that's part of Office 2007 and. NET 3.0. WPF offers rich support for displaying XPS documents via its DocumentViewer and XpsDocument classes (among others ). because the 1.1 Alpha release doesn't currently include the relevant classes, Silverlight wouldn't appear to be well suited for XPS document display at first glance...

However, Silverlight does have the Downloader class which extends des support for packages (for the purposes of this discussion, packages are basically just ZIP archives ). since an XPS document is really just a package, and the core document format XPS uses is XAML, and Silverlight speaks XAML (well, at least a subset of it !), Maybe it's not such a stretch to do XPS with Silverlight after all.

I thought it wocould 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:

Go ahead and click here (or on the image above) to play around with the application in your browser. if you find yourself wondering how it works, just click here to download the complete source code/resources and play around with it yourself! (To build the SimpleSilverlightXpsViewer project, you'll want to use Orcas Beta 1 and the Silverlight Tools .)

Of course, this is just a proof-of-concept application built on an Alpha platform, so there are some rough edges. Some notes are in order:

  • I created my own XPS documents ents so I wouldn't have to worry about getting permission to use someone else's XPS documents. office 2007 comes with a handy "Microsoft XPS document writer" Printer Driver that lets you create an XPS document from any application simply by printing to the xps "Printer" (which then saves the resulting 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 understand the internal format of other valid XPS files. 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 renamed, some attribute values are tweaked, and some attributes are removed entirely. the tweaking is done to address the glyphs. fonturi/imagebrush. imagesource 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 (Package) itself. this is great for Simplifying Distribution 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. setSource 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 SetSource is made ), so SimpleSilverlightXpsViewer doesn't enable this by default. interested parties can#define SETSOURCE(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 Glyphs. 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 SetFontSource method that seems interesting, but xps xaml uses the Glyphs class which doesn't seem to support SetFontSource.
  • 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 cocould 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 image) -SimpleSilverlightXpsViewer simply ignores the TIFF images and neatly side-steps the support issue.

While simplesilverlightxpsviewer is a cute proof-of-concept application I enjoyed writing, it is hardly the final word on Silverlight XPS support. (Hey, I'm not even on the Silverlight team !) I don't know 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 shoshould be possible to provide a pretty compelling XPS-like user experience for Silverlight!

Ref: http://blogs.msdn.com/delay/archive/2007/05/22/lighting-up-the-xml-paper-specification-proof-of-concept-xps-reader-for-silverlight.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.