I'd like to create an XPS document for storing and printing.
What is the easiest way to create an XPS document (for example with a simple grid with some data inside) in my program, and to pass it around?
I'd like to create an XPS document for storing and printing.
What is the easiest way to create an XPS document (for example with a simple grid with some data inside) in my program, and to pass it around?
If you are working in .NET (v2 or later), you can very easily generate a valid XPS document from a WPF visual.
For an example, take a look at this blog post of mine:
http://nixps.blogspot.com/2008/12/wpf-to-pdf.html
In the example I create a WPF visual and convert it as an XPS file, before doing further processing.
If you are not working in .NET, or want more control on the XPS output, then I would advise you to use a library (like the NiXPS SDK) for this. It is a lot easier to code for, and a lot less error prone than writing out the XML constructs yourself (and doing proper resource management, etc...).
All it is, is really XML. If you're comfortable working with XML files, you should have no problem working with XPS documents. Here's a simple tutorial I've used in the past to get me started:
http://blogs.ocrasoft.nl/jeroenveurink/?p=21
Nothing easy about it. But it can be done. I've got some (sadly, still buggy) sample code and information on my blog for creating the document in memory.
Here's some code I whipped up for testing that encapsulates everything (it writes a collection of FixedPages to an XPS document in memory). It includes code for serializing the document to a byte array, but you can skip that part and just return the document:
Note the crappy threading code. You can't do this on MTA threads; if you are on an STA thread you can get rid of that as well.