Exploring Metro: the Longhorn Document and Print Model

Exploring Metro: the Longhorn Document and Print Model

  • Comments 5

Metro is a suite of new document and print infrastructure technologies that are embedded in Longhorn and WinFX. I wrote about Metro in the Avalon Beta 1 RC guide, but in brief Metro provides a standardized ZIP-based document container and open document format, a new print pipeline and spool format, and a file viewer that supports fixed format documents. If you ever want to print or save a document in Avalon, or you want to create a portable file that can be viewed on a machine that doesn't even have your application installed, Metro is the technology for you. In preparation for a Channel 9 interview I'm conducting tomorrow with one of the architects in the Metro team, I've been spelunking through the object model to try and build a few small samples, and it's so cool that I wanted to share what I've got. Everything below runs with the published WinFX Beta 1 RC bits, available for download from MSDN (make sure you install the SDK if you want to build this stuff).

Download the full code sample here.

Creating a Metro file using WinFX just requires a few lines of code, and once you've created it you can print, save, edit at your leisure. Metro revolves around a ReachPackage object, which describes the container format, and is in turn created from a ZipPackage, which is simply a .NET representation of a ZIP library. ReachPackage exposes a number of methods such as AddReachFixedDocument, which allows you to work with fixed documents directly; it can also be used in conjunction with Avalon with a PrintableArchivableDocument object, which can write any Avalon visual to the ReachPackage. The following code fragment shows how this works:

  Package package =
    ZipPackage.Open(@"c:\test.container", FileMode.Create);
     
  ReachPackage reachPackage = 
    new ReachPackage(package);
    
  PrintableArchivableDocument pad = 
    new PrintableArchivableDocument(reachPackage);

  pad.Write(MyDocument); 

To test this out, I created a TextFlow object in XAML - since this derives from Visual, it can be used as the parameter for the PrintableArchivableDocument.Write() method.

This creates a file called c:\test.container, which you can open on any machine with the WinFX Beta 1 RC installed, and see what it looks like using the Metro file viewer.


click to enlarge

Another thing you can do with Metro is to add your own arbitrary parts. For the purposes of demonstration, the following code fragment adds a small HTML test file to the Metro container, but in practice this could be your application's own proprietary data stream.

  PackagePart part = package.CreatePart(new Uri("/test.html", 
    UriKind.Relative), "text/html", CompressionOption.Normal);

  // this is where you'd write your data
  using (StreamWriter sw = new StreamWriter(
         part.GetStream(FileMode.OpenOrCreate, FileAccess.Write)))
  {
    sw.WriteLine("<HTML><HEAD><TITLE>Metro Test</TITLE>" & 
                 "</HEAD><BODY>Custom data</BODY></HTML>");
    sw.Flush();
  }

Even cooler, you can actually rename this .container file to a .zip extension, and then you can see its contents and indeed work with them. Everything in the container is fully documented as an open format - see the specification. This is the same container format as Office 12 uses, so if you create your own applications to use the Metro container format, then any application that understands the Office format will be able to work with your application natively. Remember too that you can embed all kinds of metadata in the Metro container header, such as digital signatures for example.

Download the full code sample here.

  • You've been busy Tim! Thanks for the info I will really be able to use what you have provided!
  • Interesting stuff! However it does seem to overlap some of the functionality of Adobe Acrobat. How would you position the two technologies? (Or have I completely misunderstood :-)
  • Tim Sneath, a Longhorn Technical Evangelist, explores Metro in his June 15th post.

    &quot;Metro&amp;nbsp;provides...
  • A while ago Charles Torre and I shot a video with Jerry Dunietz, one of the principal architects for...
  • Is ZipPackage a trustable package?
    I am trying to unzip a zip using your directions, but always the package turns to be empty.

    Do you someone that can help?
    There is no help documentation on the web.

    Thanks,
    Lorenzo
Page 1 of 1 (5 items)