Welcome to MSDN Blogs Sign in | Join | Help

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 would 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 includes 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 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:

SimpleSilverlightXpsViewer Application

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 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 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 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 should be possible to provide a pretty compelling XPS-like user experience for Silverlight!

Published Tuesday, May 22, 2007 12:27 PM by Delay
Filed under:

Attachment(s): SimpleSilverlightXpsViewer.png

Comments

# Silverlight prototype of XPS Viewer

Tuesday, May 22, 2007 9:10 PM by WPF Team Bloggers

# Silverlight XPS Document Reader Proof-Of-Concept

Tuesday, May 22, 2007 10:38 PM by Windows Live Quantum Mechanics

Delay started out poking around in the XPS document format. Before he could stop himself, he had an XPS

# Do you see the Silverlight?

Tuesday, May 22, 2007 10:41 PM by Adrian Ford

Delay has a great proof-of-concept example that shows how a simple XPS viewer can be implemented in Silverlight

# David Anson's Silverlight Based XPS Viewer

Wednesday, May 23, 2007 1:12 AM by Mike Harsh's Blog

David Anson has created an XPS viewer using the Silverlight 1.1 Alpha. This app is a very cool idea and

# Silverlight XPS viewer

Wednesday, May 23, 2007 2:56 AM by Wictor Wilen

Delay has done it - a Silverlight "proof-of-concept" XPS viewer! Head over to his post and try it out, this might be the start of the cross-platform XPS viewer that we all long for...

# XPS reader for Silverlight

Wednesday, May 23, 2007 5:12 AM by neuhawk

Delay 发布了一个SimpleSilverlightXpsViewer 小软件

# [Silverlight] Proof-of-concept XPS reader for Silverlight

Wednesday, May 23, 2007 6:48 AM by Damir Tomicic : ein Tag in der Community

# Silverlight Cream for May 23, 2007

Wednesday, May 23, 2007 1:20 PM by WynApse

Silverlight Cream for May 23, 2007

# Why XML in Silverlight is Significant

Wednesday, May 23, 2007 5:55 PM by Synergist

I have been working with XML for most of my career, from developing a CAD application to my roles at

# Silverlight news - May 24, 2007

Thursday, May 24, 2007 8:07 AM by Silverlight City

Silverlight news - May 24, 2007

# Points of interest #9

Saturday, May 26, 2007 4:25 PM by John Mandia's Points of Interest

Well this week was a nice rest, most of it spent relaxing with my wife. So it was a non-coding week but

# My "Lap Around Silverlight" Talk at TechEd

Wednesday, June 06, 2007 11:53 PM by ScottGu's Blog

On Monday I gave an overview talk called "A Lap Around Silverlight" at TechEd in Orlando. You can download

# חומרים חדשים על Silverlight

Tuesday, June 12, 2007 7:46 PM by It's All About Causality

לאט לאט עולים יותר הדגמות וחומרים על Silvelight, הנה כמה דברים מעניינים: מימוש של גרפים ב Silverlight

# re: Lighting up the XML Paper Specification [Proof-of-concept XPS reader for Silverlight!]

Wednesday, June 13, 2007 9:53 PM by veeral

G'day,

I have been playing with XPS/Silverlight for a few days. I have noticed the sample XPS document you have provided with your sample code contains several ottdf file. How do you generate such a file ? My understanding is that  font data is included within the XPS file unless i am incorrect.

Another question for you is that if I used the Save As XPS option within Office 2007 within the XPS package there is a file FixedDoc.fdoc under Documents\1\. Whereas if I used the XPS printer driver the file is FixedDocument.fdoc.

Why is there a difference ? In your code you are referring to FixedDocument.fdoc.

thanks

Veeral

# re: Lighting up the XML Paper Specification [Proof-of-concept XPS reader for Silverlight!]

Wednesday, June 13, 2007 10:14 PM by veeral

sorry mate i reread your post above regarding the ottdf file. Its included within Documents\1\Resources\Fonts path.

Still interested in the fixeddoc.fdoc and fixeddocument.fdoc issue.

# re: Lighting up the XML Paper Specification [Proof-of-concept XPS reader for Silverlight!]

Thursday, June 14, 2007 2:49 AM by Delay

veeral,

Regarding the internal XPS file structure difference between documents generated by the XPS Printer Driver vs. Office, that's something I was aware of [and touched on with my second bullet point about not setting out to write a full XPS parser :) ]. Like the difference you cite, the differences I've seen so far have seemed pretty minor and I suspect that both variants are fully valid XPS files according to the spec. I just happened to focus my efforts on the XPS Printer Driver variant (and knew I controlled the sample data), so that's what the sample code currently assumes. I think it would be relatively straightforward to support the Office variant, too, but that was somewhat tangential to my goal of proving the concept of an XPS viewer in Silverlight.

Hope that explains things!

# re: Lighting up the XML Paper Specification [Proof-of-concept XPS reader for Silverlight!]

Thursday, June 14, 2007 11:49 PM by veeral

thanks for the feedback. It makes sense after a good sleep.

