WPF Printing Performance and PDF

Printing in WPF has been a big thorn for me over the last few years, it just feels like you have to learn everything the hard way through extensive trial and error… and better write it down or you’ll be re-learning it again later.

One hard lesson for me has been printing a lot of vector based graphics in a particular application. Early on the data would wind up rasterizing for various reasons and creating massive rasterized data pages, which when sent to the printer would cloe things way down and create ugly printouts (someone has a case of the jaggies). Even PDF printing suffered from this, so instead of a nice lightweight PDF with some lines and text drawn on the page, it would barf out a giant file with every page represented in ugly rasterized pixels.

The first thing to make sure of if you fighting this is to make sure any element you are printing doesnt use any form of transparency (opacity). I had an element sneak back into my app recently which had text printed with a background color with opacity set to 35%. Not sure if this is the fault of WPF, PDF, or the particular Acrobat software I’m using, but even a single element with this opacity setting on a page will cause the entire page to be rasterized… not just the element itself, as one might expect.

The other side effect of this rasterization is slow printing. Even printing to a local PDF file can take over a minute PER PAGE on my high-end laptop. So if your print times seem astronomical, you’re likely running into this same issue.

I recall a number of other weird issues encountered in the past, I’ll copy them here if I can find them again. But for now, note to self: NO opacity variations.

 

Update:

I found out today another item that can cause rasterization – if you have a canvas with clipping turned on, and items hanging out beyond the edge of the canvas. Doesnt seem to always do it though, it might only occur if the items clipped by the canvas extend beyond the edge of the printed page. I experienced this again today and now I’ll have to figure out yet another workaround for it.