Thinking about adding printer support to your Windows Store app? Do you want to use XAML for pagination and printing? Does you app enable users to print the next George R. R. Marten novel? If so XAML probably isn’t the right solution for you. Don't despair, you can print to your heart's content using Direct2D and DirectWrite. A fitting solution for any Lannister (more later).
So why not use XAML to print that novel? There are known resource pitfalls associated with XAML printing. The XAML printing subsystem requires that you pass ALL of your pages to it before it will render. Once you pass in all of your pages the PrintManager then sends your pages to the print spooler. This requires that every page be cached. Each page is a fully initialized instance of its XAML representation. Once fully cached every page gets rendered as a bitmap. This all happens in memory.
Five pages? No problem. Ten pages? Sure. Seven hundred and fifty? Probably not. Your mileage will certainly vary depending on the device type and available system resources. Unlike desktop apps Windows Store apps don’t allow you to use up to your process limit (usually about 2 GB for x86) you get much less. If you are trying to print lots of pages on a resource constrained system it is very likely that the Process Lifecycle Manager (PLM) will freak out. The PLM will say something like: "Your using too much memory Dave!" and unceremoniously shut down. You might be able to cut your document into sections and print each section separately but the runtime will require your user to hit the print button for each section. Not necessarily the best user experience.
As mentioned above the good news is that you can print as may pages as you want simply by using Direct2D and DirectWrite instead of XAML. Unfortunately for our C# / XAML programmers you likely will need to learn some C++ / Cx to implement a solution. Direct2D and DirectWrite. allow you to “stream” the pages to the print spooler. The print spooler is backed by disk cache and doesn’t cause the same memory pressure problems (although you can still theoretically run out of disk space) as the XAML solution. Needless to say this is the ideal solution but does require you to write a solution using C++ / Cx since we don't support calling these technologies from managed languages. While learning C++ / Cx sounds a little scary the benefits may out way the challenges. Direct2D and DirectWrite are extremely powerful and flexible APIs that give you pixel perfect pagination every time.
If you are intersted in going the Direct2D and DirectWriter route we have a good sample to help get you started:
Direct2Dapp printing sample
Direct2D printing images and effects sample
I hope you find your hard copy bliss!
Follow the Windows Store Developer Solutions team on Twitter @wsdevsol. Comments are welcome, both below and on twitter.