I have been reading the XPS specification and the spec states that the actual name of the file with the extension fdoc can be anything as long as the long as the other aspects of the part naming conventions are adhered to. Just read up on section 2.2 and S2.25.

Now I got to spend a bit more time with the XAML syntaxes between the two platforms.

You guys pump out so many new things so quickly its hard for me to keep abreast with everything. :D

What amazes me is how you managed to write this app and the ms surface app so quickly. I guess you have access to the team and internal resources. I really do envy you alot. :-)

thanks for your help.

veeral

# A new Silverlight version has been released! [Samples updated for the 1.1 Refresh]

Friday, July 27, 2007 7:14 PM by Delay's Blog

With today's release of the Silverlight 1.0 RC and 1.1 Refresh , I've updated the Silverlight samples

# A new Silverlight version has been released! [Samples updated for the 1.1 Refresh]

Friday, July 27, 2007 7:15 PM by Noticias externas

With today's release of the Silverlight 1.0 RC and 1.1 Refresh , I've updated the Silverlight

# איך לעשות Upgrade ל Silverlight 1.1 בגרסה החדשה

Saturday, July 28, 2007 7:50 PM by It's All About Causality

Delay מפרט את השלבים שהוא עשה כדי לעשות Upgrade לכמה דברים שהוא כתב ב Silverlight 1.1. בינהם ה Air Lines

# re: Lighting up the XML Paper Specification [Proof-of-concept XPS reader for Silverlight!]

Thursday, August 02, 2007 4:33 PM by cesarzam

Cool tool, I wonder if I can use this in my project, I am using Windows 2003, VS.NET c# ASP.net. I am plannig to convert all documents to .XPS and use the XPS viewer to view document overt the interner. Any Suggestions?

All my web application was coded in ASP.net,C#, Windows 2003. It is too late for me to change to Vista for the monent.

# re: Lighting up the XML Paper Specification [Proof-of-concept XPS reader for Silverlight!]

Thursday, August 02, 2007 5:08 PM by Delay

cesarzam,

You could use something like my demo application or just rely on the native .XPS viewing that comes with .NET 3.0. It's my understanding that Vista is not a requirement for XPS viewing. You can find more details here: http://www.microsoft.com/whdc/xps/default.mspx

Thanks and good luck!

# [PL] Przeglądarka XPS w Silverlight

Thursday, August 09, 2007 6:27 AM by Only Human | Devoted to technology v.2.0

Bardzo mi się podoba fakt, że niektóre technologie kojarzone jako "produkty" bardzo odrębnie, ze względu

# [PL] Przeglądarka XPS w Silverlight

Thursday, August 09, 2007 7:34 AM by Noticias externas

Bardzo mi się podoba fakt, że niektóre technologie kojarzone jako "produkty" bardzo odrębnie

# XPS rozwija się

Thursday, October 11, 2007 11:14 AM by SocialITy

Dla porządku nieco wstępu teoretycznego - XPS ( XML Paper Specification ) to jeden z nowych formatów

# Приложение для просмотре XPS на Silverlight

Friday, October 12, 2007 6:35 AM by Gaidar Magdanurov

Интересное приложение, созданное с использованием Silverlight, опубликовано в Delay's Blog . Приложение

# Приложение для просмотре XPS на Silverlight

Friday, October 12, 2007 6:36 AM by Gaidar Magdanurov

Интересное приложение, созданное с использованием Silverlight, опубликовано в De

# 点燃XML Paper Specification[Proof-of-concept]XPS reader for Silverlight

Saturday, October 13, 2007 1:34 AM by works guo

这是一篇关于基于Silverlight 1.1 alpha 的XPS查看器,现在微软还没有官方的基于Silverlight的 XPS 查看器,但这位开发者是非Silverlight 团队的,他制作一个可以很好的查看XPS文档的查看器而且还有源代码下载,虽然能使用但是有一些问题(在文中会谈到).

# [Ww.Web] SilverDark

Thursday, January 24, 2008 7:45 AM by Ww.CodeSculptor

Postanowiłem wziąść udział w konkursie Silverlighta (SL). Długo szukalem tematu. Po akademicku zacząłem

# David Anson's Silverlight Based XPS Viewer

Wednesday, February 20, 2008 2:54 AM by Mirrored Blogs

David Anson has created an XPS viewer using the Silverlight 1.1 Alpha. This app is a very cool idea and

# Proof-of-concept Silverlight XPS reader gets some Beta 1 love [SimpleSilverlightXpsViewer updated for Silverlight 2 Beta 1!]

Tuesday, April 08, 2008 6:05 PM by Delay's Blog

After a few customer requests to update my SimpleSilverlightXpsViewer proof-of-concept XPS reader for

# XAML, XPS, WPF, XPSviewer, Silverlight ?

Wednesday, April 09, 2008 7:00 PM by PDF 千夜一夜

Windows Vistaに伴って、いろいろと新しいMSのアーキテクチャが導入さ...

Anonymous comments are disabled
 
Page view tracker