Printing in WPF (XPS)

I wanted to print a visual out to an XPS file on a recent project, and couldn't find the code to do it. Anyway I eventually found it so I thought I'd post it. You need references to ReachFramework and System.Printing.

using System.Windows.Xps.Packaging;
using System.Windows.Xps;
Package pckg = Package.Open(@"c:\x.xps", FileMode.Create);
XpsDocument xpsDoc = new XpsDocument(pckg);
XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
xpsWriter.Write(visual);
xpsDoc.Close();
pckg.Close();

Comments