[ACCEPTED]-How to create an XPS document?-xpsdocument

Accepted answer
Score: 15

Nothing easy about it. But it can be done. I've 12 got some (sadly, still buggy) sample code 11 and information on my blog for creating 10 the document in memory.

Here's some code 9 I whipped up for testing that encapsulates 8 everything (it writes a collection of FixedPages 7 to an XPS document in memory). It includes 6 code for serializing the document to a byte 5 array, but you can skip that part and just 4 return the document:

public static byte[] ToXpsDocument(IEnumerable<FixedPage> pages)
    // Note, this is test code, so I don't care about disposing my memory streams
    // You'll have to pay more attention to their lifespan.  You might have to 
    // serialize the xps document and remove the package from the package store 
    // before disposing the stream in order to prevent throwing exceptions
    byte[] retval = null;
    Thread t = new Thread(new ThreadStart(() =>
        // A memory stream backs our document
        MemoryStream ms = new MemoryStream(2048);
        // a package contains all parts of the document
        Package p = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);
        // the package store manages packages
        Uri u = new Uri("pack://TemporaryPackageUri.xps");
        PackageStore.AddPackage(u, p);
        // the document uses our package for storage
        XpsDocument doc = new XpsDocument(p, CompressionOption.NotCompressed, u.AbsoluteUri);
        // An xps document is one or more FixedDocuments containing FixedPages
        FixedDocument fDoc = new FixedDocument();
        PageContent pc;
        foreach (var fp in pages)
            // this part of the framework is weak and hopefully will be fixed in 4.0
            pc = new PageContent();
        // we use the writer to write the fixed document to the xps document
        XpsDocumentWriter writer;
        writer = XpsDocument.CreateXpsDocumentWriter(doc);
        // The paginator controls page breaks during the writing process
        // its important since xps document content does not flow 

        // this part serializes the doc to a stream so we can get the bytes
        ms = new MemoryStream();
        var writer = new XpsSerializerFactory().CreateSerializerWriter(ms);

        retval = ms.ToArray();
    // Instantiating WPF controls on a MTA thread throws exceptions
    // adjust as needed
    t.Priority = ThreadPriority.AboveNormal;
    t.IsBackground = false;
    //~five seconds to finish or we bail
    int milli = 0;
    while (buffer == null && milli++ < 5000)
    //Ditch the thread
    // If we time out, we return null.
    return retval;

Note the crappy threading 3 code. You can't do this on MTA threads; if 2 you are on an STA thread you can get rid 1 of that as well.

Score: 7

If you are working in .NET (v2 or later), you 12 can very easily generate a valid XPS document 11 from a WPF visual.

For an example, take a 10 look at this blog post of mine:


In the example 9 I create a WPF visual and convert it as 8 an XPS file, before doing further processing.

If 7 you are not working in .NET, or want more 6 control on the XPS output, then I would 5 advise you to use a library (like the NiXPS SDK) for 4 this. It is a lot easier to code for, and 3 a lot less error prone than writing out 2 the XML constructs yourself (and doing proper 1 resource management, etc...).

Score: 0

All it is, is really XML. If you're comfortable 4 working with XML files, you should have 3 no problem working with XPS documents. Here's 2 a simple tutorial I've used in the past 1 to get me started:


More Related questions